📖 Scenario: You are managing cloud infrastructure using Terraform. Sometimes, you want to control how Terraform handles resource changes, like preventing deletion or ignoring certain attribute changes.
🎯 Goal: Build a Terraform configuration that creates an AWS S3 bucket with lifecycle customization to prevent accidental deletion and ignore changes to the bucket's tags.
📋 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 to my_bucket that prevents the bucket from being deleted
Configure the lifecycle block to ignore changes to the tags attribute
💡 Why This Matters
🌍 Real World
Lifecycle customization in Terraform helps protect critical cloud resources from accidental deletion and manage updates safely.
💼 Career
Cloud engineers and DevOps professionals use lifecycle blocks to control resource behavior during infrastructure changes.
Progress0 / 4 steps
1
Create AWS S3 bucket resource
Create a Terraform resource block named aws_s3_bucket.my_bucket with the bucket name set to "my-unique-bucket-12345".
Terraform
Hint
Use resource "aws_s3_bucket" "my_bucket" {} and set the bucket attribute.
2
Add lifecycle block to prevent deletion
Inside the aws_s3_bucket.my_bucket resource, add a lifecycle block with prevent_destroy = true to stop accidental deletion.
Terraform
Hint
Use a lifecycle block with prevent_destroy = true inside the resource.
3
Configure lifecycle to ignore tag changes
Inside the lifecycle block of aws_s3_bucket.my_bucket, add ignore_changes = ["tags"] to ignore changes to the bucket's tags.
Terraform
Hint
Add ignore_changes = ["tags"] inside the lifecycle block.
4
Add tags to the S3 bucket
Add a tags attribute to aws_s3_bucket.my_bucket with Environment = "Dev" and Owner = "CloudTeam".
Terraform
Hint
Add a tags map with the specified keys and values inside the resource.
Practice
(1/5)
1. What does the create_before_destroy lifecycle setting do in Terraform?
easy
A. It ignores changes to specific resource attributes during updates.
B. It prevents the resource from being destroyed under any circumstances.
C. It forces Terraform to destroy the resource before creating a new one.
D. It creates a new resource before destroying the old one to avoid downtime.
Solution
Step 1: Understand the purpose of create_before_destroy
This setting ensures Terraform creates the replacement resource first.
Step 2: Avoid downtime during resource replacement
By creating the new resource before destroying the old, service interruptions are minimized.
Final Answer:
It creates a new resource before destroying the old one to avoid downtime. -> Option D
Quick Check:
create_before_destroy = create new before delete [OK]
Hint: Remember: create_before_destroy means new first, then old removed [OK]
Common Mistakes:
Confusing create_before_destroy with prevent_destroy
Thinking it deletes first then creates
Assuming it ignores changes
2. Which of the following is the correct syntax to prevent a resource from being destroyed in Terraform?
easy
A. lifecycle { ignore_changes = true }
B. lifecycle { prevent_destroy = true }
C. lifecycle { create_before_destroy = true }
D. lifecycle { destroy_protection = true }
Solution
Step 1: Identify the correct lifecycle argument to prevent destruction
The argument is prevent_destroy set to true.
Step 2: Verify syntax correctness
The syntax is lifecycle { prevent_destroy = true }, which is valid Terraform code.
Final Answer:
lifecycle { prevent_destroy = true } -> Option B
Quick Check:
prevent_destroy = true prevents deletion [OK]
Hint: Use prevent_destroy = true to block resource deletion [OK]
Common Mistakes:
Using ignore_changes instead of prevent_destroy
Using a non-existent destroy_protection argument
Confusing create_before_destroy with prevent_destroy
5. You want to update an AWS S3 bucket's tags without triggering bucket replacement or downtime. Which lifecycle customization should you apply in Terraform to achieve this?
hard
A. Use ignore_changes = ["tags"] to skip tag updates.
B. Use prevent_destroy = true to block bucket deletion.
C. Use create_before_destroy = true to create new bucket first.
D. Use ignore_changes = ["versioning"] to skip versioning changes.
Solution
Step 1: Identify the goal to update tags without replacement
Tags often cause resource updates; ignoring changes to tags avoids replacement.
Step 2: Choose lifecycle setting to ignore tag changes
Setting ignore_changes = ["tags"] tells Terraform not to replace the bucket when tags change.
Final Answer:
Use ignore_changes = ["tags"] to skip tag updates. -> Option A
Quick Check:
ignore_changes on tags prevents replacement on tag updates [OK]
Hint: Ignore tag changes to update tags without replacement [OK]
Common Mistakes:
Using prevent_destroy which blocks deletion but not replacement
Using create_before_destroy which affects replacement order
Ignoring unrelated attributes like versioning
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.