Skip to main content

Command Palette

Search for a command to run...

πŸš€ Day 08 – Terraform Meta-Arguments (count, for_each, depends_on, lifecycle)

#30DaysOfAWSTerraform challenge

Updated
β€’3 min read
N

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 indexes

  • for_each = best for unique items using sets/maps

  • for 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