DEV Community

Cover image for πŸš€ Terraform Day 8: Meta Arguments β€” Controlling Dependencies & Resource Replication
Jeeva
Jeeva

Posted on

πŸš€ Terraform Day 8: Meta Arguments β€” Controlling Dependencies & Resource Replication

Day 8 of the 30 Days of AWS Terraform series introduces Terraform Meta Arguments β€” special built-in arguments that change how resources behave rather than configuring the cloud provider itself.

Meta arguments allow Terraform to:
Control resource creation order
Create multiple resources dynamically
Handle lists, sets, and maps correctly
Scale infrastructure without repetitive code

This lesson focused on three core meta arguments:
πŸ‘‰ depends_on
πŸ‘‰ count
πŸ‘‰ for_each

These are fundamental for writing real-world, scalable Terraform configurations.

🎯 What Are Terraform Meta Arguments?

Meta arguments are Terraform-native arguments that can be used inside resource blocks to control behavior such as:
Dependencies
Iteration
Lifecycle behavior (covered later)

They are not cloud-provider specific and work the same across AWS, Azure, GCP, etc.

πŸ”Ή depends_on β€” Explicit Resource Dependencies

Terraform usually figures out resource order automatically through references.
Example:
subnet_id = aws_subnet.main.id

This creates an implicit dependency.

When is depends_on needed?
When Terraform cannot infer the dependency automatically.
_Example:
resource "aws_s3_bucket" "bucket2" {
bucket = "my-second-bucket"

depends_on = [
aws_s3_bucket.bucket1
]
}_
This ensures:
bucket1 is created first
bucket2 is created only after bucket1 exists
βœ… Use depends_on only when necessary
❌ Avoid overusing it β€” let Terraform infer dependencies when possible

πŸ”Ή count β€” Creating Multiple Resources from

Lists
count is used to create multiple copies of a resource.
It works best with lists, because lists are ordered and indexable.

Example variable:
variable "bucket_names" {
type = list(string)
}

Resource using count:
resource "aws_s3_bucket" "buckets" {
count = length(var.bucket_names)
bucket = var.bucket_names[count.index]
}

Terraform will:
Loop over the list
Use count.index (0-based)
Create one resource per list element

Key points:
count.index starts from 0
Best suited for lists
Not safe with sets (unordered)

πŸ”Ή for_each β€” Iterating Over Sets & Maps

for_each is more powerful and safer than count in many scenarios.
It works best with:
sets
maps

🟑 Using for_each with Sets
Sets:
Are unordered
Do not allow duplicates
Have no indexes

Variable:
variable "bucket_set" {
type = set(string)
}

Resource:
resource "aws_s3_bucket" "set_buckets" {
for_each = var.bucket_set
bucket = each.value
}

Here:

each.value β†’ current set element
each.key and each.value are the same for sets

🟑 Using for_each with Maps

Maps have keys and values, which makes for_each extremely useful.

Variable:
variable "bucket_map" {
type = map(string)
}

Resource:
_resource "aws_s3_bucket" "map_buckets" {
for_each = var.bucket_map
bucket = each.value

tags = {
Name = each.key
}
}_

Here:
each.key β†’ map key
each.value β†’ map value

This is ideal for:
Naming
Tagging
Environment-based resources

🏁 Conclusion

Day 8 unlocked one of Terraform’s most powerful capabilities: meta arguments.

With depends_on, count, and for_each, Terraform becomes:
Predictable
Scalable
Declarative
Free from manual duplication

These concepts are essential for building real-world AWS infrastructure with Terraform.

Next up: Day 9 β€” Lifecycle Meta Argument πŸš€
This will give even finer control over how Terraform creates, updates, and destroys resources.

Top comments (0)