Iterator variable in Terraform - Time & Space Complexity
When using an iterator variable in Terraform, we want to know how the number of operations changes as we loop over more items.
We ask: How does the work grow when the list we iterate over gets bigger?
Analyze the time complexity of this Terraform resource creation using an iterator variable.
resource "aws_instance" "example" {
for_each = { for name in var.instance_names : name => name }
ami = "ami-123456"
instance_type = "t2.micro"
tags = {
Name = each.key
}
}
This code creates one AWS instance for each name in the input list.
Look at what repeats as the input list grows.
- Primary operation: Creating an AWS instance resource for each item.
- How many times: Once per item in
var.instance_names.
As the list of instance names grows, the number of AWS instances created grows the same way.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 AWS instance creations |
| 100 | 100 AWS instance creations |
| 1000 | 1000 AWS instance creations |
Pattern observation: The work grows directly with the number of items.
Time Complexity: O(n)
This means if you double the number of items, the work roughly doubles too.
[X] Wrong: "Using an iterator variable runs all resources at once, so time stays the same no matter how many items."
[OK] Correct: Each resource still needs to be created separately, so more items mean more work and more API calls.
Understanding how looping over resources affects deployment time helps you design efficient infrastructure and explain your choices clearly.
"What if we changed from a list to a map for the iterator variable? How would the time complexity change?"
Practice
iterator variable in a Terraform for expression?Solution
Step 1: Understand the iterator variable concept
The iterator variable is a placeholder that takes each value from the list or map one by one during the loop.Step 2: Identify its role in the for expression
It allows Terraform to perform actions on each item individually, making the code reusable and clean.Final Answer:
It represents each item in the list or map during iteration. -> Option AQuick Check:
Iterator variable = item placeholder [OK]
- Confusing iterator with output variable
- Assuming iterator sets iteration count
- Thinking iterator stores final results
item in a Terraform for expression over a list var.names?Solution
Step 1: Recall Terraform for expression syntax
Terraform uses square brackets withfor item in list : expressionto create a new list.Step 2: Match the correct syntax
[for item in var.names : item] matches the correct syntax:[for item in var.names : item].Final Answer:
[for item in var.names : item] -> Option CQuick Check:
Correct for expression syntax = [for item in var.names : item] [OK]
- Omitting square brackets
- Using parentheses instead of brackets
- Missing 'in' keyword
[for num in [1, 2, 3] : num * 2]
What is the resulting list?
Solution
Step 1: Identify the iterator and list
The iterator variable isnum, iterating over the list [1, 2, 3].Step 2: Calculate each iteration's output
Eachnumis multiplied by 2: 1*2=2, 2*2=4, 3*2=6.Final Answer:
[2, 4, 6] -> Option BQuick Check:
Multiply each item by 2 = [2, 4, 6] [OK]
- Confusing multiplication with addition
- Returning original list without changes
- Misreading the iterator variable
[for i var.list : i * 3]
Solution
Step 1: Check for correct for expression syntax
The correct syntax requires the keyword 'in' between the iterator and the list.Step 2: Identify the missing keyword
The expression misses 'in' after 'i', causing a syntax error.Final Answer:
Missing 'in' keyword between iterator and list. -> Option AQuick Check:
For expressions need 'in' keyword [OK]
- Forgetting 'in' keyword
- Using wrong brackets
- Misplacing operators
var.servers = ["web", "db", "cache"] where each key is the server name and the value is the server port starting at 8000 incremented by 1. Which Terraform expression correctly uses an iterator variable to achieve this?Solution
Step 1: Understand the requirement
Create a map where keys are server names and values are ports starting at 8000 incremented by 1.Step 2: Analyze options for correct iterator usage
{ for idx, srv in var.servers : srv => 8000 + idx } usesfor idx, srv in var.serversto get index and server name, then mapssrv => 8000 + idx, correctly incrementing ports.Final Answer:
{ for idx, srv in var.servers : srv => 8000 + idx } -> Option DQuick Check:
Use index and value in for map expression [OK]
- Using list syntax instead of map
- Not using index to increment port
- Swapping key and value positions
Ready to prove it?
Pass the test and this pattern moves unknown
