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
Terraform Default Values
📖 Scenario: You are setting up a simple Terraform configuration to create cloud resources. You want to use variables with default values so that the configuration works even if no values are provided by the user.
🎯 Goal: Build a Terraform configuration that defines a variable with a default value and uses it to create a resource with that default unless overridden.
📋 What You'll Learn
Define a Terraform variable with a default value
Use the variable in a resource configuration
Ensure the configuration is valid and deployable
💡 Why This Matters
🌍 Real World
Using default values in Terraform variables allows cloud infrastructure to be flexible and reusable without requiring users to always provide input.
💼 Career
Cloud engineers and DevOps professionals often use Terraform variables with defaults to create modular and easy-to-maintain infrastructure code.
Progress0 / 4 steps
1
Define a variable with a default value
Create a Terraform variable called instance_type with the default value t2.micro.
Terraform
Hint
Use the variable block with default to set a default value.
2
Add a provider configuration
Add a Terraform provider block for AWS with the region set to us-east-1.
Terraform
Hint
The provider block configures the cloud provider and region.
3
Create an EC2 instance resource using the variable
Create a resource block called aws_instance named example that uses the variable instance_type for its instance_type attribute and sets ami to ami-0c55b159cbfafe1f0.
Terraform
Hint
Use var.instance_type to reference the variable inside the resource.
4
Add an output to show the instance type
Add an output block called instance_type_output that outputs the value of the variable instance_type.
Terraform
Hint
Use an output block to display values after deployment.
Practice
(1/5)
1. What is the purpose of setting a default value for a variable in Terraform?
easy
A. To make the variable mandatory without a fallback
B. To provide a fallback value if no other value is given
C. To disable the variable completely
D. To automatically generate a random value
Solution
Step 1: Understand variable defaults in Terraform
Default values act as fallback values when no input is provided by the user.
Step 2: Identify the purpose of default values
They make variables optional and provide a safe, predictable value.
Final Answer:
To provide a fallback value if no other value is given -> Option B
Quick Check:
Default value = fallback [OK]
Hint: Default means fallback if no input given [OK]
Common Mistakes:
Thinking default makes variable mandatory
Assuming default disables the variable
Believing default auto-generates random values
2. Which of the following is the correct syntax to set a default value for a variable named region in Terraform?
easy
A. variable "region" { default = "us-east-1" }
B. variable region { default: "us-east-1" }
C. var region = default "us-east-1"
D. variable "region" = "us-east-1"
Solution
Step 1: Recall Terraform variable block syntax
Variables are declared with the keyword variable and a name in quotes.
Step 2: Check correct default assignment
The default value is set inside the block with default = "value".
Final Answer:
variable "region" { default = "us-east-1" } -> Option A
Quick Check:
Correct syntax uses variable block and default = value [OK]
Hint: Use variable "name" { default = "value" } [OK]
Common Mistakes:
Missing quotes around variable name
Using colon instead of equals for default
Assigning default outside variable block
3. Given this variable declaration:
variable "instance_type" {
default = "t2.micro"
}
What will be the value of var.instance_type if no override is provided?
medium
A. null
B. Error: variable not set
C. empty string ""
D. "t2.micro"
Solution
Step 1: Identify the default value in the variable block
The variable instance_type has a default set to "t2.micro".
Step 2: Understand behavior when no override is given
If no value is provided, Terraform uses the default value automatically.
Final Answer:
"t2.micro" -> Option D
Quick Check:
Default used if no override [OK]
Hint: No override? Use default value [OK]
Common Mistakes:
Assuming null if no value given
Expecting error without override
Thinking default is empty string
4. You wrote this variable block:
variable "env" {
default: "production"
}
Why does Terraform give a syntax error?
medium
A. Because default uses colon instead of equals sign
B. Because variable name must not be in quotes
C. Because default value must be a number
D. Because variable blocks cannot have defaults
Solution
Step 1: Check the syntax for default assignment
Terraform requires = to assign default values, not a colon.
Step 2: Identify the cause of syntax error
Using default: "production" causes a syntax error because colon is invalid here.
Final Answer:
Because default uses colon instead of equals sign -> Option A
Quick Check:
Use = not : for default [OK]
Hint: Use = for default, not colon : [OK]
Common Mistakes:
Using colon instead of equals for default
Removing quotes from variable name
Thinking default must be numeric
5. You want to create a variable availability_zones with a default list of zones ["us-east-1a", "us-east-1b"]. Which is the correct way to declare it in Terraform?
hard
A. variable "availability_zones" {
default = "us-east-1a, us-east-1b"
}
B. variable availability_zones = ["us-east-1a", "us-east-1b"]
C. variable "availability_zones" {
type = list(string)
default = ["us-east-1a", "us-east-1b"]
}
D. variable "availability_zones" {
type = string
default = ["us-east-1a", "us-east-1b"]
}
Solution
Step 1: Define variable with correct type for list of strings
Use type = list(string) to specify a list of strings.
Step 2: Assign default as a list of strings
Set default = ["us-east-1a", "us-east-1b"] inside the variable block.
Final Answer:
variable "availability_zones" {
type = list(string)
default = ["us-east-1a", "us-east-1b"]
} -> Option C