What if one wrong click could erase your entire cloud setup forever?
Why Prevent_destroy lifecycle rule in Terraform? - Purpose & Use Cases
Imagine you manage a cloud server that runs your website. One day, you accidentally delete it while cleaning up resources because you forgot which one was critical.
Manually tracking which resources are important is hard. Mistakes happen easily, causing downtime and lost data. Fixing these mistakes takes time and can be costly.
The prevent_destroy rule in Terraform stops important resources from being deleted by mistake. It acts like a safety lock, so you must consciously remove the lock before deleting.
resource "aws_instance" "web" { # no protection, can be deleted anytime }
resource "aws_instance" "web" { lifecycle { prevent_destroy = true } }
This rule lets you protect critical cloud resources from accidental deletion, keeping your services safe and reliable.
A company uses prevent_destroy on their database server resource to avoid accidental removal during updates, preventing data loss and downtime.
Manual deletion risks breaking important cloud resources.
prevent_destroy adds a safety lock to stop accidental deletes.
It helps keep your cloud infrastructure stable and secure.
Practice
prevent_destroy lifecycle rule do in Terraform?Solution
Step 1: Understand the purpose of prevent_destroy
Theprevent_destroyrule is designed to stop Terraform from deleting a resource accidentally.Step 2: Compare with other lifecycle rules
Unlike update prevention or locking, it specifically blocks deletion unless the rule is removed.Final Answer:
Stops accidental deletion of a resource -> Option AQuick Check:
prevent_destroy stops deletion = A [OK]
- Thinking it prevents updates instead of deletion
- Confusing it with resource locking
- Assuming it recreates deleted resources automatically
prevent_destroy rule in a Terraform resource?Solution
Step 1: Identify correct syntax for lifecycle block
Theprevent_destroymust be inside alifecycleblock within the resource.Step 2: Check option correctness
lifecycle { prevent_destroy = true } correctly placesprevent_destroy = trueinsidelifecycle { }. Others are either missing the block or have wrong keywords.Final Answer:
lifecycle { prevent_destroy = true } -> Option CQuick Check:
Correct lifecycle syntax = C [OK]
- Placing prevent_destroy outside lifecycle block
- Using wrong keyword like prevent_delete
- Adding prevent_destroy directly under resource
terraform destroy?
resource "aws_s3_bucket" "example" {
bucket = "my-bucket"
lifecycle {
prevent_destroy = true
}
}Solution
Step 1: Understand prevent_destroy effect on destroy
Theprevent_destroy = truestops Terraform from deleting the resource during destroy.Step 2: Predict Terraform behavior on destroy
Terraform will detect the rule and raise an error, preventing the deletion.Final Answer:
Terraform will show an error and stop the destroy -> Option AQuick Check:
prevent_destroy blocks destroy = A [OK]
- Assuming resource deletes anyway
- Thinking it updates instead of blocking destroy
- Believing Terraform silently ignores the rule
prevent_destroy = true to a resource but now need to delete it. What is the correct way to fix the error during terraform destroy?Solution
Step 1: Understand how prevent_destroy blocks deletion
The rule must be removed or disabled in Terraform code before deletion can proceed.Step 2: Identify correct fix
Removing or commenting out the line and applying changes allows Terraform to delete the resource safely.Final Answer:
Remove or comment out the prevent_destroy line and apply again -> Option BQuick Check:
Disable prevent_destroy before destroy = B [OK]
- Using unsupported --force flag
- Setting prevent_destroy directly under resource
- Deleting resource manually without Terraform sync
prevent_destroy = true. You want to replace this resource safely without downtime. Which approach is best?Solution
Step 1: Understand prevent_destroy blocks deletion
Removing prevent_destroy allows deletion but risks downtime; manual deletion is unsafe.Step 2: Disable prevent_destroy for safe replacement
Settingprevent_destroy = falseinside thelifecycleblock disables the deletion block. Thenterraform applycan replace the resource if config changes require recreation.Step 3: Apply changes safely
This triggers replacement using Terraform's logic (e.g., withcreate_before_destroyif set), avoiding manual steps.Final Answer:
Set prevent_destroy to false and run terraform apply to replace resource -> Option DQuick Check:
Set prevent_destroy = false allows replacement = C [OK]
- Using `terraform taint` without disabling prevent_destroy
- Manually deleting resource causing drift
- Running destroy separately, risking downtime
Ready to prove it?
Pass the test and this pattern moves unknown
