0
0
Terraformcloud~5 mins

Meta-arguments overview in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes you want to control how Terraform creates or manages resources beyond just setting their properties. Meta-arguments help you do that by adding extra instructions to your resource blocks.
When you want to create multiple similar resources without repeating code.
When you need to run a resource only if a certain condition is true.
When you want to control the order in which resources are created or destroyed.
When you want to ignore changes to certain resource attributes during updates.
When you want to add dependencies between resources explicitly.
Config File - main.tf
main.tf
terraform {
  required_version = ">= 1.0"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  count = 2

  lifecycle {
    ignore_changes = ["tags"]
  }

  depends_on = [aws_security_group.sg]

  tags = {
    Name = "example-instance"
  }
}

resource "aws_security_group" "sg" {
  name        = "example-sg"
  description = "Example security group"

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_s3_bucket" "conditional" {
  bucket = "example-bucket-terraform-meta"

  count = var.create_bucket ? 1 : 0
}

variable "create_bucket" {
  type    = bool
  default = true
}

This Terraform file shows several meta-arguments:

  • count creates multiple instances or conditionally creates resources.
  • lifecycle.ignore_changes tells Terraform to ignore changes to tags during updates.
  • depends_on forces Terraform to create the security group before the instance.
  • The variable create_bucket controls whether the S3 bucket is created.
Commands
This command initializes the Terraform working directory. It downloads necessary provider plugins and prepares the environment.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Installing hashicorp/aws v4.0.0... - Installed hashicorp/aws v4.0.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command shows what Terraform will do based on the current configuration and state. It previews resource creation, changes, or destruction.
Terminal
terraform plan
Expected OutputExpected
An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_security_group.sg will be created + resource "aws_security_group" "sg" { + description = "Example security group" + id = (known after apply) + name = "example-sg" + ... } # aws_instance.example[0] will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t2.micro" + ... } # aws_instance.example[1] will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t2.micro" + ... } # aws_s3_bucket.conditional will be created + resource "aws_s3_bucket" "conditional" { + bucket = "example-bucket-terraform-meta" + ... } Plan: 4 to add, 0 to change, 0 to destroy.
This command applies the planned changes to create or update resources. The flag skips the confirmation prompt for faster execution.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_security_group.sg: Creating... aws_security_group.sg: Creation complete after 2s [id=sg-12345678] aws_instance.example[0]: Creating... aws_instance.example[1]: Creating... aws_instance.example[0]: Creation complete after 10s [id=i-0123456789abcdef0] aws_instance.example[1]: Creation complete after 10s [id=i-0fedcba9876543210] aws_s3_bucket.conditional: Creating... aws_s3_bucket.conditional: Creation complete after 3s [id=example-bucket-terraform-meta] Apply complete! Resources: 4 added, 0 changed, 0 destroyed.
-auto-approve - Skips manual approval to apply changes immediately
This command displays the current state of the infrastructure managed by Terraform, showing all resources and their attributes.
Terminal
terraform show
Expected OutputExpected
... # aws_instance.example[0]: resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" id = "i-0123456789abcdef0" tags = { Name = "example-instance" } ... } # aws_instance.example[1]: resource "aws_instance" "example" { ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" id = "i-0fedcba9876543210" tags = { Name = "example-instance" } ... } # aws_security_group.sg: resource "aws_security_group" "sg" { description = "Example security group" id = "sg-12345678" name = "example-sg" ... } # aws_s3_bucket.conditional: resource "aws_s3_bucket" "conditional" { bucket = "example-bucket-terraform-meta" id = "example-bucket-terraform-meta" ... }
Key Concept

Meta-arguments let you control resource creation, dependencies, and lifecycle beyond basic settings.

Common Mistakes
Using count with a resource but forgetting it creates a list of resources.
Terraform treats resources with count as a list, so referencing them without index causes errors.
Always use the index like resource_name[0] to access individual instances.
Not using depends_on when resource creation order matters.
Terraform may create resources in parallel, causing failures if one depends on another.
Use depends_on to explicitly set creation order when needed.
Ignoring lifecycle.ignore_changes and then manually changing ignored attributes outside Terraform.
Terraform will try to revert those changes unless ignore_changes is set.
Use lifecycle.ignore_changes to prevent Terraform from overwriting manual changes.
Summary
Use meta-arguments like count, depends_on, and lifecycle to control resource behavior.
count can create multiple or conditional resources.
depends_on ensures resources are created in the right order.
lifecycle.ignore_changes prevents Terraform from overwriting specific changes.