0
0
Terraformcloud~5 mins

Create_before_destroy lifecycle rule in Terraform - Commands & Configuration

Choose your learning style9 modes available
Introduction
Sometimes when you update infrastructure, you want the new resource ready before the old one is removed. This avoids downtime or service interruption. The create_before_destroy rule in Terraform helps by making sure the new resource is created first, then the old one is deleted.
When updating a server or database and you want to avoid downtime during replacement.
When changing a resource that cannot be updated in place and would cause service interruption if destroyed first.
When managing load balancers and you want to keep traffic flowing while replacing backend instances.
When updating DNS records that need to be available continuously without gaps.
When deploying cloud resources that require a smooth transition from old to new versions.
Config File - main.tf
main.tf
resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  lifecycle {
    create_before_destroy = true
  }

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

This Terraform file creates an AWS EC2 instance.

The lifecycle block with create_before_destroy = true tells Terraform to create the new instance before destroying the old one during updates.

This helps avoid downtime by ensuring the new instance is ready first.

Commands
This command initializes the Terraform working directory. It downloads necessary provider plugins and prepares Terraform to run.
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 when applying the configuration. It previews the creation of the AWS instance.
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_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-0c55b159cbfafe1f0" + arn = (known after apply) + instance_type = "t2.micro" + tags = { + "Name" = "example-instance" } } Plan: 1 to add, 0 to change, 0 to destroy. ───────────────────────────────────────────────────────────────────────────── Note: You didn't specify an "-out" parameter to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
This command applies the Terraform configuration, creating the AWS instance. The -auto-approve flag skips manual approval.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 15s [id=i-0abcd1234efgh5678] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
-auto-approve - Automatically approve the apply without prompting
If you change the instance_type in the config and run this again, Terraform will create the new instance before destroying the old one because of create_before_destroy.
Terminal
terraform apply -auto-approve
Expected OutputExpected
aws_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Creation complete after 15s [id=i-0wxyz9876mnop4321] aws_instance.example: Destroying... [id=i-0abcd1234efgh5678] aws_instance.example: Destruction complete after 10s Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
-auto-approve - Automatically approve the apply without prompting
Key Concept

If you remember nothing else from this pattern, remember: create_before_destroy makes Terraform build the new resource first to avoid downtime before removing the old one.

Common Mistakes
Not adding the lifecycle block with create_before_destroy when replacing critical resources.
Terraform will destroy the old resource before creating the new one, causing downtime or service interruption.
Always add lifecycle { create_before_destroy = true } to resources that must stay available during updates.
Using create_before_destroy on resources that cannot have two instances simultaneously.
This causes Terraform to fail because the cloud provider disallows duplicate resources.
Use create_before_destroy only when the resource supports having both old and new instances at the same time.
Summary
Initialize Terraform with terraform init to prepare the environment.
Use terraform plan to preview changes before applying.
Apply changes with terraform apply -auto-approve to create or update resources.
Add lifecycle { create_before_destroy = true } to ensure new resources are created before old ones are destroyed, avoiding downtime.