📖 Scenario: You are setting up multiple virtual machines (instances) in the cloud. Each instance has a unique name and a specific size. You want to create these instances efficiently using Terraform.
🎯 Goal: Create a Terraform configuration that uses for_each to deploy multiple instances from a map of instance names and sizes.
📋 What You'll Learn
Define a map variable with instance names as keys and sizes as values
Use for_each in the resource block to create instances from the map
Assign the instance name and size from the map values
Output the instance IDs after creation
💡 Why This Matters
🌍 Real World
Cloud engineers often need to deploy multiple similar resources with different configurations. Using for_each with maps helps manage these resources efficiently.
💼 Career
Understanding for_each with maps in Terraform is essential for infrastructure as code roles, enabling scalable and maintainable cloud deployments.
Progress0 / 4 steps
1
Define the instances map variable
Create a Terraform variable called instances of type map(string) with these exact entries: "web1" = "small", "web2" = "medium", "db1" = "large".
Terraform
Hint
Use variable block with type = map(string) and set default to the given map.
2
Add a local variable for instance configuration
Create a local variable called instance_config that references the var.instances variable.
Terraform
Hint
Use a locals block to assign instance_config to var.instances.
3
Create instances using for_each
Create a resource block resource "aws_instance" "example" that uses for_each = local.instance_config. Set the instance_type to each.value and the tags with a Name key set to each.key.
Terraform
Hint
Use for_each with local.instance_config. Set instance_type to each.value and tag the instance with Name = each.key.
4
Output the instance IDs
Create an output called instance_ids that outputs a map of instance names to their IDs using aws_instance.example and for_each.
Terraform
Hint
Use an output block with a for expression to map instance names to their IDs.
Practice
(1/5)
1. What does for_each do when used with a map in Terraform?
easy
A. Creates resources only for the first key in the map
B. Deletes all resources in the map
C. Combines all map entries into a single resource
D. Creates one resource for each key-value pair in the map
Solution
Step 1: Understand the role of for_each with maps
for_each iterates over each key-value pair in a map to create multiple resources.
Step 2: Recognize resource creation behavior
Each key-value pair results in one resource instance, using each.key and each.value inside the resource block.
Final Answer:
Creates one resource for each key-value pair in the map -> Option D
Quick Check:
for_each with map = one resource per pair [OK]
Hint: Remember: for_each maps keys to separate resources [OK]
Common Mistakes:
Thinking for_each merges all map entries into one resource
Assuming for_each deletes resources
Believing for_each only uses the first map key
2. Which of the following is the correct syntax to use for_each with a map in a Terraform resource?
You want to create AWS instances only for servers with valid IPs using for_each. Which expression correctly filters the map to exclude empty or null IPs?
hard
A. for_each = var.servers
B. for_each = { for k, v in var.servers : k => v if v != "" && v != null }
C. for_each = { for k, v in var.servers : k => v if v == "" || v == null }
D. for_each = [ for v in var.servers : v if v != "" ]
Solution
Step 1: Understand filtering with for expressions
Use a for expression with an if condition to filter out empty strings and null values from the map.
Step 2: Check each option
for_each = { for k, v in var.servers : k => v if v != "" && v != null } correctly filters keys where value is neither empty string nor null, producing a valid filtered map for for_each.
Final Answer:
for_each = { for k, v in var.servers : k => v if v != "" && v != null } -> Option B
Quick Check:
Filter map with if condition to exclude empty/null [OK]
Hint: Filter map with for and if to exclude empty/null [OK]
Common Mistakes:
Not filtering out empty or null values
Using list comprehension instead of map
Filtering for empty/null instead of excluding them
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.