Replacing resources and replace_triggered_by
Force a rebuild with apply -replace, let one resource's change trigger another's rebuild with replace_triggered_by, and why taint is on the way out.
Lifecycle rules: create_before_destroy, prevent_destroy, ignore_changes
Replacing resources and replace_triggered_by
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- `terraform apply -replace=ADDRESS` plans a destroy and create of that one object, even though nothing in the code changed. It shows up in the plan like any other change, so it can be reviewed.
- `terraform taint` marks an object so the next apply replaces it. It is deprecated, and the docs recommend `-replace`, because taint changes state before anyone has seen a plan.
- `replace_triggered_by` lists managed resources or attributes. When one of them is updated or replaced, the resource that lists it is replaced too.
- It accepts only references to managed resources. For a plain value such as a variable or local, put it in `terraform_data` with `triggers_replace` and reference that.
- Replacement destroys data on resources that hold it. Read the plan for `-/+`, and pair replacement of stateful things with `prevent_destroy` or backups.
When nothing changed but the thing is wrong
A server is acting up and the cheapest fix is a new one. Or an auto-scaling group is configured to pull a launch template, and the template changes, but the running instances stay on the old version because Terraform sees no change in them. Both situations ask Terraform to replace something its normal diff says is fine. The tools for that are -replace, and the replace_triggered_by lifecycle argument.
Replace on request
terraform apply -replace="aws_instance.web" tells Terraform to plan that object as destroy-and-create regardless of what the diff says. The flag is part of the planning options, and it works with plan too, so you can preview the replacement before running it.
The older way was terraform taint aws_instance.web, which marks the object in state as bad, so the next apply replaces it. The docs now say the command is deprecated and recommend -replace for Terraform 0.15.2 and later. The reason is ordering. Taint changes state first, and the replacement happens whenever the next run happens, possibly from a teammate’s apply who did not know. With -replace the intent is inside one plan that you read and approve. Taint has not been removed, as some older videos and posts say. It still exists and is documented as deprecated.
I can’t run Terraform here. To show how the two modes appear in a plan, I wrote a toy planner. It is a Python stand-in, and the wording of its lines is mine, not Terraform’s:
security group updated in place:
-/+ aws_instance.web (because aws_security_group.web is planned to update)
(no change) aws_instance.batch
nothing changed upstream, apply -replace=aws_instance.batch:
(no change) aws_instance.web
-/+ aws_instance.batch (replace requested with -replace)
The first block is replace_triggered_by and the second is -replace. In both, a resource is destroyed and created, and the plan says why.
replace_triggered_by
Terraform added replace_triggered_by to the lifecycle block in 1.2. It lists references, and when any of them changes, this resource is replaced.
resource "aws_security_group" "web" {
name_prefix = "web-"
# ...
}
resource "aws_instance" "web" {
ami = var.ami_id
instance_type = "t3.small"
lifecycle {
replace_triggered_by = [aws_security_group.web]
}
}
That is written from the lifecycle docs. I did not run it. The docs define when a reference triggers a replacement:
- A reference to a resource with several instances triggers when any instance is updated or replaced.
- A reference to one instance triggers when a plan updates or replaces that instance.
- A reference to one attribute, such as
aws_launch_template.web.latest_version, triggers on any change to that attribute’s value.
If the resource itself uses count or for_each, you can use count.index or each.key to pair each instance with the matching instance of the resource it watches.
The docs add a limit that surprises people. Only managed resources are allowed, because the decision is based on planned actions for each reference, and variables and locals have none. To react to a plain value, use the built-in terraform_data resource. Its triggers_replace argument stores a value and replaces terraform_data itself when the value changes. Then watch that:
resource "terraform_data" "config_version" {
triggers_replace = var.config_version
}
resource "aws_instance" "web" {
# ...
lifecycle {
replace_triggered_by = [terraform_data.config_version]
}
}
This pattern replaced null_resource for most uses. terraform_data is built in to Terraform and needs no provider download. The lesson on utility providers covers it alongside random and time.
Before you replace something
Replacing is destroy and create, so anything stored on the old object goes. A database replaced this way loses its data, and an instance loses its local disk. Three habits help. Read the plan for -/+ and must be replaced. Put prevent_destroy on stateful resources, as in the previous lesson, so a replacement you did not intend fails. And for things that can briefly exist twice, add create_before_destroy, so the new one is up before the old one goes.
Also be careful about a resource that is replaced often. replace_triggered_by pointing at something that changes on every apply, such as a timestamp, rebuilds the resource every time. If a trigger is noisy, trace what is changing before you blame the watcher.
About the video
The embed is WORLD OF AWS’s “Terraform replace_triggered_by Explained | Real-World Example & Step-by-Step Tutorial”, published April 2025. I clipped it from 1:03 to 10:16, using the creator’s own timestamps: what replace_triggered_by is, a demo that creates a security group and instance, then updates the group and triggers a rebuild. The full video is 10 minutes 48 seconds and opens with a short lifecycle overview. It is a small channel with a few hundred views, and it covers the argument but not -replace, terraform_data or the single-attribute and count.index forms, which this lesson adds from the docs. English was judged from the title, description and English auto-caption track.
Where this fits
Replacement is the sharp edge of Terraform’s model. Many teams reach for scripts that run after creation instead. The next lesson looks at that habit: provisioners and why to avoid them.
Check yourself
A VM has been compromised and you want a fresh one, but the code has not changed. What do you run?
`terraform apply -replace=aws_instance.web`. The plan shows a destroy and create for that instance, you review it, and apply performs it.
Why did the docs move people from `terraform taint` to `-replace`?
`taint` writes a mark into state immediately, before any plan is shown, and another run may then replace the object. `-replace` is part of a plan, so it is visible and approved first.
You want an instance rebuilt whenever a `var.config_version` string changes. `replace_triggered_by = [var.config_version]` fails. Why, and what do you do?
The argument accepts only managed resources, which have planned actions. Wrap the value in a `terraform_data` resource with `triggers_replace = var.config_version`, and reference `terraform_data.config` in `replace_triggered_by`.
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.