📖 Scenario: You are managing cloud resources using Terraform. You want to understand how to set values that are fixed when resources are created (creation-time) versus values that can be changed or removed when resources are destroyed (destruction-time).
🎯 Goal: Build a Terraform configuration that defines a resource with a creation-time attribute and a destruction-time attribute, showing how to set and update these values properly.
📋 What You'll Learn
Create a Terraform resource with a fixed creation-time attribute
Add a variable to configure a destruction-time attribute
Use Terraform expressions to assign these attributes correctly
Complete the resource configuration with lifecycle rules
💡 Why This Matters
🌍 Real World
Managing cloud infrastructure with Terraform requires understanding which resource attributes can be changed without recreating resources and which cannot. This helps avoid downtime and data loss.
💼 Career
Cloud engineers and DevOps professionals must write Terraform code that safely manages resource lifecycle, balancing fixed creation-time settings with flexible destruction-time updates.
Progress0 / 4 steps
1
Define a Terraform resource with a creation-time attribute
Create a Terraform resource called aws_s3_bucket named example_bucket with the attribute bucket set to the fixed string my-fixed-bucket-name.
Terraform
Hint
The bucket name is fixed at creation time and cannot be changed later.
2
Add a variable for destruction-time attribute
Add a Terraform variable called enable_versioning of type bool with a default value false to control versioning on the bucket.
Terraform
Hint
Variables allow you to configure attributes that can be changed without recreating the resource.
3
Use the variable to configure destruction-time attribute
Update the aws_s3_bucket.example_bucket resource to add a versioning block. Set enabled to the variable enable_versioning.
Terraform
Hint
The versioning block can be changed after creation, so it is a destruction-time attribute.
4
Add lifecycle rule to prevent bucket recreation
Add a lifecycle block inside aws_s3_bucket.example_bucket with prevent_destroy = true to avoid accidental bucket deletion.
Terraform
Hint
This lifecycle rule protects the bucket from being destroyed accidentally.
Practice
(1/5)
1. What does creation-time mean in Terraform?
easy
A. The moment when resources are first set up or created
B. The moment when resources are deleted or removed
C. The time when Terraform runs a plan without changes
D. The time when Terraform refreshes the state
Solution
Step 1: Understand creation-time meaning
Creation-time refers to when Terraform sets up resources for the first time.
Step 2: Differentiate from destruction-time
Destruction-time is when resources are removed, not created.
Final Answer:
The moment when resources are first set up or created -> Option A
Quick Check:
Creation-time = resource setup [OK]
Hint: Creation-time means resource setup, destruction-time means removal [OK]
Common Mistakes:
Confusing creation-time with destruction-time
Thinking creation-time is when resources are deleted
Mixing creation-time with Terraform plan execution
2. Which Terraform lifecycle argument controls actions at destruction-time?
easy
A. create_before_destroy
B. depends_on
C. ignore_changes
D. prevent_destroy
Solution
Step 1: Identify destruction-time control arguments
The prevent_destroy argument stops Terraform from deleting a resource.
Step 2: Compare other lifecycle arguments
create_before_destroy affects creation and replacement order, not destruction prevention.
Final Answer:
prevent_destroy -> Option D
Quick Check:
Destruction-time control = prevent_destroy [OK]
Hint: Use prevent_destroy to block resource deletion at destruction-time [OK]
Common Mistakes:
Choosing create_before_destroy which affects creation order
Confusing ignore_changes with destruction control
Using depends_on which manages resource dependencies
5. You want to replace a resource safely without downtime. Which lifecycle settings should you use to ensure the new resource is created before the old one is destroyed, and prevent accidental deletion?
hard
A. Set create_before_destroy = true and prevent_destroy = true
B. Set create_before_destroy = false and prevent_destroy = true
C. Set create_before_destroy = true and ignore_changes = [all]
D. Set prevent_destroy = false and ignore_changes = [all]
Solution
Step 1: Use create_before_destroy for safe replacement
This ensures the new resource is ready before the old one is removed, avoiding downtime.
Step 2: Use prevent_destroy to block accidental deletion
This stops Terraform from deleting the resource unless explicitly allowed, adding safety.
Final Answer:
Set create_before_destroy = true and prevent_destroy = true -> Option A