A software development team is building a service that publishes and consumes messages using Google Cloud Pub/Sub. To facilitate fast offline testing without incurring cloud resource charges, a developer starts the Pub/Sub emulator locally using the command `gcloud beta emulators pubsub start`. Which environment variable must be set in the local application environment to ensure the Google Cloud Client Libraries automatically route requests to the local emulator instead of live GCP endpoints?
- PUBSUB_EMULATOR_HOST set to the host and port of the running emulator (e.g., localhost:8085)Answer
- BGOOGLE_APPLICATION_CREDENTIALS set to a local service account JSON key file
- CTERRAFORM_STATE_BACKEND set to local file storage mode
- DGKE_CLUSTER_ENDPOINT set to a local Minikube cluster address
Answer
The environment variable PUBSUB_EMULATOR_HOST must be set to the emulator's host and port address (for example, localhost:8085).
Google Cloud Pub/Sub client libraries automatically check for the presence of the PUBSUB_EMULATOR_HOST environment variable upon instantiation. When this variable contains a host and port (such as localhost:8085), the SDK routes all publisher and subscriber calls to the local emulator, bypassing authentication requirements and remote API calls.
Step-by-Step Solution
Key Concept
Configuring Cloud Emulators for Local Testing
Estimated Time:45s