Count vs for_each decision in Terraform - Performance Comparison
When using Terraform, we often create multiple resources using either count or for_each. Understanding how the time to apply changes grows helps us choose the best method.
We want to know how the number of resources affects the number of API calls and provisioning steps.
Analyze the time complexity of creating multiple resources using count.
resource "aws_instance" "example" {
count = var.instance_count
ami = var.ami_id
instance_type = "t2.micro"
}
This code creates a number of AWS instances equal to var.instance_count using count.
Each instance creation triggers similar operations.
- Primary operation: API call to create one AWS instance resource.
- How many times: Exactly once per instance, so
var.instance_counttimes.
As you increase the number of instances, the number of API calls grows directly with it.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 |
| 100 | 100 |
| 1000 | 1000 |
Pattern observation: The number of API calls grows linearly as you add more instances.
Time Complexity: O(n)
This means the time to create resources grows directly in proportion to how many you create.
[X] Wrong: "Using count or for_each changes the total number of API calls needed."
[OK] Correct: Both methods create one resource per item, so the total API calls scale the same way with the number of resources.
Understanding how resource creation scales helps you design infrastructure that grows predictably. This skill shows you can think about costs and performance in real cloud projects.
"What if we used a module that internally creates multiple resources per item? How would that affect the time complexity?"
Practice
Solution
Step 1: Understand the purpose of
countcountcreates multiple copies of a resource by number, perfect for identical resources.Step 2: Compare with
for_eachfor_eachis used when you want unique keys or values to identify each resource, not just copies.Final Answer:
count = 3-> Option AQuick Check:
Simple copies = count [OK]
- Using for_each when only number copies are needed
- Setting count to zero expecting resources
- Confusing sets with counts
for_each with a map in Terraform?Solution
Step 1: Identify valid
for_eachtypesfor_eachaccepts maps or sets, not lists or strings directly.Step 2: Check each option
resource "aws_instance" "example" { for_each = { key1 = "val1", key2 = "val2" } }uses a map with keys and values, which is valid syntax forfor_each.Final Answer:
resource with for_each map -> Option DQuick Check:
for_each needs map or set [OK]
- Using list instead of map/set for for_each
- Confusing count with for_each syntax
- Passing string directly to for_each
resource "aws_s3_bucket" "buckets" {
for_each = { bucket1 = "private", bucket2 = "public-read" }
bucket = each.key
acl = each.value
}What will be the names of the created buckets?
Solution
Step 1: Understand
The keys arefor_eachmap keys and valuesbucket1andbucket2, values are ACL values.Step 2: Check bucket assignment
The bucket name is set toeach.key, so buckets named "bucket1" and "bucket2" are created.Final Answer:
["bucket1", "bucket2"] -> Option CQuick Check:
Bucket names = map keys [OK]
- Using values as bucket names instead of keys
- Assuming syntax error without checking
- Confusing each.key with each.value
resource "aws_instance" "example" {
count = length(var.instances)
name = var.instances[count.index]
}But Terraform throws an error about
count.index. What is the likely cause?Solution
Step 1: Identify variable type mismatch
var.instancesis likely a map, butcountworks with numbers and lists indexed by numbers.Step 2: Choose correct iterator
For maps,for_eachis the correct choice to iterate over keys and values.Final Answer:
Use for_each for maps, not count -> Option AQuick Check:
Maps need for_each, not count [OK]
- Using count.index with maps
- Assuming count must be fixed number
- Ignoring variable data type
users = {
alice = "admin",
bob = "read-only",
carol = "power-user"
}Which Terraform resource block correctly uses
count or for_each to create these users with unique names?Solution
Step 1: Analyze data structure
The users variable is a map with usernames as keys and policies as values.Step 2: Match iteration method
for_eachworks well with maps to create uniquely identified resources using keys and values.Step 3: Check each option
resource "aws_iam_user" "user" { for_each = var.users name = each.key policy = each.value }correctly usesfor_each = var.usersand assignsname = each.keyandpolicy = each.value.Final Answer:
Use for_each with map for unique users -> Option BQuick Check:
Unique keys need for_each [OK]
- Using count with maps incorrectly
- Mixing keys and values in wrong order
- Using toset on maps causing errors
Ready to prove it?
Pass the test and this pattern moves unknown
