0
0
Terraformcloud~10 mins

Iterator variable in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Iterator variable
Start with collection
Pick next item as iterator variable
Use iterator variable in block
More items?
YesRepeat pick next item
No
End iteration
Terraform loops over each item in a collection, assigning it to an iterator variable to use inside the loop block.
Execution Sample
Terraform
locals {
  names = ["app1", "app2", "app3"]
}

resource "null_resource" "example" {
  for_each = toset(local.names)
  triggers = {
    name = each.key
  }
}
This code loops over a list of names, using each name as an iterator variable to create resources.
Process Table
StepIterator Variable (each.key)ActionResource Created
1"app1"Assign first item to iterator variablenull_resource.example["app1"]
2"app2"Assign second item to iterator variablenull_resource.example["app2"]
3"app3"Assign third item to iterator variablenull_resource.example["app3"]
4N/ANo more items, iteration endsNo new resource
💡 All items in the set processed, iteration stops.
Status Tracker
VariableStartAfter 1After 2After 3Final
each.keyN/A"app1""app2""app3"N/A
Key Moments - 2 Insights
Why does the iterator variable change each step?
Because Terraform assigns each item from the collection to the iterator variable in order, as shown in execution_table steps 1 to 3.
What happens when all items are processed?
Iteration stops, no more resources are created, as shown in execution_table step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the iterator variable value at step 2?
A"app2"
B"app1"
C"app3"
DN/A
💡 Hint
Check the 'Iterator Variable (each.key)' column at step 2 in the execution_table.
At which step does the iteration end?
AStep 3
BStep 2
CStep 4
DStep 1
💡 Hint
Look for the step where 'No more items, iteration ends' is noted in the Action column.
If the list had 4 names instead of 3, how would the execution table change?
AThere would still be 4 steps, no change.
BThere would be 5 steps, with the 4th step assigning the new name.
CThere would be 3 steps only.
DThe iteration would never end.
💡 Hint
Refer to the pattern in execution_table rows for each item processed.
Concept Snapshot
Terraform iterator variable:
- Loops over each item in a collection
- Assigns current item to iterator variable (e.g., each.key)
- Use iterator variable inside resource or block
- Iteration ends when all items processed
- Enables dynamic resource creation
Full Transcript
In Terraform, an iterator variable is used to loop over a collection like a list or set. Each item in the collection is assigned to the iterator variable one by one. This variable can then be used inside resource blocks to create multiple resources dynamically. The process continues until all items are processed, then the iteration stops. For example, looping over a list of names assigns each name to the iterator variable 'each.key' in turn, creating a resource for each name.