π Terraform Series β Day 9

Meta Arguments: count vs for_each with Output
π Abstract
In Terraform, managing multiple resources efficiently is a common requirement in real-world DevOps projects. While creating multiple instances may seem straightforward, choosing the right approach can significantly impact scalability, flexibility, and maintainability.
This blog explores Terraform meta arguments, specifically count and for_each, and demonstrates how they behave differently when creating multiple resources. It also covers how outputs change based on these approaches and when to use depends_on for explicit dependencies.
π― Objectives
After completing this blog, you will be able to:
Understand what meta arguments are in Terraform
Create multiple resources using
countIdentify limitations of
countUse
for_eachfor better flexibilityHandle outputs correctly for both approaches
Understand the use of
depends_on
π· Step 1: Understanding Meta Arguments
Meta arguments are special Terraform keywords that control resource behavior.
Common Meta Arguments:
countfor_eachdepends_onlifecycle
π In this tutorial, we focus on:
countfor_each
π· Step 2: Creating Multiple Resources using count
β Code
resource "aws_instance" "my_instance" {
count = 2
ami = var.ec2_ami_id
instance_type = var.ec2_instance_type
}
π Explanation
Terraform creates 2 EC2 instances
Indexed as:
aws_instance.my_instance[0]
aws_instance.my_instance[1]
π· Step 3: Output using count
output "ec2_public_ip" {
value = aws_instance.my_instance[*].public_ip
}
π§ Concept
[*]β Splat ExpressionUsed to extract values from multiple resources
π· Step 4: Limitation of count
Using count has restrictions:
Same configuration for all instances
Cannot assign:
Different names β
Different instance types β
π· Step 6: Using for_each (Better Approach)
resource "aws_instance" "my_instance" {
for_each = {
Apurv-1 = "t2.micro"
Apurv-2 = "t2.medium"
}
ami = var.ec2_ami_id
instance_type = each.value
tags = {
Name = each.key
}
}
π§ Explanation
each.keyβ Instance Nameeach.valueβ Instance Type
π Each instance becomes unique and configurable
π· apply terraform and check output
terraform apply
π· Step 7: Why Old Output Fails
β Old Code
aws_instance.my_instance[*].public_ip
β Reason
countβ returns listfor_eachβ returns map
π So [*] does not work here
π· Step 8: Correct Output with for_each
β Public IP
output "ec2_public_ip" {
value = [
for instance in aws_instance.my_instance :
instance.public_ip
]
}
β Public DNS
output "ec2_public_dns" {
value = [
for instance in aws_instance.my_instance :
instance.public_dns
]
}
β Private IP
output "ec2_private_ip" {
value = [
for instance in aws_instance.my_instance :
instance.private_ip
]
}
π· Step 9: Final Result
After applying for_each:
Different instance names β
Different instance types β
Correct outputs fetched β
π· Step 10: Understanding depends_on
β Definition
depends_on is used to explicitly define dependencies
βοΈ Example
depends_on = [
aws_security_group.my_groups,
aws_key_pair.my_key
]
π§ When to Use
When Terraform cannot detect dependency automatically
When using hardcoded values
When dependencies are indirect
β When NOT to Use
key_name = aws_key_pair.my_key.key_name
π Terraform automatically handles this (implicit dependency)
π Conclusion
Use
countβ for simple, identical resourcesUse
for_eachβ for flexible and scalable setupsUnderstand difference between list vs map outputs
Use
depends_ononly when required
π¨βπ» About the Author
βA complete Terraform series covering everything from fundamentals to advanced real-world infrastructure automation in a DevOps environment.β
π¬ Let's Stay Connected
π§ Email: gujjarapurv181@gmail.com
π GitHub: github.com/ApurvGujjar07
πΌ LinkedIn: linkedin.com/in/apurv-gujjar





