Bird
Raised Fist0
GCPcloud~10 mins

Variables and outputs in GCP - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Variables and outputs
Define Variables
↓
Use Variables in Config
↓
Deploy Infrastructure
↓
Capture Outputs
↓
View Outputs
↓
Use Outputs Elsewhere
This flow shows how variables are defined, used in deployment, and how outputs are captured and viewed after deployment.
Execution Sample
GCP
variable "region" {
  default = "us-central1"
}

output "instance_zone" {
  value = "${var.region}-a"
}
Defines a variable for region and outputs a computed instance zone based on that variable.
Process Table
StepActionVariable StateOutput ValueNotes
1Define variable 'region' with default 'us-central1'region = 'us-central1'Variable ready for use
2Use variable 'region' to compute output 'instance_zone'region = 'us-central1'instance_zone = 'us-central1-a'Output expression evaluated
3Deploy infrastructure using variable 'region'region = 'us-central1'Deployment uses variable value
4Capture output 'instance_zone' after deploymentregion = 'us-central1'instance_zone = 'us-central1-a'Output available for viewing
5View output 'instance_zone'region = 'us-central1'instance_zone = 'us-central1-a'Output shown to user
6Use output 'instance_zone' in another config or scriptregion = 'us-central1'instance_zone = 'us-central1-a'Output passed as input elsewhere
7End of processregion = 'us-central1'instance_zone = 'us-central1-a'All steps complete
💡 All variables defined and outputs captured successfully after deployment
Status Tracker
VariableStartAfter Step 1After Step 2Final
regionundefinedus-central1us-central1us-central1
instance_zoneundefinedundefinedus-central1-aus-central1-a
Key Moments - 2 Insights
Why does the output 'instance_zone' depend on the variable 'region'?
Because in the execution_table at Step 2, the output value is computed using the variable 'region', showing the dependency clearly.
Can outputs be used before deployment?
No, outputs are only available after deployment as shown in Step 4 and Step 5 where the output is captured and viewed after infrastructure is deployed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 2, what is the value of 'instance_zone'?
Aus-central1-a
Bus-central1
Cundefined
Da-us-central1
💡 Hint
Check the 'Output Value' column at Step 2 in the execution_table
At which step does the deployment use the variable 'region'?
AStep 1
BStep 3
CStep 5
DStep 6
💡 Hint
Look at the 'Action' column for deployment usage in the execution_table
If the variable 'region' changed to 'europe-west1', what would be the new output 'instance_zone'?
Aeurope-west1
Bus-central1-a
Ceurope-west1-a
Da-europe-west1
💡 Hint
Refer to how 'instance_zone' is computed from 'region' in the execution_sample and execution_table
Concept Snapshot
Variables store input values for deployment.
Outputs show results after deployment.
Define variables with defaults or inputs.
Use variables in resource configs.
Outputs can be used elsewhere after deployment.
Full Transcript
This lesson shows how variables and outputs work in cloud infrastructure deployment. First, variables are defined with default values or user inputs. Then, these variables are used in the configuration to customize resources. When the infrastructure is deployed, outputs are computed based on variables or resource attributes. Outputs become available after deployment and can be viewed or used in other configurations. The execution table traces each step from defining variables, using them, deploying, capturing outputs, and using outputs elsewhere. Variables hold values throughout, and outputs depend on those variables. This helps make deployments flexible and reusable.

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

  1. Step 1: Understand the role of variables

    Variables hold values like names or sizes that you want to reuse in your cloud setup.
  2. Step 2: Differentiate variables from outputs

    Outputs show results after deployment, but variables store reusable data before deployment.
  3. Final Answer:

    To store values that can be reused throughout the configuration -> Option D
  4. 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

  1. Step 1: Recall Terraform variable syntax

    Terraform uses the keyword 'variable' followed by the name in quotes and a block with 'default' value.
  2. Step 2: Compare options

    variable "region" { default = "us-central1" } matches Terraform syntax exactly; others use invalid or different language syntax.
  3. Final Answer:

    variable "region" { default = "us-central1" } -> Option B
  4. Quick Check:

    Terraform variable syntax uses 'variable' block [OK]
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

  1. 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.
  2. Step 2: Confirm output command result

    Running 'terraform output instance_ip' returns the value of that output, which is the public IP address.
  3. Final Answer:

    The public IP address of the VM instance -> Option A
  4. 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

  1. Step 1: Check the default value syntax

    Terraform requires string values to be in quotes. Here, us-central1-a is unquoted.
  2. Step 2: Understand Terraform variable rules

    Variable names like 'zone' are valid, default values are allowed, and 'variable' keyword is correct.
  3. Final Answer:

    The default value is missing quotes around the string -> Option A
  4. 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

  1. Step 1: Understand variable interpolation syntax

    Terraform uses ${var.variable_name} inside strings to insert variable values.
  2. 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.
  3. Final Answer:

    output "bucket_url" { value = "https://storage.googleapis.com/${var.bucket_name}" } -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard