What if you could create dozens of cloud resources by writing just one block of code?
Count vs for_each decision in Terraform - When to Use Which
Imagine you need to create multiple cloud resources, like servers or storage buckets, by writing each one separately in your configuration.
You copy and paste blocks of code, changing only names or IDs manually.
This manual way is slow and boring.
It's easy to make mistakes like forgetting to update a name or creating duplicates.
When you want to add or remove resources, you must edit many places, which wastes time and causes errors.
Using count or for_each lets you write one block of code that automatically creates many resources.
This saves time, reduces mistakes, and makes your setup easy to change.
resource "aws_instance" "server1" { ... } resource "aws_instance" "server2" { ... }
resource "aws_instance" "servers" { count = 2 ... }
You can quickly and safely manage many resources with simple, clear code that adapts as your needs grow.
A company needs 10 identical virtual machines for a project.
Instead of writing 10 blocks, they use count = 10 to create all at once.
Later, they add 5 more by changing one number.
Manual resource creation is slow and error-prone.
count and for_each automate resource creation.
This makes infrastructure easy to scale and maintain.
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
