0
0
Terraformcloud~20 mins

Prevent_destroy lifecycle rule in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
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
Need a 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
Need a 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
Need a 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
Need a hint?

Make sure all parts are present and correctly indented.