What if you could stop accidental cloud outages by telling Terraform exactly how to handle resource changes?
Why Lifecycle customization in Terraform? - Purpose & Use Cases
Imagine you manage cloud resources by hand, deleting or changing them one by one whenever something needs updating.
Sometimes, deleting a resource too soon breaks others that depend on it, causing outages.
Manually tracking dependencies and update order is slow and confusing.
You might accidentally delete something important or update in the wrong order, causing errors and downtime.
Lifecycle customization lets you tell Terraform exactly how to handle resource creation, update, and deletion.
This means you can protect important resources, control update order, and avoid accidental breaks automatically.
resource "aws_instance" "web" { # no lifecycle rules }
resource "aws_instance" "web" { lifecycle { prevent_destroy = true } }
It enables safe, predictable management of cloud resources by customizing how and when they change.
For example, you can prevent your database server from being destroyed accidentally while updating your web servers.
Manual cloud changes risk breaking dependencies and causing downtime.
Lifecycle customization lets you control resource update and deletion behavior.
This makes infrastructure changes safer and more reliable.
Practice
create_before_destroy lifecycle setting do in Terraform?Solution
Step 1: Understand the purpose of
This setting ensures Terraform creates the replacement resource first.create_before_destroyStep 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 DQuick Check:
create_before_destroy = create new before delete [OK]
- Confusing create_before_destroy with prevent_destroy
- Thinking it deletes first then creates
- Assuming it ignores changes
Solution
Step 1: Identify the correct lifecycle argument to prevent destruction
The argument isprevent_destroyset to true.Step 2: Verify syntax correctness
The syntax islifecycle { prevent_destroy = true }, which is valid Terraform code.Final Answer:
lifecycle { prevent_destroy = true } -> Option BQuick Check:
prevent_destroy = true prevents deletion [OK]
- Using ignore_changes instead of prevent_destroy
- Using a non-existent destroy_protection argument
- Confusing create_before_destroy with prevent_destroy
resource "aws_instance" "example" {
ami = "ami-123456"
instance_type = "t2.micro"
lifecycle {
ignore_changes = ["tags"]
}
}
What happens if you manually change the tags of this instance outside Terraform and then run terraform apply?Solution
Step 1: Understand the effect of ignore_changes on tags
The lifecycle settingignore_changes = ["tags"]tells Terraform to ignore any changes to tags made outside Terraform.Step 2: Predict Terraform behavior on apply
Terraform will not attempt to update tags, so manual changes remain intact without error or replacement.Final Answer:
Terraform will ignore the tag changes and keep the manual tags as is. -> Option AQuick Check:
ignore_changes on tags means manual tag changes stay [OK]
- Assuming Terraform overwrites manual tag changes
- Expecting an error on tag mismatch
- Thinking resource will be recreated
lifecycle {
create_before_destroy = true
prevent_destroy = true
}
What problem will occur if you try to update this resource that requires replacement?Solution
Step 1: Analyze interaction of create_before_destroy and prevent_destroy
create_before_destroy tries to create new resource before deleting old one, but prevent_destroy forbids deletion.Step 2: Understand Terraform behavior on replacement
Terraform cannot destroy the old resource due to prevent_destroy, so replacement fails.Final Answer:
Terraform will fail because it cannot destroy the old resource due to prevent_destroy. -> Option CQuick Check:
prevent_destroy blocks deletion, causing failure with create_before_destroy [OK]
- Assuming Terraform ignores prevent_destroy
- Thinking resource updates without replacement
- Believing create_before_destroy overrides prevent_destroy
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
Settingignore_changes = ["tags"]tells Terraform not to replace the bucket when tags change.Final Answer:
Useignore_changes = ["tags"]to skip tag updates. -> Option AQuick Check:
ignore_changes on tags prevents replacement on tag updates [OK]
- Using prevent_destroy which blocks deletion but not replacement
- Using create_before_destroy which affects replacement order
- Ignoring unrelated attributes like versioning
Ready to prove it?
Pass the test and this pattern moves unknown
