Prevent_destroy lifecycle rule in Terraform - Time & Space Complexity
We want to understand how the use of the prevent_destroy lifecycle rule affects the number of operations Terraform performs.
Specifically, we ask: How does this rule impact the steps Terraform takes when destroying resources?
Analyze the time complexity of applying prevent_destroy in a resource lifecycle.
resource "aws_s3_bucket" "example" {
count = var.bucket_count
bucket = "example-bucket-${count.index}"
lifecycle {
prevent_destroy = true
}
}
This code creates multiple S3 buckets and prevents them from being destroyed by Terraform.
Look at what Terraform does repeatedly when this rule is set.
- Primary operation: Checking each resource for destruction permission before destroy.
- How many times: Once per resource instance (equal to
countvalue).
As the number of buckets increases, Terraform checks each one to prevent accidental deletion.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of checks grows directly with the number of resources.
Time Complexity: O(n)
This means the time to verify destruction prevention grows linearly with the number of resources.
[X] Wrong: "Using prevent_destroy stops Terraform from checking resources during destroy."
[OK] Correct: Terraform still checks every resource to enforce the rule, so the operation count grows with resource count.
Understanding how lifecycle rules affect operation counts shows you can predict infrastructure management costs and behavior.
What if we removed prevent_destroy from some resources? How would the time complexity change?
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
