DEV Community

Cover image for Day 8: Terraform Finally Makes Sense (Loops, Count, For_Each & Dependencies)
Zakariyau Mukhtar
Zakariyau Mukhtar

Posted on

Day 8: Terraform Finally Makes Sense (Loops, Count, For_Each & Dependencies)

Today was one of those days where everything just clicked.
No stress, no confusion… just straight understanding. Terraform started feeling less like “DevOps theory” and more like something I can actually control confidently.

What I Learned

  • I finally understood meta-arguments especially count, for_each, and depends_on.
  • Learned how to use count.index to loop through lists.
  • Learned how for_each works with sets and how it gives better resource naming.
  • Saw why dependencies matter so Terraform doesn’t rush resources in the wrong order.

Honestly, these concepts looked scary from afar, but once I practiced them… they were simple.

What I Built Today

Created two S3 buckets using count

Used a list of names, looped with count.index, and everything created perfectly.

Created two more S3 buckets using for_each

Used a set this time and referenced bucket names using each.value.
Added depends_on just to make sure Terraform behaves.

Displayed bucket names and IDs

Used for-expressions to print out everything cleanly with tf output.

Commands I ran

tf plan
tf apply --auto-approve
tf output

Everything worked without drama.

A Bit of My Code:

resource aws_s3_bucket "bucket5" { 
  count = 2
  bucket = var.bucket_name[count.index]
  tags = var.tags
}
Enter fullscreen mode Exit fullscreen mode
output "s3_bucket_names" {
  value = [for bucket in aws_s3_bucket.bucket1 : bucket.bucket]
}
Enter fullscreen mode Exit fullscreen mode

Today was smooth.
The tasks didn’t overwhelm me at all.
It felt like the previous lessons were preparing me for today, and everything connected naturally.

The more I practice Terraform, the more I feel like:

“Okay… I can actually do this DevOps thing.”

DAY 8 :

Key Lessons from Day 8

  • count = great for lists
  • for_each = cleaner when you want unique names
  • Sets work nicely with for_each
  • depends_on prevents Terraform from rushing
  • For-expressions make outputs neat and professional

Top comments (0)