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
Variable declaration syntax in Terraform
📖 Scenario: You are setting up a Terraform configuration to manage cloud infrastructure. Variables help you customize your setup without changing the main code.
🎯 Goal: Learn how to declare variables in Terraform with default values and descriptions.
📋 What You'll Learn
Declare a variable named region with default value us-west-2
Declare a variable named instance_type with default value t2.micro
Add descriptions to both variables
💡 Why This Matters
🌍 Real World
Variables in Terraform let you customize infrastructure deployments without changing the main code, making it easy to reuse and share configurations.
💼 Career
Understanding variable declaration is essential for cloud engineers and DevOps professionals to write flexible and maintainable infrastructure as code.
Progress0 / 4 steps
1
Declare the region variable
Write a Terraform variable declaration for a variable called region with a default value of "us-west-2" and a description "AWS region to deploy resources".
Terraform
Hint
Use the variable block with description and default inside.
2
Declare the instance_type variable
Add a Terraform variable declaration for a variable called instance_type with a default value of "t2.micro" and a description "EC2 instance type" below the region variable.
Terraform
Hint
Follow the same pattern as the region variable.
3
Use the region variable in a provider block
Add a Terraform provider block for AWS that uses the variable region with the syntax region = var.region.
Terraform
Hint
Use provider "aws" { region = var.region } to connect the variable.
4
Use the instance_type variable in an EC2 resource
Add a Terraform resource block for an AWS EC2 instance named example that uses the variable instance_type with the syntax instance_type = var.instance_type and the ami set to "ami-0c55b159cbfafe1f0".
Terraform
Hint
Use resource "aws_instance" "example" { ami = "ami-..." instance_type = var.instance_type }.
Practice
(1/5)
1. What is the purpose of declaring a variable in Terraform?
easy
A. To write comments in the Terraform file
B. To create a new resource in the cloud
C. To define the output of a Terraform plan
D. To store values that can be reused and customized in the configuration
Solution
Step 1: Understand variable role in Terraform
Variables hold values that can be changed without editing the main code, making it flexible.
Step 2: Compare with other options
Creating resources, defining outputs, or comments are different Terraform features, not variable declaration.
Final Answer:
To store values that can be reused and customized in the configuration -> Option D
Quick Check:
Variables = reusable values [OK]
Hint: Variables hold reusable values to customize code easily [OK]
Common Mistakes:
Confusing variables with resources
Thinking variables define outputs
Using variables as comments
2. Which of the following is the correct syntax to declare a string variable named region with a default value of us-west-1 in Terraform?
easy
A. variable region = "us-west-1"
B. var region = "us-west-1"
C. variable "region" { type = string default = "us-west-1" }
D. variable "region" { default = us-west-1 type = string }
Variables use the block format: variable "name" { type = TYPE default = VALUE } with quotes around strings.
Step 2: Check each option
variable "region" { type = string default = "us-west-1" } matches correct syntax with proper quotes and order. Options B and C are invalid syntax. variable "region" { default = us-west-1 type = string } misses quotes around default value.
Final Answer:
variable "region" { type = string default = "us-west-1" } -> Option C
Quick Check:
Correct block syntax with quotes [OK]
Hint: Use variable block with quotes and type before default [OK]
Common Mistakes:
Missing quotes around string values
Using assignment (=) outside variable block
Incorrect order or missing type keyword
3. Given this variable declaration in Terraform:
variable "instance_count" {
type = number
default = 3
}
What will be the value of var.instance_count if no override is provided?
medium
A. 3
B. null
C. 0
D. Error: variable not set
Solution
Step 1: Identify default value in variable block
The variable instance_count has a default value set to 3.
Step 2: Understand behavior when no override is given
If no value is provided during plan or apply, Terraform uses the default value.
Final Answer:
3 -> Option A
Quick Check:
Default value used when no override = 3 [OK]
Hint: Default value applies if no input given [OK]
Common Mistakes:
Assuming default is zero if not set
Expecting error without override
Confusing null with default
4. Identify the error in this Terraform variable declaration:
variable "enable_logging" {
type = bool
default = "true"
}
medium
A. The default value should not be in quotes for bool type
B. The type should be string, not bool
C. The variable name is invalid
D. Missing description field
Solution
Step 1: Check type and default value compatibility
The variable type is bool, so the default must be a boolean value, not a string.
Step 2: Identify the error in default value
The default is "true" (a string), but it should be true (boolean without quotes).
Final Answer:
The default value should not be in quotes for bool type -> Option A
Quick Check:
Boolean default must be unquoted true/false [OK]
Hint: Boolean defaults must be unquoted true or false [OK]
Common Mistakes:
Putting boolean default in quotes
Confusing string and bool types
Ignoring type mismatch errors
5. You want to declare a variable tags in Terraform that holds a map of strings with a default value of {"env" = "prod", "team" = "devops"}. Which is the correct declaration?
hard
A. variable "tags" { type = map(string) default = { env = prod, team = devops } }
B. variable "tags" { type = map(string) default = { "env" = "prod", "team" = "devops" } }
C. variable "tags" { type = map default = { "env" = "prod", "team" = "devops" } }
D. variable "tags" { type = map(string) default = ["env" = "prod", "team" = "devops"] }
Solution
Step 1: Understand map type syntax in Terraform
Maps of strings use type = map(string) and string values must be quoted.
Step 2: Check default value format
Default must be a map with quoted string values inside curly braces, not square brackets.
Step 3: Evaluate options
variable "tags" { type = map(string) default = { "env" = "prod", "team" = "devops" } } correctly uses type = map(string) and default with quoted values. variable "tags" { type = map(string) default = { env = prod, team = devops } } misses quotes on values, variable "tags" { type = map default = { "env" = "prod", "team" = "devops" } } misses string type, variable "tags" { type = map(string) default = ["env" = "prod", "team" = "devops"] } uses square brackets (list) incorrectly.
Final Answer:
variable "tags" { type = map(string) default = { "env" = "prod", "team" = "devops" } } -> Option B
Quick Check:
Map(string) with quoted string values in braces [OK]
Hint: Use map(string) type and quoted string values in braces [OK]