AWS with Terraform Day (08)
Mastering Terraform Meta-Arguments: depends_on, count & for_each
Today was an eye-opening deep dive into one of the most powerful features in Terraform—Meta-Arguments. If you’ve ever seen Terraform deploy resources in the wrong order or duplicated the same resource block multiple times just to change a small value, you already know the pain.
Meta-arguments solve that. They control how Terraform behaves—creation order, iteration, replication, and resource relationships—without writing external scripts or manually forcing flow.
As a DevOps Engineer with 2 years of experience, today was the day I finally connected the dots between real-world infrastructure complexity and Terraform’s built-in intelligence.
Why Terraform Meta-Arguments Matter
Terraform resource arguments focus on what is being created—like tags, instance type, or CIDR blocks.
But Terraform Meta-Arguments define how resources are created:
| Meta-Argument | Purpose |
|---|---|
depends_on | Controls explicit resource creation order |
count | Creates multiple instances of the same resource using numeric loops |
for_each | Iterates over sets and maps with unique identifiers |
This separation keeps configurations clean and avoids chaos during provisioning.
Meta-Argument 1 — depends_on
Terraform automatically infers dependencies when you reference resource attributes, but in larger real-world configurations, inference sometimes fails.
Example:
You want Bucket-2 only after Bucket-1 completes:
Now bucket1 is always created before bucket2, eliminating random failures.
Real Impact Example
-
VPC before EC2
-
Security Group before rules
-
Database before application deployments
Meta-Argument 2 — count
Perfect for repeating identical resources from a list.
No copy-paste blocks. No code duplication. Just clean iteration.
Limitation
count fails with sets, because sets don’t have indexes. That’s where for_each shines.
Meta-Argument 3 — for_each
For unique iterative resource creation using sets and maps.
Example with a set:
Example with a map:
Perfect when each resource needs its own identity.
Hands-On Practice Today
✔ Implemented multiple S3 buckets using count, for_each, and depends_on
✔ Tested provisioning & destruction order
✔ Saw how for_each handles maps and dynamic tagging
✔ Experienced why count fails with sets
✔ Simplified code from multiple blocks to smart loops
Key Lessons from Day 8
| Topic | Understanding |
|---|---|
depends_on | Explicit control prevents random ordering issues |
count | Best for ordered lists & identical resources |
for_each | Best for sets/maps and unique resource values |
| Clean Code | Fewer blocks + smarter loops = scalable infrastructure |
Diagram:
What’s Next?
Tomorrow (Day 9), I’ll dive into:
Terraform lifecycle rules
—prevent_destroy, ignore_changes, and create_before_destroy
These control how Terraform behaves during updates and safety-sensitive deployments.
Excited to continue!
Final Thoughts
Terraform meta-arguments changed how I think about Infrastructure as Code. Instead of long repetitive files, I now design dynamic, reliable, maintainable configs that scale.
If you’re learning Terraform:
Start small,
experiment,
break things,
fix them.
The plan, apply, destroy cycle teaches far more than reading alone.
See you on Day 9
Happy Terraforming!
Here is my github repo link: https://github.com/Mo-Adnan-Mo-Ayyub/Aws-with-Terraform
Here is the session link:
Comments
Post a Comment