GOOGLE-PROFESSIONAL-CLOUD-DEVELOPER practice question 34 of 35

You want to create “fully baked” or “golden” Compute Engine images for your application. You need to bootstrap your application to connect to the appropriate database according to the environment the application is running on (test, staging, production). What should you do?

Full set: 265 questions
Answer options
A: Embed the appropriate database connection string in the image. Create a different image for each environment.
B: When creating the Compute Engine instance, add a tag with the name of the database to be connected. In your application, query the Compute Engine API to pull the tags for the current instance, and use the tag to construct the appropriate database connection string.
C: When creating the Compute Engine instance, create a metadata item with a key of “DATABASE” and a value for the appropriate database connection string. In your application, read the “DATABASE” environment variable, and use the value to connect to the appropriate database.
D: When creating the Compute Engine instance, create a metadata item with a key of “DATABASE” and a value for the appropriate database connection string. In your application, query the metadata server for the “DATABASE” value, and use the value to connect to the appropriate database. Correct answer
Correct Answer:
D. When creating the Compute Engine instance, create a metadata item with a key of “DATABASE” and a value for the appropriate database connection string. In your application, query the metadata server for the “DATABASE” value, and use the value to connect to the appropriate database.
Explanation

This approach correctly utilizes instance metadata, which is the standard Google Cloud mechanism for passing configuration data to a virtual machine instance at runtime. By storing the database connection string as a custom metadata value, a single "golden" image can be used across all environments (test, staging, production). The application within the instance can then query the internal metadata server—a highly available local service—to retrieve the appropriate connection string for its specific environment. This decouples the application artifact from its runtime configuration, which is a fundamental best practice for creating scalable and manageable systems.

Why Incorrect

A. This approach is inefficient and contradicts the "golden image" principle. It creates image sprawl, making updates and management complex and error-prone.
B. Tags are primarily intended for network firewall rules and resource organization, not for passing application configuration or sensitive data like connection strings.
C. Setting instance metadata does not automatically create a corresponding environment variable within the instance. A startup script would be needed to read the metadata and export it as an environment variable.

References

1. Google Cloud Documentation, Compute Engine, "Storing and retrieving instance metadata": This document explicitly states, "Custom metadata is useful for passing in arbitrary values to your instance that can be used by your startup scripts and other code running on your instance." It provides examples of querying the metadata server from within an instance using curl "http://metadata.google.internal/computeMetadata/v1/instance/attributes/DATABASE" -H "Metadata-Flavor: Google". This directly supports the method described in option D.

2. Google Cloud Documentation, VPC, "Tags overview": This page clarifies the purpose of tags: "Tags are strings that you can add to your Compute Engine virtual machine (VM) instances. Tags let you make firewall rules and routes applicable to specific VM instances." This confirms that using tags for application configuration, as suggested in option B, is not their intended use.

3. Google Cloud Architecture Center, "Building immutable infrastructure with Packer and Spinnaker on Compute Engine": This article describes the best practice of creating immutable or "golden" images. It emphasizes separating the application artifact from its configuration, which is injected at launch time. This principle invalidates option A and supports the dynamic configuration approach of option D.

4. Google Cloud Documentation, Compute Engine, "Running startup scripts": This documentation shows that to use metadata values as environment variables, a script must first query the metadata server and then export the value. This confirms that the direct link assumed in option C does not exist automatically. For example, a script would need a line like DBSTRING=$(curl ...) followed by export DBSTRING.

Scroll to Top