0
0
Terraformcloud~10 mins

Lifecycle customization in Terraform - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Lifecycle customization
Define Resource
Apply Lifecycle Block?
NoDefault Create/Update/Delete
Yes
Check create_before_destroy
Create New
Check prevent_destroy
Block Destroy
Check ignore_changes
Skip Update
Resource State Updated
Terraform checks lifecycle settings to decide how to create, update, or delete resources safely.
Execution Sample
Terraform
resource "aws_instance" "example" {
  lifecycle {
    create_before_destroy = true
    prevent_destroy = true
    ignore_changes = ["tags"]
  }
}
This resource uses lifecycle rules to create new before deleting old, prevent accidental destroy, and ignore tag changes.
Process Table
StepLifecycle Setting CheckedConditionAction TakenResource State
1create_before_destroytrueCreate new instance before destroying oldOld and New instances exist
2prevent_destroytrueBlock destroy operationDestroy blocked, old instance remains
3ignore_changestags changedIgnore tag changes, no updateInstance running with old tags
4No more lifecycle rules-Resource state stableInstance running, no changes applied
5Attempt destroy without prevent_destroyfalseDestroy allowedInstance destroyed
6End-No further actionsFinal resource state stable
💡 Lifecycle rules applied to control creation, update, and destruction; execution stops when resource state is stable or destroy blocked.
Status Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Resource Instances1 old instance1 old + 1 new instance1 old + 1 new instance (destroy blocked)1 old + 1 new instance (tags unchanged)1 old + 1 new instance (stable)
Destroy Allowedfalsefalsefalse (blocked)falsefalse
Key Moments - 3 Insights
Why does Terraform create a new instance before destroying the old one?
Because create_before_destroy is true (see execution_table step 1), Terraform ensures no downtime by creating the new resource first.
What happens if prevent_destroy is true and a destroy is requested?
Terraform blocks the destroy action to protect the resource (see execution_table step 2), so the old instance remains.
Why are tag changes ignored even if tags are updated in configuration?
Because ignore_changes includes tags (see execution_table step 3), Terraform skips updating tags to avoid unnecessary resource replacement.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the resource state after step 1?
ABoth old and new instances exist
BOnly the old instance exists
COnly the new instance exists
DNo instances exist
💡 Hint
Check the 'Resource State' column in execution_table row for step 1
At which step does Terraform block the destroy operation due to lifecycle settings?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look for 'Block destroy operation' in the 'Action Taken' column
If ignore_changes did not include tags, what would happen at step 3?
ATerraform would ignore tag changes anyway
BTerraform would destroy the instance
CTerraform would update the tags on the instance
DTerraform would block the update
💡 Hint
Refer to step 3 where ignore_changes affects tag updates
Concept Snapshot
Terraform lifecycle customization controls resource creation, update, and deletion.
Key settings:
- create_before_destroy: create new resource before deleting old
- prevent_destroy: block accidental resource deletion
- ignore_changes: skip updates to specified attributes
Use lifecycle block inside resource to apply these rules.
Full Transcript
Terraform lifecycle customization lets you control how resources are created, updated, or destroyed. When create_before_destroy is true, Terraform creates a new resource before deleting the old one to avoid downtime. If prevent_destroy is set, Terraform blocks any destroy action to protect the resource. The ignore_changes setting tells Terraform to skip updates to certain attributes, like tags, so changes there don't cause resource replacement. These lifecycle rules help manage resource state safely and predictably during infrastructure changes.