Storing Service Endpoints as Compute Engine Metadata

PCD By islever Editorial Last updated

Short answer

A Store the service endpoint as instance metadata retrieved at runtime so newly launched Compute Engine instances connect to the correct service version.

Question

Your application is composed of a set of loosely coupled services orchestrated by code executed on Compute Engine. You want your application to easily bring up new Compute Engine instances that find and use a specific version of a service. How should this be configured?
  1. Define your service endpoint information as metadata that is retrieved at runtime and used to connect to the desired service. Correct Answer
  2. Define your service endpoint information as label data that is retrieved at runtime and used to connect to the desired service.
  3. Define your service endpoint information to be retrieved from an environment variable at runtime and used to connect to the desired service.
  4. Define your service to use a fixed hostname and port to connect to the desired service. Replace the service at the endpoint with your new version.

Key Takeaways

For loosely coupled services on Compute Engine, the service endpoint should be defined as instance metadata read at runtime, making option A the correct choice. Metadata assigned at instance creation lets brand-new instances discover and connect to a specific service version without baked-in configuration. Labels, environment variables, and fixed hostnames each fail to provide runtime, version-aware endpoint discovery.

Expert Analysis

The application consists of loosely coupled services orchestrated by code on Compute Engine, and new instances must find and use a specific version of the service. The working mechanism is instance metadata: a Compute Engine instance retrieves its project or instance metadata over the instance metadata server at http://metadata.google.internal/ (token guarded by the Metadata-Flavor header) at runtime and reads the endpoint it needs.

The deciding constraint is that the information must be both discoverable by a freshly created instance and able to identify a specific version. Option A satisfies both: metadata is assigned to instances (project-wide or per-instance) when they are created, so a new instance booting with that same metadata key immediately knows which service endpoint — and therefore which service version — to call, without any baked-in configuration. Because metadata is read at each connection, redeploying a new service version behind the recorded endpoint is reflected in every instance that re-reads it.

Option B fails on the semantics of labels. Compute Engine labels are key/value pairs used to organise and filter instances themselves (for example in gcloud compute instances list --filter=...); they are not a supported way for an instance to resolve a downstream service address at runtime. Labels answer "which instances are these?", not "where does this version live?".

Option C fails because environment variables are static shell state captured when the process starts. If the endpoint changes to a newer service version, running processes keep using the old value until restarted; a new instance only inherits the variable if the orchestration code explicitly sets it, which reintroduces the hard-coding problem the metadata approach avoids.

Option D is the direct opposite of the requirement. A fixed hostname and port means every instance points at one address, so "replacing the service at the endpoint" mutates a shared target rather than letting each instance discover a specific version; there is no per-version selection and a broken rollout affects all callers at once.

The mechanism this question relies on but never states: the instance metadata server is reachable only from the instance itself (and via the Metadata-Flavor: Google header to prevent SSRF), and metadata set at instance creation is available to the guest immediately at boot. That is precisely why a "newly brought up" Compute Engine instance in option A can read the desired endpoint with no prior configuration, while the alternatives each lack either runtime refreshability or version targeting.

Official Reference

https://cloud.google.com/compute/docs/metadata/overview

This link opens the official vendor documentation in a new tab.

Exam Strategy

When an answer must let newly created instances discover a resource at runtime, look for the mechanism that is set at provisioning time and read on demand — instance metadata — rather than values baked into images or process state. Eliminate options that only classify resources (labels), freeze values at process start (environment variables), or pin a single mutable address (fixed hostname). This pattern recurs across GCP questions about dynamic configuration and service discovery.

Practice All PCD Questions

Access 84 questions with complete answers and detailed explanations.

View Full PCD Practice Test →

← Back to PCD Study Guide