Lifecycle rules: create_before_destroy, prevent_destroy, ignore_changes
The lifecycle block changes how Terraform replaces, protects and ignores things. What each rule does, what it cannot stop, and a replace timeline run for real.
Dependencies, implicit and explicitReading a plan and the dependency graph
Lifecycle rules: create_before_destroy, prevent_destroy, ignore_changes
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- By default a change that cannot be made in place destroys the old object, then creates the new one. `create_before_destroy` flips that order, and it only works if the old and new objects can exist at once, so unique names need a suffix or prefix.
- `create_before_destroy` spreads to the resources a resource depends on. You cannot turn it off on a dependency, because that would create a cycle.
- `prevent_destroy` makes plans that would destroy the object fail. It does not stop you deleting the resource block, because the rule disappears with the block.
- `ignore_changes` is for attributes something else legitimately owns, like tags added by an agent. Terraform still uses them at create time and ignores them on update. `ignore_changes = all` means Terraform never updates the object.
- All lifecycle arguments take literal values. You cannot drive them from a variable or expression.
Three ways a plan can hurt
A plan that replaces a resource is not a bug. Some arguments cannot change on a live object, so Terraform deletes it and builds another. The lesson on reading a plan taught you to spot the -/+ marker. That leaves three problems. The replacement leaves a gap with nothing serving traffic. Someone could delete a database with one careless apply. And a tool outside Terraform keeps changing a field, so every plan fights it. The lifecycle block is how you tell Terraform about each of these.
create_before_destroy
By default, replacement is destroy then create. The docs describe this as the behaviour when an argument cannot be updated in place because of remote API limits. For a server behind a load balancer, that means a gap.
I wrote a toy cloud to see the order. It enforces unique names and counts what is serving. It is a Python stand-in, not Terraform, and the log lines are my own.
default (destroy, then create), new name web-v2:
destroy web-v1
serving now: []
create web-v2
serving now: ['web-v2']
create_before_destroy, new name web-v2:
create web-v2
serving now: ['web-v1', 'web-v2']
destroy web-v1
serving now: ['web-v2']
create_before_destroy, same fixed name web-v1:
create web-v1: ERROR name already exists
The first run has an empty serving set in the middle. The second never does. The third is why the rule is opt-in: when the name is fixed, creating the new object first collides with the old one. The docs say many remote object types have unique-name constraints, and that some resource types offer an option to append a random suffix, such as a name_prefix argument. Terraform can’t turn those on for you. You have to know the resource.
resource "aws_launch_template" "web" {
name_prefix = "web-"
image_id = var.ami_id
instance_type = "t3.small"
lifecycle {
create_before_destroy = true
}
}
That block is written from the provider and Terraform docs. I did not run it, since there is no Terraform or AWS account on this machine.
One more rule from the docs. Terraform applies create_before_destroy to a resource’s dependencies too. If resource A has it and depends on B, B gets it implicitly and Terraform stores that in state, so you can’t set it to false on B without creating a dependency cycle. A destroy-time provisioner won’t run on a resource with the rule, per the docs.
prevent_destroy
prevent_destroy = true makes Terraform reject any plan that would destroy the object, with an error. It guards against a replacement you did not expect and against terraform destroy, which also fails. The docs say to use it sparingly, because it also blocks legitimate teardown of temporary environments.
Its limit matters more. The docs state that it does not stop destruction if you delete the resource’s configuration, because lifecycle settings other than create_before_destroy are not recorded in state. Remove the block, apply, and the object goes, with the rule gone along with it. So it is a guard against accidents in a plan, not against a person deleting code. Pair it with review of destroy lines in plans, versioned buckets for state, and backups of the data. For a way to take a resource out of Terraform without deleting it, the docs point to removing it from state, and the moved and removed blocks do that in code.
ignore_changes
By default any difference between the real object and your code becomes a planned update. That is right until something else legitimately owns an attribute: an autoscaler sets the instance count, an agent adds tags, a rotation job changes a password field.
resource "aws_autoscaling_group" "web" {
# ...
lifecycle {
ignore_changes = [desired_capacity]
}
}
The docs say Terraform considers these arguments when planning a create, and ignores them when planning an update. Attributes are given by relative address, with index notation for map and list elements, such as tags["Name"]. ignore_changes = all makes Terraform ignore every attribute, so it can create and destroy the object but never update it. That is a heavy tool, and I would rather list the one field.
This is also the usual answer to drift you decided to live with, from the last lesson in the state group. Ignoring a field hides drift on that field. Keep the list short and add a comment saying who owns it.
Literals only
The docs note that lifecycle settings affect how Terraform builds and walks the dependency graph, so only literal values are allowed. You cannot write prevent_destroy = var.is_prod. People hit this and reach for workarounds like duplicating blocks. OpenTofu 1.11 added an enabled meta-argument, nested in lifecycle, as an alternative to count for zero-or-one instances. It is an OpenTofu feature, not a Terraform one, and I read it in OpenTofu’s changelog, not tried it.
About the video
The embed is “How to use the terraform lifecycle block to protect your resources” from LetMeTechYou, published October 2022, 14 minutes. Its description says it covers create_before_destroy, prevent_destroy and ignore_changes, the three in this lesson. It is a small channel, and its description lists only those three rules, so replace_triggered_by is left to the next lesson. The Piyush video in deeper is a 32 minute course episode from January 2025 that covers those plus replace_triggered_by and custom conditions, and is based on Azure. English was judged from the title, description and the English auto-caption track.
Where this fits
The rules above mostly shape how Terraform replaces things. Next, how to force or trigger a replacement on purpose: replacing resources and replace_triggered_by.
Check yourself
Why is `create_before_destroy` not the default for every resource?
Many objects have unique names or other limits that stop two from existing at once. The new one would fail to create while the old one is still there. Terraform can't know which resource types are safe.
You set `prevent_destroy = true` on a database and later delete the whole `resource` block. What happens on apply?
Terraform destroys the database. The rule is part of the block you removed, and it is not saved in state, so nothing is left to block the destroy. A plan review is the real safeguard.
An autoscaler changes `desired_capacity` on your group all day. What stops Terraform from resetting it on every apply?
`lifecycle { ignore_changes = [desired_capacity] }`. Terraform still sets it when the group is created and then ignores differences when planning updates.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.