📖 Scenario: You are managing cloud infrastructure using Terraform. You want to update a resource safely without downtime by creating the new resource before destroying the old one.
🎯 Goal: Learn how to use the create_before_destroy lifecycle rule in Terraform to ensure resources are replaced safely.
📋 What You'll Learn
Create a Terraform resource block for an AWS S3 bucket named exactly my_bucket with bucket name my-unique-bucket-12345.
Add a lifecycle block with create_before_destroy = true inside the resource.
Use the exact resource type aws_s3_bucket and resource name my_bucket.
💡 Why This Matters
🌍 Real World
Using create_before_destroy helps avoid downtime when updating resources that cannot be replaced instantly, like S3 buckets or databases.
💼 Career
Cloud engineers and DevOps professionals use lifecycle rules in Terraform to manage infrastructure changes safely and reliably.
Progress0 / 4 steps
1
Create the AWS S3 bucket resource
Create a Terraform resource block named my_bucket of type aws_s3_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 inside.
2
Add the lifecycle block
Add a lifecycle block inside the my_bucket resource.
Terraform
Hint
Inside the resource block, add lifecycle { } on separate lines.
3
Set create_before_destroy to true
Inside the lifecycle block, add the line create_before_destroy = true.
Terraform
Hint
Inside the lifecycle block, write create_before_destroy = true exactly.
4
Complete the resource with lifecycle rule
Ensure the full resource block for my_bucket includes the bucket attribute and the lifecycle block with create_before_destroy = true.
Terraform
Hint
Check that the resource block is complete with all required parts.
Practice
(1/5)
1. What is the main purpose of the create_before_destroy lifecycle rule in Terraform?
easy
A. To create a new resource before deleting the old one to avoid downtime
B. To destroy resources before creating new ones to save costs
C. To prevent any resource from being created or destroyed
D. To automatically backup resources before deletion
Solution
Step 1: Understand lifecycle behavior
The create_before_destroy rule tells Terraform to create the replacement resource first.
Step 2: Avoid downtime during updates
This ensures the old resource is only destroyed after the new one is ready, preventing service interruption.
Final Answer:
To create a new resource before deleting the old one to avoid downtime -> Option A
Quick Check:
create_before_destroy = avoid downtime [OK]
Hint: Remember: create new first, then delete old [OK]
Common Mistakes:
Thinking it deletes old resource before creating new
Confusing with prevent_destroy lifecycle rule
Assuming it backs up resources automatically
2. Which of the following is the correct syntax to enable create_before_destroy in a Terraform resource?
easy
A. lifecycle { create_before_destroy = true }
B. lifecycle { create_before_destroy: true }
C. lifecycle = { create_before_destroy = true }
D. lifecycle { create_before_destroy = "true" }
Solution
Step 1: Review Terraform lifecycle block syntax
The lifecycle block uses curly braces with key-value pairs using = and booleans without quotes.
Step 2: Identify correct boolean assignment
Boolean values are unquoted, so true is correct, not "true" as string.
Final Answer:
lifecycle { create_before_destroy = true } -> Option A
Quick Check:
Correct syntax uses = and unquoted booleans [OK]
Hint: Use = and true without quotes in lifecycle block [OK]
Common Mistakes:
Using colon : instead of equals =
Putting boolean true in quotes
Assigning lifecycle block with = instead of braces
3. Given this Terraform resource snippet, what will happen during an update?
A. Terraform will destroy the old instance before creating the new one
B. Terraform will fail because create_before_destroy is not supported
C. Terraform will update the instance in place without replacement
D. Terraform will create the new instance before destroying the old one
Solution
Step 1: Check lifecycle rule effect
The create_before_destroy = true means Terraform creates the new resource first.
Step 2: Understand resource replacement
Since the instance requires replacement, Terraform creates new instance before destroying old one.
Final Answer:
Terraform will create the new instance before destroying the old one -> Option D
Quick Check:
create_before_destroy = new before old [OK]
Hint: If replacement needed, new resource comes first [OK]
Common Mistakes:
Assuming old resource is destroyed first
Thinking update happens in place always
Believing create_before_destroy is unsupported
4. You added create_before_destroy = true to a resource but Terraform still destroys the old resource before creating the new one. What is the likely cause?
medium
A. You used quotes around true in lifecycle block
B. The resource does not support create_before_destroy lifecycle rule
C. You forgot to run terraform init
D. Terraform always destroys before creating regardless of lifecycle
Solution
Step 1: Understand resource support
Not all Terraform resources support create_before_destroy. If unsupported, Terraform ignores it.
Step 2: Check lifecycle behavior fallback
If unsupported, Terraform defaults to destroying old resource before creating new one.
Final Answer:
The resource does not support create_before_destroy lifecycle rule -> Option B
5. You manage a critical database server resource that must never be unavailable during updates. How should you configure Terraform to ensure zero downtime using create_before_destroy?
hard
A. Remove lifecycle block and rely on default behavior
B. Use prevent_destroy = true to stop any deletion
C. Add lifecycle { create_before_destroy = true } and verify the resource supports it
D. Manually delete old resource before applying changes
Solution
Step 1: Use create_before_destroy for zero downtime
Setting create_before_destroy = true ensures new resource is ready before old is removed.
Step 2: Confirm resource supports lifecycle rule
Check documentation to ensure the database resource supports this lifecycle feature to avoid unexpected downtime.
Final Answer:
Add lifecycle { create_before_destroy = true } and verify the resource supports it -> Option C