Using the Prevent_destroy Lifecycle Rule in Terraform
📖 Scenario: You are managing cloud resources using Terraform. Sometimes, you want to make sure that certain important resources are not accidentally deleted when you run Terraform commands.One way to protect these resources is by using the prevent_destroy lifecycle rule in Terraform. This rule stops Terraform from deleting the resource unless you remove the rule first.
🎯 Goal: Build a Terraform configuration that creates an AWS S3 bucket and uses the prevent_destroy lifecycle rule to protect it from accidental deletion.
📋 What You'll Learn
Create an AWS S3 bucket resource named my_bucket with the bucket name my-unique-bucket-12345.
Add a lifecycle block with the prevent_destroy = true setting inside the S3 bucket resource.
Use Terraform version 1.0 or later syntax.
Ensure the configuration is valid and deployable.
💡 Why This Matters
🌍 Real World
Cloud engineers often need to protect critical resources from accidental deletion during infrastructure updates. The prevent_destroy lifecycle rule is a simple way to add this protection.
💼 Career
Understanding lifecycle rules in Terraform is essential for roles like Cloud Engineer, DevOps Engineer, and Infrastructure Developer to manage infrastructure safely and reliably.
Progress0 / 4 steps
1
Create the AWS S3 bucket resource
Write a Terraform resource block named aws_s3_bucket.my_bucket that creates an S3 bucket with the exact bucket name my-unique-bucket-12345.
Terraform
Hint
Use the resource keyword followed by "aws_s3_bucket" "my_bucket" and set the bucket attribute.
2
Add the lifecycle block
Inside the aws_s3_bucket.my_bucket resource, add a lifecycle block.
Terraform
Hint
The lifecycle block goes inside the resource block, indented one level.
3
Set prevent_destroy to true
Inside the lifecycle block of aws_s3_bucket.my_bucket, add the line prevent_destroy = true.
Terraform
Hint
Inside the lifecycle block, write prevent_destroy = true exactly.
4
Complete the Terraform configuration
Ensure the full Terraform configuration for aws_s3_bucket.my_bucket includes the bucket name and the lifecycle block with prevent_destroy = true.
Terraform
Hint
Make sure all parts are present and correctly indented.
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.