Bird
Raised Fist0
Terraformcloud~10 mins

Lifecycle customization in Terraform - Step-by-Step Execution

Choose your learning style10 modes available
Process Flow - Lifecycle customization
Define Resource
↓
Apply Lifecycle Block?
No→Default 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.

Practice

(1/5)
1. What does the create_before_destroy lifecycle setting do in Terraform?
easy
A. It ignores changes to specific resource attributes during updates.
B. It prevents the resource from being destroyed under any circumstances.
C. It forces Terraform to destroy the resource before creating a new one.
D. It creates a new resource before destroying the old one to avoid downtime.

Solution

  1. Step 1: Understand the purpose of create_before_destroy

    This setting ensures Terraform creates the replacement resource first.
  2. Step 2: Avoid downtime during resource replacement

    By creating the new resource before destroying the old, service interruptions are minimized.
  3. Final Answer:

    It creates a new resource before destroying the old one to avoid downtime. -> Option D
  4. Quick Check:

    create_before_destroy = create new before delete [OK]
Hint: Remember: create_before_destroy means new first, then old removed [OK]
Common Mistakes:
  • Confusing create_before_destroy with prevent_destroy
  • Thinking it deletes first then creates
  • Assuming it ignores changes
2. Which of the following is the correct syntax to prevent a resource from being destroyed in Terraform?
easy
A. lifecycle { ignore_changes = true }
B. lifecycle { prevent_destroy = true }
C. lifecycle { create_before_destroy = true }
D. lifecycle { destroy_protection = true }

Solution

  1. Step 1: Identify the correct lifecycle argument to prevent destruction

    The argument is prevent_destroy set to true.
  2. Step 2: Verify syntax correctness

    The syntax is lifecycle { prevent_destroy = true }, which is valid Terraform code.
  3. Final Answer:

    lifecycle { prevent_destroy = true } -> Option B
  4. Quick Check:

    prevent_destroy = true prevents deletion [OK]
Hint: Use prevent_destroy = true to block resource deletion [OK]
Common Mistakes:
  • Using ignore_changes instead of prevent_destroy
  • Using a non-existent destroy_protection argument
  • Confusing create_before_destroy with prevent_destroy
3. Given this Terraform resource snippet:
resource "aws_instance" "example" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  lifecycle {
    ignore_changes = ["tags"]
  }
}
What happens if you manually change the tags of this instance outside Terraform and then run terraform apply?
medium
A. Terraform will ignore the tag changes and keep the manual tags as is.
B. Terraform will destroy and recreate the instance to sync tags.
C. Terraform will throw an error due to tag mismatch.
D. Terraform will overwrite the manual tag changes with the configuration tags.

Solution

  1. Step 1: Understand the effect of ignore_changes on tags

    The lifecycle setting ignore_changes = ["tags"] tells Terraform to ignore any changes to tags made outside Terraform.
  2. Step 2: Predict Terraform behavior on apply

    Terraform will not attempt to update tags, so manual changes remain intact without error or replacement.
  3. Final Answer:

    Terraform will ignore the tag changes and keep the manual tags as is. -> Option A
  4. Quick Check:

    ignore_changes on tags means manual tag changes stay [OK]
Hint: ignore_changes = ["tags"] means Terraform skips tag updates [OK]
Common Mistakes:
  • Assuming Terraform overwrites manual tag changes
  • Expecting an error on tag mismatch
  • Thinking resource will be recreated
4. You have this Terraform lifecycle block:
lifecycle {
  create_before_destroy = true
  prevent_destroy = true
}
What problem will occur if you try to update this resource that requires replacement?
medium
A. Terraform will create the new resource and destroy the old one successfully.
B. Terraform will ignore prevent_destroy and destroy the resource anyway.
C. Terraform will fail because it cannot destroy the old resource due to prevent_destroy.
D. Terraform will skip the update and keep the old resource unchanged.

Solution

  1. Step 1: Analyze interaction of create_before_destroy and prevent_destroy

    create_before_destroy tries to create new resource before deleting old one, but prevent_destroy forbids deletion.
  2. Step 2: Understand Terraform behavior on replacement

    Terraform cannot destroy the old resource due to prevent_destroy, so replacement fails.
  3. Final Answer:

    Terraform will fail because it cannot destroy the old resource due to prevent_destroy. -> Option C
  4. Quick Check:

    prevent_destroy blocks deletion, causing failure with create_before_destroy [OK]
Hint: prevent_destroy blocks deletion, so replacement fails [OK]
Common Mistakes:
  • Assuming Terraform ignores prevent_destroy
  • Thinking resource updates without replacement
  • Believing create_before_destroy overrides prevent_destroy
5. You want to update an AWS S3 bucket's tags without triggering bucket replacement or downtime. Which lifecycle customization should you apply in Terraform to achieve this?
hard
A. Use ignore_changes = ["tags"] to skip tag updates.
B. Use prevent_destroy = true to block bucket deletion.
C. Use create_before_destroy = true to create new bucket first.
D. Use ignore_changes = ["versioning"] to skip versioning changes.

Solution

  1. Step 1: Identify the goal to update tags without replacement

    Tags often cause resource updates; ignoring changes to tags avoids replacement.
  2. Step 2: Choose lifecycle setting to ignore tag changes

    Setting ignore_changes = ["tags"] tells Terraform not to replace the bucket when tags change.
  3. Final Answer:

    Use ignore_changes = ["tags"] to skip tag updates. -> Option A
  4. Quick Check:

    ignore_changes on tags prevents replacement on tag updates [OK]
Hint: Ignore tag changes to update tags without replacement [OK]
Common Mistakes:
  • Using prevent_destroy which blocks deletion but not replacement
  • Using create_before_destroy which affects replacement order
  • Ignoring unrelated attributes like versioning
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard