0
0
Terraformcloud~10 mins

Create_before_destroy lifecycle rule in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Create_before_destroy lifecycle rule
Start Apply
Check resource change
Create new resource
Wait for new resource ready
Destroy old resource
Apply complete
Terraform first creates the new resource before destroying the old one to avoid downtime.
Execution Sample
Terraform
resource "aws_instance" "example" {
  lifecycle {
    create_before_destroy = true
  }
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}
This Terraform code creates an AWS instance with the create_before_destroy lifecycle rule to ensure new instance is created before old one is destroyed.
Process Table
StepActionResource State BeforeResource State AfterNotes
1Start applyOld instance existsOld instance existsTerraform plan starts
2Create new instanceOld instance existsOld instance + New instanceNew instance is created first
3Wait for new instance readyOld instance + New instanceOld instance + New instanceEnsures new instance is healthy
4Destroy old instanceOld instance + New instanceNew instance onlyOld instance is safely removed
5Apply completeNew instance onlyNew instance onlyInfrastructure updated without downtime
💡 Old instance destroyed only after new instance is ready, ensuring zero downtime.
Status Tracker
VariableStartAfter Step 2After Step 4Final
old_instanceexistsexistsdestroyeddestroyed
new_instancenot existscreatedcreatedcreated
Key Moments - 2 Insights
Why does Terraform create a new resource before destroying the old one?
Because the create_before_destroy rule tells Terraform to avoid downtime by ensuring the new resource is ready before removing the old one, as shown in steps 2 and 4 of the execution_table.
What happens if the new resource fails to become ready?
Terraform will not destroy the old resource, preventing service interruption. This is implied in step 3 where Terraform waits for readiness before proceeding.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does the new instance get created?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Refer to the 'Action' column in execution_table row for Step 2.
According to variable_tracker, what is the state of old_instance after Step 4?
Aexists
Bcreated
Cdestroyed
Dnot exists
💡 Hint
Check the 'old_instance' row under 'After Step 4' in variable_tracker.
If create_before_destroy was false, what would change in the execution_table?
AOld instance destroyed before new instance created
BNew instance created before old instance destroyed
CBoth instances created simultaneously
DNo instances destroyed
💡 Hint
The lifecycle rule controls creation and destruction order shown in execution_table steps.
Concept Snapshot
Terraform lifecycle create_before_destroy rule:
- Ensures new resource is created before old is destroyed
- Prevents downtime during resource replacement
- Applies during terraform apply phase
- Useful for resources needing continuous availability
- Syntax: lifecycle { create_before_destroy = true }
Full Transcript
This visual execution trace shows how Terraform applies the create_before_destroy lifecycle rule. First, Terraform starts the apply process with the old resource existing. Then it creates the new resource while keeping the old one active. Terraform waits until the new resource is ready and healthy. Only after that does it destroy the old resource. This sequence prevents downtime by ensuring continuous availability. The variable tracker shows the old instance exists initially and is destroyed only after the new instance is created. Key moments clarify why the new resource must be ready before destroying the old one and what happens if readiness fails. The quiz tests understanding of the creation and destruction order and the effect of disabling this rule.