Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why variables make configurations reusable
📖 Scenario: You are setting up a cloud server configuration using Terraform. You want to make your setup easy to change for different projects without rewriting the whole code.
🎯 Goal: Build a Terraform configuration that uses variables to make the server setup reusable for different environments.
📋 What You'll Learn
Create a variable for the server name
Create a variable for the server instance type
Use the variables in the resource configuration
Set default values for the variables
💡 Why This Matters
🌍 Real World
Cloud engineers use variables in Terraform to create flexible infrastructure code that works for many projects.
💼 Career
Knowing how to use variables is essential for writing reusable and maintainable infrastructure as code.
Progress0 / 4 steps
1
Create variables for server name and instance type
Create two variables in Terraform: server_name with default value my-server and instance_type with default value t2.micro.
Terraform
Hint
Use variable blocks with default values for both variables.
2
Add a resource using the variables
Add an aws_instance resource named example that uses the variables server_name for tags.Name and instance_type for instance_type.
Terraform
Hint
Use var.instance_type and var.server_name inside the resource block.
3
Add a variable for the AMI ID
Create a variable called ami_id with default value ami-0c55b159cbfafe1f0 and update the aws_instance resource to use var.ami_id for the ami attribute.
Terraform
Hint
Define the ami_id variable and replace the hardcoded AMI in the resource with var.ami_id.
4
Add an output for the server ID
Add an output called server_id that outputs the id attribute of the aws_instance.example resource.
Terraform
Hint
Use an output block with the value set to aws_instance.example.id.
Practice
(1/5)
1. Why do variables make Terraform configurations reusable?
easy
A. Because they allow changing values without modifying the main code
B. Because they automatically create resources
C. Because they prevent any errors in the code
D. Because they remove the need for providers
Solution
Step 1: Understand the role of variables
Variables hold values that can be changed without editing the main configuration code.
Step 2: Recognize how this affects reusability
Changing variable values lets you reuse the same code for different setups or environments easily.
Final Answer:
Because they allow changing values without modifying the main code -> Option A
Quick Check:
Variables = flexible values [OK]
Hint: Variables let you change inputs, not code [OK]
Common Mistakes:
Thinking variables create resources automatically
Believing variables fix all code errors
Assuming variables replace providers
2. Which of the following is the correct way to declare a variable in Terraform?
easy
A. var region = "us-west-1"
B. variable "region" { default = "us-west-1" }
C. set variable region = "us-west-1"
D. define region: "us-west-1"
Solution
Step 1: Recall Terraform variable syntax
Terraform variables are declared using the keyword 'variable' followed by the name in quotes and a block with settings.