Day 10 — Top 10 Terraform Meta-Arguments with Examples


1. Introduction
What meta-arguments are (special arguments that modify the behavior of resources and modules).
Why they are important for clean, scalable infrastructure.
2. Top 10 Meta-Arguments with Examples
count
– Create multiple instances of a resource.resource "aws_instance" "example" { count = 3 ami = "ami-123456" instance_type = "t2.micro" }
👉 Spins up 3 EC2 instances.
for_each
– Iterate over a map or set.resource "aws_s3_bucket" "example" { for_each = toset(["app", "logs", "backup"]) bucket = "${each.key}-bucket-123" }
👉 Creates multiple buckets with unique names.
depends_on
– Explicitly set dependencies.resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" depends_on = [aws_s3_bucket.example] }
👉 Ensures the EC2 is created after the bucket.
provider
– Use multiple provider configurations.provider "aws" { region = "us-east-1" } provider "aws" { alias = "mumbai" region = "ap-south-1" } resource "aws_instance" "mumbai" { provider = aws.mumbai ami = "ami-123456" instance_type = "t2.micro" }
👉 Deploys in multiple regions.
lifecycle
– Manage how Terraform handles changes.resource "aws_s3_bucket" "example" { bucket = "mybucket-123" lifecycle { prevent_destroy = true } }
👉 Prevents accidental deletion.
provisioner
– Run scripts after resource creation.resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" provisioner "local-exec" { command = "echo Instance created > instance.log" } }
👉 Executes a local script after instance creation.
connection
– Define how Terraform connects to resources.resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" connection { type = "ssh" user = "ec2-user" password = "example-pass" host = self.public_ip } }
👉 Defines SSH connection for remote provisioning.
timeouts
– Control how long Terraform waits.resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" timeouts { create = "10m" delete = "5m" } }
👉 Useful for slow services.
tags
(special for AWS) – Assign metadata.resource "aws_instance" "example" { ami = "ami-123456" instance_type = "t2.micro" tags = { Name = "ExampleInstance" Env = "Dev" } }
👉 Helps in cost tracking & resource identification.
source
(in modules) – Define where the module comes from.
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "my-vpc"
cidr = "10.0.0.0/16"
}
👉 Allows reusability with community or local modules.
3. Best Practices
Use
for_each
instead ofcount
when dealing with named resources.Always tag resources for better management.
Use
depends_on
sparingly (Terraform usually handles dependencies).Avoid
provisioners
unless absolutely necessary.
4. Conclusion
Meta-arguments supercharge your Terraform.
They make code scalable, reusable, and predictable.
Follow my journey
GitHub (learning repo): https://github.com/abdulraheem381/terraform-learning-journey
X / Twitter: https://x.com/Abdulraheem183
Hashnode series: https://abdulraheem.hashnode.dev/series/terraform-with-aws
Subscribe to my newsletter
Read articles from Abdul Raheem directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Abdul Raheem
Abdul Raheem
Cloud DevOps | AWS | Terraform | CI/CD | Obsessed with clean infrastructure. Cloud DevOps Engineer 🚀 | Automating Infrastructure & Securing Pipelines | Bridging Gaps Between Code and Cloud ☁️ I’m on a mission to master DevOps from the ground up—building scalable systems, automating workflows, and integrating security into every phase of the SDLC. Currently working with AWS, Terraform, Docker, CI/CD, and learning the art of cloud-native development.