For‑Each & Count Loops
Imagine you’re expanding your Lego city. Sometimes you want to build a fixed number of identical houses (like 3 small cottages). Other times, you want to build a set of unique houses, each with its own name and design.
Terraform gives you two looping tools for this: count (repeat something a fixed number of times) and for_each (repeat something for each item in a list or map). Together, they make your infrastructure modular, scalable, and dynamic.
Key Concepts
1. Count Loops
- Definition: Creates multiple instances of a resource based on a number.
- Result: Creates 3 EC2 instances.
- Accessing:
aws_instance.web[0],aws_instance.web[1],aws_instance.web[2]. - Use case: Fixed repetition (e.g., 3 identical servers).
resource "aws_instance" "web" {
count = 3
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
}
2. For‑Each Loops
- Definition: Creates multiple instances of a resource based on items in a list or map.
- Result: Creates 3 EC2 instances, each tagged with
dev,staging,prod. - Accessing:
aws_instance.web["dev"],aws_instance.web["staging"],aws_instance.web["prod"]. - Use case: Unique resources with identifiers.
resource "aws_instance" "web" {
for_each = toset(["dev", "staging", "prod"])
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = each.key
}
}
3. Comparison Table
| Feature | Count Loop | For‑Each Loop |
|---|---|---|
| Basis | Number | List/Map items |
| Access method | Index ([0]) | Key (["dev"]) |
| Best for | Fixed repetition | Unique identifiers |
| Flexibility | Less flexible | More flexible |
| Readability | Simple | Clearer with named keys |
Hands‑On Lab / Demo
Lab: Deploying Servers with Count vs For‑Each
variable "environments" {
default = ["dev", "staging", "prod"]
}
resource "aws_instance" "foreach_example" {
for_each = toset(var.environments)
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = each.key
}
}
→ Creates dev, staging, and prod servers.
resource "aws_instance" "count_example" {
count = 2
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t2.micro"
tags = {
Name = "server-${count.index}"
}
}
→ Creates server-0 and server-1.
Pro Tips & Best Practices
- Use
countfor simple repetition with no unique identifiers. - Use
for_eachwhen resources need distinct names or attributes. - Avoid mixing
countandfor_eachin the same resource as it gets confusing. - Always prefer readability - choose the loop that makes code clearer.
- Document loop usage in module README for maintainability.
Summary & Cheatsheet
- Count: Repeat resources by number →
count.index. - For‑Each: Repeat resources by list/map →
each.key,each.value. - Best practice: Use
countfor fixed repetition,for_eachfor unique identifiers.
Quick mnemonic: Count = Numbers, For‑Each = Names
The Hackers Notebook
Loops are Terraform’s scaling tools. count lets you repeat resources by number, while for_each lets you create resources for each item in a list or map. Together, they make your infrastructure flexible, scalable, and easier to manage.
