Terraform gets much easier to use once you understand its meta arguments. These arguments help you decide how many resources to create, how they should be linked, and how Terraform keeps track of them. In this article, we will break down the three most commonly used meta arguments: count, for_each, and depends_on.
These are important to know if you're building AWS infrastructure with Terraform, especially when creating multiple resources or managing their order.
1. count: Create Multiple Resources Easily
count allows you to create multiple instances of the same resource using a simple number. It is the most straightforward way to scale resource creation.
Example
resource "aws_s3_bucket" "bucket_example" {
count = 3
bucket = "my-bucket-${count.index}"
}
How it works
-
count = 3creates three buckets. - Each resource gets an index: 0, 1, 2.
- The index helps generate unique names or tags.
When to use
- When resources are identical.
- When you want simple numeric scaling.
- When the order does not matter.
2. for_each: Create Resources With Stable Identity
for_each is more flexible and safer than count, especially in production. It creates resources based on a map or set, giving each resource a stable identity.
Example
resource "aws_s3_bucket" "bucket_foreach" {
for_each = toset(["bucket-a", "bucket-b"])
bucket = each.value
}
How it works
- Uses
each.keyandeach.valueinstead of numeric indexes. - Resource identity does not change when the list order changes.
- Ideal when each resource represents a unique logical item.
When to use
- When resource names must remain stable.
- When creating resources from lists, sets, or maps.
- When avoiding recreation caused by index shifting.
3. depends_on: Control Resource Creation Order
Terraform normally detects dependencies automatically. However, some situations require explicit ordering, and that is where depends_on helps.
Example
resource "aws_s3_bucket" "primary" {
bucket = "primary-bucket"
}
resource "aws_s3_bucket" "secondary" {
bucket = "secondary-bucket"
depends_on = [aws_s3_bucket.primary]
}
How it works
- Ensures
secondaryis created only afterprimary. - Useful when there is no direct reference linking two resources.
- Gives you full control over creation and destruction sequence .
When to use
- When a resource must exist before another one.
- For IAM roles, attachments, permissions, or cross-module links.
- When Terraform cannot automatically determine the dependency.
Conclusion
Terraform meta arguments play a crucial role in managing infrastructure at scale. With count, you can quickly replicate resources; with for_each, you gain predictable, stable resource identity; and with depends_on, you get full control over dependency ordering. Together, these features allow you to write cleaner, safer, and more maintainable Terraform configurations for AWS. Mastering these fundamentals sets the foundation for building reliable, production-ready Infrastructure as Code.
References
>> Connect With Me
If you enjoyed this post or want to follow my #30DaysOfAWSTerraformChallenge journey, feel free to connect with me here:
๐ผ LinkedIn: Amit Kushwaha
๐ GitHub: Amit Kushwaha
๐ Hashnode / Amit Kushwaha
๐ฆ Twitter/X: Amit Kushwaha
Top comments (0)