Day 10 — Top 10 Terraform Meta-Arguments with Examples

Abdul RaheemAbdul Raheem
3 min read

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

  1. 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.


  1. 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.


  1. 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.


  1. 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.


  1. lifecycle – Manage how Terraform handles changes.

     resource "aws_s3_bucket" "example" {
       bucket = "mybucket-123"
    
       lifecycle {
         prevent_destroy = true
       }
     }
    

    👉 Prevents accidental deletion.


  1. 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.


  1. 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.


  1. 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.


  1. 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.


  1. 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 of count 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

0
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.