Day 15 – Creating Multiple Resources in Terraform with count and for_each

Tech-driven, cloud-focused, and growth-minded ☁️ Building skills in cloud engineering with a DevOps base. Passionate about learning and solving real problems.
When working with Terraform, you’ll often need to create multiple resources — such as several subnets, EC2 instances, or even IAM users. Instead of copying and pasting the same resource block again and again, Terraform gives us two powerful features:
👉 count – create multiple identical (or almost identical) resources.
👉 for_each – create multiple resources from a map or set, with more control over naming and customization.
Let’s explore both with easy-to-follow examples.
1. Using count
The count meta-argument allows you to create a specific number of resources.
Example: Creating 3 Subnets
resource "aws_subnet" "example" {
count = 3
vpc_id = aws_vpc.main.id
cidr_block = "10.0.${count.index}.0/24"
availability_zone = element(["us-east-1a", "us-east-1b", "us-east-1c"], count.index)
}
✅ Here:
count = 3→ Creates 3 subnets.count.index→ Automatically gives us0,1,2for naming or CIDR assignment.Each subnet goes into a different AZ.
This is perfect when you just need a fixed number of similar resources.
2. Using for_each
The for_each meta-argument allows you to create multiple resources based on a map or set of strings. This gives you names you control, instead of just numbers.
Example: Creating Subnets with Custom Names
variable "subnets" {
default = {
public = "10.0.1.0/24"
private = "10.0.2.0/24"
db = "10.0.3.0/24"
}
}
resource "aws_subnet" "example" {
for_each = var.subnets
vpc_id = aws_vpc.main.id
cidr_block = each.value
availability_zone = "us-east-1a"
tags = {
Name = "${each.key}-subnet"
}
}
✅ Here:
for_eachloops over the map of subnets.each.keygives the subnet type (public,private,db).each.valuegives the subnet CIDR.
This is more flexible than count when your resources aren’t identical.
3. Multiple EC2 Instances with count
Example: Launch 3 identical EC2 Instances
resource "aws_instance" "web" {
count = 3
ami = "ami-12345678"
instance_type = "t2.micro"
tags = {
Name = "web-${count.index}"
}
}
✅ Terraform creates web-0, web-1, web-2.
Great when all instances are the same.
4. Multiple EC2 Instances with Different AMIs (using for_each)
Sometimes, you want different configurations per instance.
variable "instances" {
default = {
ubuntu = "ami-12345678"
amazon = "ami-87654321"
debian = "ami-56781234"
}
}
resource "aws_instance" "servers" {
for_each = var.instances
ami = each.value
instance_type = "t2.micro"
tags = {
Name = "${each.key}-server"
}
}
✅ Terraform creates:
ubuntu-serverwith its AMI.amazon-serverwith its AMI.debian-serverwith its AMI.
This is the cleanest way to manage different resources with custom settings.
5. When to Use count vs for_each
Use
countwhen you want a specific number of similar resources.Use
for_eachwhen you have unique names or values to assign to resources.
Think of it like this:
count= "repeat this block N times".for_each= "loop over this map/set and create resources for each item".
Conclusion
Terraform’s count and for_each meta-arguments make your code more reusable, cleaner, and dynamic.
countis best for identical resources.for_eachis best for resources with different values or names.
Once you master these, you’ll stop writing repetitive Terraform code and start writing scalable infrastructure.
🔗 Follow My Journey
📖 Blogs: Hashnode
💻 Code: GitHub
🐦 Updates: X (Twitter)




