What is the difference between count and for_each in Terraform?
Compare count and for_each in Terraform: indexing, the index-shift problem, each.key and each.value, conditional resources, examples and interview questions.
Expected Interview Answer
count creates multiple instances of a resource indexed by number (0, 1, 2...), while for_each creates instances keyed by the members of a map or set — giving each instance a stable, meaningful key instead of a positional index.
Both are meta-arguments for creating many similar resources from one block. count is best when instances are truly identical and the number is all that matters, but because instances are tracked by numeric index, removing an item in the middle shifts every later index and can force needless re-creation. for_each avoids this by tracking each instance under a string key from a map or set, so adding or removing one member only affects that member. As a rule of thumb: use for_each when items have identities, count when you just need N copies or a simple on/off toggle.
- count: simplest way to make N identical instances
- count: easy conditional create with a 0-or-1 pattern
- for_each: stable keys prevent index-shift churn
- for_each: iterate over maps and sets with meaningful names
- for_each: safer edits when the collection changes
AI Mentor Explanation
count is like numbering identical practice cones one to eleven — remove cone number five and every cone after it shifts down a number. for_each is like assigning fielding positions by name such as slip, gully, or mid-off; drop gully and every other position keeps its own label, so no one else has to move just because one role disappeared.
Step-by-Step Explanation
Step 1
Decide identity
Ask whether each instance has a meaningful name/key or is just one of N identical copies.
Step 2
Use count for copies
Set count = N to create numbered instances referenced as name[0], name[1], and so on.
Step 3
Use for_each for keyed sets
Pass a map or set to for_each; each instance is keyed by each.key with data in each.value.
Step 4
Reference correctly
count instances use integer indexes; for_each instances use their string keys.
Step 5
Anticipate change
Prefer for_each when the collection may grow or shrink, to avoid index-shift re-creation.
What Interviewer Expects
- count is indexed by number, for_each by map/set keys
- The index-shift problem that count causes on removal
- Knowing each.key and each.value with for_each
- The 0-or-1 conditional pattern with count
- A clear rule for choosing between them
Common Mistakes
- Using count for named resources and suffering re-creation on edits
- Passing a list (not a set) to for_each and hitting type errors
- Confusing each.key with each.value
- Assuming count and for_each can be used together in one block
- Not knowing count can take a conditional 0-or-1 expression
Best Answer (HR Friendly)
“Both let you create many copies of a resource from one block. count numbers them like a plain list, while for_each names each one with its own label, which makes editing the group later much safer.”
Code Example
resource "aws_instance" "worker" {
count = 3
ami = "ami-123456"
instance_type = "t3.micro"
tags = {
Name = "worker-${count.index}"
}
}
# referenced as aws_instance.worker[0], [1], [2]resource "aws_iam_user" "team" {
for_each = {
alice = "admin"
bob = "developer"
}
name = each.key
tags = {
Role = each.value
}
}
# referenced as aws_iam_user.team["alice"]Follow-up Questions
- Why does removing a middle item with count cause re-creation?
- What types can for_each accept?
- What are each.key and each.value?
- How do you conditionally create a resource with count?
- Can you convert a count resource to for_each without destroying it?
MCQ Practice
1. How does for_each identify each instance?
for_each keys each instance by the map key or set value, giving it a stable identity.
2. What problem does count have when you remove a middle element?
count tracks by numeric index, so removing an item shifts every later index and can re-create resources.
3. Which meta-argument best fits creating one resource conditionally?
The count = condition ? 1 : 0 pattern creates the resource only when the condition is true.
Flash Cards
How does count index instances? — By integer position: name[0], name[1], name[2]...
How does for_each index instances? — By string key from a map or set: name["key"], with each.key and each.value.
Why prefer for_each for named resources? — Stable keys avoid index-shift re-creation when the collection changes.
How do you conditionally create one resource? — count = condition ? 1 : 0 creates it only when the condition is true.
Continue Learning
Related Interview Questions
What is the difference between a root module and a child module in Terraform?
easy
How does Terraform handle dependencies between resources?
medium
What is Terraform and what problem does infrastructure as code solve?
easy
What is the difference between Terraform and configuration tools like Ansible?
medium