0
0
Terraformcloud~5 mins

Why meta-arguments control resource behavior in Terraform - Why It Works

Choose your learning style9 modes available
Introduction
Sometimes you want to change how Terraform creates or manages resources beyond just their settings. Meta-arguments let you control this behavior, like deciding when to create or destroy resources or how to handle dependencies.
When you want to make sure a resource is created only after another resource is ready
When you want to prevent Terraform from destroying a resource accidentally
When you want to create multiple copies of a resource easily
When you want to ignore changes to certain resource attributes during updates
When you want to control the order of resource creation and deletion
Config File - main.tf
main.tf
resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  lifecycle {
    prevent_destroy = true
    ignore_changes  = ["tags"]
  }

  depends_on = [aws_security_group.sg]
}

resource "aws_security_group" "sg" {
  name        = "example-sg"
  description = "Allow SSH"

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

resource "aws_instance" "multiple" {
  count         = 3
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
}

This file shows several meta-arguments:

  • lifecycle.prevent_destroy stops Terraform from deleting the instance accidentally.
  • lifecycle.ignore_changes tells Terraform to ignore changes to tags so it won't update the instance just because tags changed.
  • depends_on ensures the security group is created before the instance.
  • count creates multiple copies of the same instance easily.
Commands
This command sets up Terraform in the current folder by downloading necessary plugins and preparing 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!
This command shows what Terraform will do before making any changes. It helps you verify that your meta-arguments will behave as expected.
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 = "Allow SSH" + id = (known after apply) + name = "example-sg" + ... } # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + instance_type = "t2.micro" + ... } # aws_instance.multiple[0] will be created # aws_instance.multiple[1] will be created # aws_instance.multiple[2] will be created + resource "aws_instance" "multiple" { + ami = "ami-0c55b159cbfafe1f0" + count = 3 + instance_type = "t2.micro" + ... } Plan: 5 to add, 0 to change, 0 to destroy.
This command applies the planned changes and creates the resources with the meta-arguments controlling their behavior.
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: Creating... aws_instance.multiple[0]: Creating... aws_instance.multiple[1]: Creating... aws_instance.multiple[2]: Creating... aws_instance.example: Creation complete after 10s [id=i-1234567890abcdef0] aws_instance.multiple[0]: Creation complete after 10s [id=i-abcdef01234567890] aws_instance.multiple[1]: Creation complete after 10s [id=i-abcdef01234567891] aws_instance.multiple[2]: Creation complete after 10s [id=i-abcdef01234567892] Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
-auto-approve - Skip manual approval to apply changes immediately
This command tries to delete all resources, but the instance with prevent_destroy will not be destroyed.
Terminal
terraform destroy -auto-approve
Expected OutputExpected
aws_instance.multiple[0]: Destroying... aws_instance.multiple[1]: Destroying... aws_instance.multiple[2]: Destroying... aws_instance.multiple[0]: Destruction complete aws_instance.multiple[1]: Destruction complete aws_instance.multiple[2]: Destruction complete Error: Instance cannot be destroyed on main.tf line 2, in resource "aws_instance" "example": 2: resource "aws_instance" "example" { Resource aws_instance.example has lifecycle.prevent_destroy set, so it cannot be destroyed. Destroy complete! Resources: 3 destroyed, 0 changed, 1 failed.
-auto-approve - Skip manual approval to destroy resources immediately
Key Concept

If you remember nothing else from this pattern, remember: meta-arguments let you control how and when Terraform creates, updates, or deletes resources beyond their basic settings.

Common Mistakes
Not using depends_on when resource order matters
Terraform might create resources in the wrong order causing failures or errors.
Use depends_on to explicitly tell Terraform which resources must be created first.
Forgetting lifecycle.prevent_destroy on critical resources
Terraform might delete important resources accidentally during destroy or update.
Add lifecycle.prevent_destroy = true to protect important resources from deletion.
Using count without understanding its effect on resource indexing
Resources created with count are indexed and must be referenced with [index], causing confusion.
Use count carefully and reference resources with their index like resource.name[0].
Summary
terraform init prepares Terraform to work with your configuration and providers.
terraform plan shows what Terraform will do, helping you verify meta-argument effects before applying.
terraform apply creates resources respecting meta-arguments like lifecycle and depends_on.
terraform destroy tries to remove resources but respects lifecycle.prevent_destroy to protect them.