What if your cloud resources could build themselves up and clean themselves away without you lifting a finger?
Creation-time vs destruction-time in Terraform - When to Use Which
Imagine you are setting up a new cloud server by manually clicking buttons to create it, and later you have to remember to delete it by clicking again when it's no longer needed.
This manual way is slow and easy to forget. You might leave unused servers running, wasting money, or accidentally delete the wrong one because you lost track of what was created.
Using creation-time vs destruction-time in Terraform means you define what happens when resources are made and what happens when they are removed, all in one place. Terraform handles these steps automatically and safely.
Click 'Create Server' in cloud console Remember to click 'Delete Server' later
resource "aws_instance" "example" { # creation-time setup } # destruction-time handled automatically by Terraform
This lets you manage cloud resources reliably and automatically, avoiding mistakes and saving time.
When launching a website, Terraform creates the servers and databases at creation-time, and when you no longer need the site, it safely removes all resources at destruction-time without extra clicks.
Manual cloud setup is slow and error-prone.
Creation-time vs destruction-time automates setup and cleanup.
Terraform manages resource lifecycle safely and efficiently.
Practice
creation-time mean in Terraform?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 AQuick Check:
Creation-time = resource setup [OK]
- Confusing creation-time with destruction-time
- Thinking creation-time is when resources are deleted
- Mixing creation-time with Terraform plan execution
Solution
Step 1: Identify destruction-time control arguments
Theprevent_destroyargument stops Terraform from deleting a resource.Step 2: Compare other lifecycle arguments
create_before_destroyaffects creation and replacement order, not destruction prevention.Final Answer:
prevent_destroy -> Option DQuick Check:
Destruction-time control = prevent_destroy [OK]
- Choosing create_before_destroy which affects creation order
- Confusing ignore_changes with destruction control
- Using depends_on which manages resource dependencies
resource "aws_instance" "example" {
ami = "ami-123456"
instance_type = "t2.micro"
lifecycle {
create_before_destroy = true
}
}What happens when you change
instance_type from t2.micro to t2.small?Solution
Step 1: Understand create_before_destroy effect
This setting makes Terraform create the new resource before destroying the old one.Step 2: Apply to instance_type change
Changing instance_type forces replacement, so new instance is created first, then old destroyed.Final Answer:
Terraform creates the new instance first, then destroys the old one -> Option CQuick Check:
create_before_destroy = new before old [OK]
- Assuming in-place update without replacement
- Thinking old resource is destroyed before new is created
- Believing lifecycle causes errors on changes
prevent_destroy = true to a resource but Terraform still deletes it on terraform destroy. What is the likely cause?Solution
Step 1: Check prevent_destroy usage
It must be inside thelifecycleblock and spelled correctly.Step 2: Understand terraform destroy behavior
terraform destroyrespectsprevent_destroyand will error if deletion is blocked.Final Answer:
The prevent_destroy argument is misspelled or misplaced -> Option BQuick Check:
Misplaced prevent_destroy causes deletion despite protection [OK]
- Assuming terraform destroy ignores prevent_destroy
- Placing prevent_destroy outside lifecycle block
- Thinking depends_on affects prevent_destroy behavior
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 AQuick Check:
Safe replacement = create_before_destroy + prevent_destroy [OK]
- Setting prevent_destroy without create_before_destroy causes downtime
- Using ignore_changes to avoid destruction which doesn't prevent deletion
- Disabling prevent_destroy risks accidental resource loss
Ready to prove it?
Pass the test and this pattern moves unknown
