What does the prevent_destroy lifecycle rule do in Terraform?
It stops Terraform from deleting a resource, even if you run terraform destroy or remove the resource from your configuration. This protects important resources from accidental deletion.
Click to reveal answer
beginner
How do you enable the prevent_destroy rule in a Terraform resource?
Inside the resource block, add a lifecycle block with prevent_destroy = true. For example:
Why is using prevent_destroy considered a best practice for critical resources?
Because it acts like a safety lock, preventing accidental deletion of resources that could cause downtime or data loss if removed unintentionally.
Click to reveal answer
beginner
What happens if you try to delete a resource with prevent_destroy enabled?
Terraform will show an error and stop the deletion process. You must first remove or disable prevent_destroy to delete the resource.
Click to reveal answer
intermediate
Can prevent_destroy protect resources from manual deletion outside Terraform?
No. It only protects resources managed by Terraform during Terraform operations. Manual deletion outside Terraform is not prevented by this rule.
Click to reveal answer
What is the purpose of the prevent_destroy lifecycle rule in Terraform?
ATo automatically recreate a resource if deleted
BTo allow manual deletion of a resource
CTo prevent Terraform from creating a resource
DTo prevent a resource from being deleted by Terraform
✗ Incorrect
The prevent_destroy rule stops Terraform from deleting a resource during destroy or update operations.
How do you enable prevent_destroy for a resource?
AUse <code>terraform protect</code> command
BSet <code>destroy_protection = true</code> at the provider level
CAdd <code>prevent_destroy = true</code> inside a <code>lifecycle</code> block
DAdd <code>prevent_delete = true</code> in the resource block
✗ Incorrect
You enable it by adding prevent_destroy = true inside the resource's lifecycle block.
What happens if you try to delete a resource with prevent_destroy enabled?
ATerraform shows an error and stops deletion
BTerraform deletes it anyway
CTerraform ignores the resource
DTerraform recreates the resource automatically
✗ Incorrect
Terraform will stop and show an error to prevent accidental deletion.
Does prevent_destroy protect resources from manual deletion outside Terraform?
AYes, it blocks manual deletion
BNo, it only protects Terraform operations
CYes, but only for AWS resources
DNo, it only protects during creation
✗ Incorrect
It only prevents deletion during Terraform runs, not manual deletion outside Terraform.
Which of these is a good use case for prevent_destroy?
AProtecting a database instance from accidental deletion
BAutomatically scaling resources
CAllowing quick resource replacement
DIgnoring resource changes
✗ Incorrect
Critical resources like databases should be protected from accidental deletion using prevent_destroy.
Explain how the prevent_destroy lifecycle rule works in Terraform and why it is useful.
Think about how to keep important things safe from being removed by mistake.
You got /4 concepts.
Describe a scenario where using prevent_destroy would be important and how you would configure it.
Consider resources that hold valuable data or services.
You got /4 concepts.
Practice
(1/5)
1. What does the prevent_destroy lifecycle rule do in Terraform?
easy
A. Stops accidental deletion of a resource
B. Automatically recreates a resource if deleted
C. Prevents updates to a resource
D. Locks the resource to prevent any changes
Solution
Step 1: Understand the purpose of prevent_destroy
The prevent_destroy rule 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 A
Quick Check:
prevent_destroy stops deletion = A [OK]
Hint: Remember: prevent_destroy means no accidental deletes [OK]
Common Mistakes:
Thinking it prevents updates instead of deletion
Confusing it with resource locking
Assuming it recreates deleted resources automatically
2. Which is the correct way to set the prevent_destroy rule in a Terraform resource?
easy
A. resource { prevent_destroy = true }
B. prevent_destroy = true
C. lifecycle { prevent_destroy = true }
D. lifecycle { prevent_delete = true }
Solution
Step 1: Identify correct syntax for lifecycle block
The prevent_destroy must be inside a lifecycle block within the resource.
Step 2: Check option correctness
lifecycle { prevent_destroy = true } correctly places prevent_destroy = true inside lifecycle { }. Others are either missing the block or have wrong keywords.
Final Answer:
lifecycle { prevent_destroy = true } -> Option C
Quick Check:
Correct lifecycle syntax = C [OK]
Hint: Always put prevent_destroy inside lifecycle block [OK]
Common Mistakes:
Placing prevent_destroy outside lifecycle block
Using wrong keyword like prevent_delete
Adding prevent_destroy directly under resource
3. Given this Terraform resource snippet, what happens if you run terraform destroy?
Removing prevent_destroy allows deletion but risks downtime; manual deletion is unsafe.
Step 2: Disable prevent_destroy for safe replacement
Setting prevent_destroy = false inside the lifecycle block disables the deletion block. Then terraform apply can replace the resource if config changes require recreation.
Step 3: Apply changes safely
This triggers replacement using Terraform's logic (e.g., with create_before_destroy if set), avoiding manual steps.
Final Answer:
Set prevent_destroy to false and run terraform apply to replace resource -> Option D
Quick Check:
Set prevent_destroy = false allows replacement = C [OK]
Hint: Set prevent_destroy = false before terraform apply to replace [OK]
Common Mistakes:
Using `terraform taint` without disabling prevent_destroy
Manually deleting resource causing drift
Running destroy separately, risking downtime
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.