Using Variables and Outputs in GCP Deployment Manager
📖 Scenario: You are setting up a simple Google Cloud Platform (GCP) deployment using Deployment Manager. You want to create a virtual machine instance with a specific name and zone, and then output the instance's selfLink URL after deployment.
🎯 Goal: Build a Deployment Manager configuration that uses variables to define the VM instance name and zone, and outputs the instance's selfLink URL.
📋 What You'll Learn
Create a variable called instanceName with the value my-vm-instance.
Create a variable called zone with the value us-central1-a.
Define a resource of type compute.v1.instance using the variables for name and zone.
Add an output called instanceSelfLink that outputs the selfLink of the created instance.
💡 Why This Matters
🌍 Real World
Cloud engineers often use Deployment Manager to automate creating and managing cloud resources with reusable templates.
💼 Career
Knowing how to use variables and outputs in cloud templates is essential for infrastructure as code roles and cloud automation tasks.
Progress0 / 4 steps
1
Create variables for instance name and zone
Create a variable called instanceName and set it to my-vm-instance. Also create a variable called zone and set it to us-central1-a.
GCP
Hint
Use YAML syntax to define variables with exact names and values.
2
Add a resource using the variables
Add a resource named vm-instance of type compute.v1.instance. Use the variables instanceName for the resource name and zone for the resource zone property.
GCP
Hint
Use $(variableName) syntax to reference variables inside properties.
3
Add an output for the instance selfLink
Add an output called instanceSelfLink that outputs the selfLink of the resource named vm-instance.
GCP
Hint
Outputs use $(ref.resourceName.property) syntax to get resource properties.
4
Complete the deployment configuration
Ensure the deployment configuration includes the variables instanceName and zone, the resource vm-instance using those variables, and the output instanceSelfLink with the correct reference.
GCP
Hint
Check that all parts are present and correctly linked.
Practice
(1/5)
1. What is the main purpose of variables in a Google Cloud deployment configuration?
easy
A. To monitor resource usage in real-time
B. To display the final results after deployment
C. To delete resources automatically
D. To store values that can be reused throughout the configuration
Solution
Step 1: Understand the role of variables
Variables hold values like names or sizes that you want to reuse in your cloud setup.
Step 2: Differentiate variables from outputs
Outputs show results after deployment, but variables store reusable data before deployment.
Final Answer:
To store values that can be reused throughout the configuration -> Option D
Quick Check:
Variables store reusable values [OK]
Hint: Variables hold reusable data; outputs show results after deployment [OK]
Common Mistakes:
Confusing variables with outputs
Thinking variables delete resources
Assuming variables monitor usage
2. Which of the following is the correct syntax to declare a variable named region with default value us-central1 in a Terraform configuration for GCP?
easy
A. var region = "us-central1"
B. variable "region" { default = "us-central1" }
C. set variable region = "us-central1"
D. region := "us-central1"
Solution
Step 1: Recall Terraform variable syntax
Terraform uses the keyword 'variable' followed by the name in quotes and a block with 'default' value.
Step 2: Compare options
variable "region" { default = "us-central1" } matches Terraform syntax exactly; others use invalid or different language syntax.
Final Answer:
variable "region" { default = "us-central1" } -> Option B
Hint: Terraform variables use 'variable' block with default inside braces [OK]
Common Mistakes:
Using assignment syntax from other languages
Missing quotes around variable name
Using := which is not Terraform syntax
3. Given this Terraform output block:
output "instance_ip" {
value = google_compute_instance.vm_instance.network_interface[0].access_config[0].nat_ip
}
What will terraform output instance_ip show after deployment?
medium
A. The public IP address of the VM instance
B. The internal IP address of the VM instance
C. The VM instance name
D. An error because output syntax is wrong
Solution
Step 1: Understand the output value path
The path accesses the VM's network interface, then the first access config's NAT IP, which is the public IP.
Step 2: Confirm output command result
Running 'terraform output instance_ip' returns the value of that output, which is the public IP address.
Final Answer:
The public IP address of the VM instance -> Option A
Quick Check:
Output shows public IP from access_config NAT IP [OK]
Hint: Access NAT IP in output shows public IP address [OK]
Common Mistakes:
Confusing NAT IP with internal IP
Assuming output shows instance name
Thinking output block syntax is invalid
4. You wrote this Terraform variable block:
variable "zone" {
default = us-central1-a
}
Why does Terraform fail to apply this configuration?
medium
A. The default value is missing quotes around the string
B. Variable name cannot be 'zone'
C. Default values are not allowed in variables
D. The block must use 'var' instead of 'variable'
Solution
Step 1: Check the default value syntax
Terraform requires string values to be in quotes. Here, us-central1-a is unquoted.
Step 2: Understand Terraform variable rules
Variable names like 'zone' are valid, default values are allowed, and 'variable' keyword is correct.
Final Answer:
The default value is missing quotes around the string -> Option A
Quick Check:
Strings need quotes in Terraform variables [OK]
Hint: Always quote string default values in Terraform variables [OK]
Common Mistakes:
Forgetting quotes around string values
Thinking variable names are restricted
Using 'var' instead of 'variable' keyword
5. You want to create a Terraform output that shows the URL of a deployed Cloud Storage bucket using a variable bucket_name. Which output block correctly uses the variable to build the URL?
hard
A. output "bucket_url" { value = "https://storage.googleapis.com/var.bucket_name" }
B. output "bucket_url" { value = https://storage.googleapis.com/${bucket_name} }
C. output "bucket_url" { value = "https://storage.googleapis.com/${var.bucket_name}" }
D. output "bucket_url" { value = "https://storage.googleapis.com/bucket_name" }
Solution
Step 1: Understand variable interpolation syntax
Terraform uses ${var.variable_name} inside strings to insert variable values.
Step 2: Check each option for correct syntax
output "bucket_url" { value = "https://storage.googleapis.com/${var.bucket_name}" } correctly uses "${var.bucket_name}" inside quotes. Others either miss quotes or use wrong syntax.
Final Answer:
output "bucket_url" { value = "https://storage.googleapis.com/${var.bucket_name}" } -> Option C
Quick Check:
Use ${var.name} for variable interpolation in outputs [OK]
Hint: Use ${var.name} inside quotes for variable interpolation [OK]
Common Mistakes:
Missing quotes around the URL string
Not using 'var.' prefix for variables
Using variable name without interpolation syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.