π Day 08 β Terraform Meta-Arguments (count, for_each, depends_on, lifecycle)
#30DaysOfAWSTerraform challenge
Iβm a Cloud & DevOps Engineer passionate about building reliable, scalable, and automated cloud infrastructures. I work extensively with AWS, Kubernetes, Terraform, Docker, and CI/CD pipelines to deliver production-ready environments.
My journey started in technical troubleshooting, where I gained strong root-cause analysis and system diagnostic skills. Transitioning into cloud engineering, I have built 3-tier microservices architectures, automated VPCs using Terraform, and containerized legacy applications for performance and portability.
I enjoy solving real-world problems, optimizing cloud cost and performance, and creating automated workflows that reduce manual effort. Iβm continuously learning and applying best practices in DevOps, IaC, and cloud security.
Core Skills: AWS β’ Kubernetes β’ Docker β’ Terraform β’ CI/CD β’ Linux β’ Networking β’ Monitoring β’ Automation β’ Troubleshooting
Looking For: Cloud Engineer | DevOps Engineer | SRE (Junior/Mid-level) roles where I can build, automate, and scale cloud workloads.
Today in the #30DaysOfAWSTerraform challenge, I learned one of the most powerful topics in Terraform β meta-arguments. These special arguments allow Terraform to create multiple resources efficiently, control resource dependencies, and manage how resources behave during creation or deletion.
Day 08 focused mainly on count, for_each, for expressions, and also covered depends_on and lifecycle.
πΉ 1. Understanding COUNT in Terraform
count is used when you want to create multiple identical resources using a list.
Example scenario:
You want to create 2 S3 buckets.
Variable
variable "s3_bucket_names" {
type = list(string)
default = ["day08-bucket-1", "day08-bucket-2"]
}
Resource using count
resource "aws_s3_bucket" "example_count" {
count = length(var.s3_bucket_names)
bucket = var.s3_bucket_names[count.index]
}
β Advantages of count:
Easy to use
Great for simple repeated resources
Good for fixed-size lists
β Limitations:
Removing one item from the list changes indexes
Not safe for production when list order may change
πΉ 2. Understanding FOR_EACH in Terraform
for_each is used when you want stable identifiers β works with maps and sets.
Variable
variable "s3_bucket_set" {
type = set(string)
default = ["day08-bucket-a", "day08-bucket-b"]
}
Resource using for_each
resource "aws_s3_bucket" "example_each" {
for_each = var.s3_bucket_set
bucket = each.value
}
β Advantages of for_each:
Stable addressing
Handles unordered sets/maps
Best for production
Items do NOT shift if you add/remove more values
β When not to use?
- When you specifically need numeric index references
πΉ 3. Output Using FOR Expressions
Terraform allows loops inside outputs.
Print all bucket names
output "bucket_names" {
value = [for name in var.s3_bucket_names : name]
}
Print bucket IDs
output "bucket_ids" {
value = { for k, v in aws_s3_bucket.example_each : k => v.id }
}
This helps inspect resources quickly via:
terraform output
πΉ 4. depends_on Meta-Argument
Terraform usually auto-detects dependencies, but sometimes you must explicitly force order.
Example:
resource "aws_s3_bucket" "primary" {
bucket = "day08-primary"
}
resource "aws_s3_bucket" "dependent" {
bucket = "day08-dependent"
depends_on = [aws_s3_bucket.primary]
}
β Ensures primary is created before dependent
β Helps when Terraform can't automatically detect dependency
πΉ 5. lifecycle Meta-Argument
Controls how Terraform creates/destroys resources.
Example:
lifecycle {
prevent_destroy = false
create_before_destroy = true
ignore_changes = [ tags["CreatedDate"] ]
}
lifecycle can:
β Prevent accidental deletion
β Create new resource before destroying the old one
β Ignore unwanted drift (useful in production)
π§ My Day 08 Takeaways
count= for identical items using list indexesfor_each= best for unique items using sets/mapsfor loops in outputs help in visibility
depends_on ensures proper ordering
lifecycle controls how resources behave
Day 08 improved my understanding of Terraform automation and resource management.
π₯ Day 08 Video ( link )
π End
Excited for Day 09!
#30DaysOfAWSTerraform #AWS #Terraform #DevOps #IaC




