Q: What is the `replace_triggered_by` lifecycle argument?
Forces resource replacement when another resource changes. Example: replace EC2 instance whenever the launch template changes:
Want to master this scenario in a live sandbox? KodeKloud's HashiCorp Certified Terraform Associate (003) Interactive Labs covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: The <strong><code>replace_triggered_by</code> lifecycle argument</strong>, available from Terraform 1.2, tells Terraform to destroy and recreate a resource whenever another referenced resource or attribute changes. It solves cases where a dependency changes but the dependent object has no argument that changes, so Terraform would otherwise plan nothing. Interviewers ask it to check that you understand the dependency graph, can distinguish it from <code>-replace</code>, <code>taint</code> and <code>create_before_destroy</code>, and know the blast radius of automatic replacement.
Mental model: what replace_triggered_by does
By default Terraform only updates a resource when its own arguments differ. This argument adds an extra reason to replace it.
- Trigger list:
replace_triggered_bytakes a list of references to managed resources or their attributes. - Resource reference: pointing at a whole resource (
aws_launch_template.app) replaces your resource if that resource is updated or replaced. - Attribute reference: pointing at an attribute (
aws_launch_template.app.latest_version) triggers only when that attribute changes, which is more precise. - Scope: references must be to managed resources, or to attributes of them, not to data sources or input variables; to react to a variable, route it through
terraform_data. - Why it exists: before Terraform 1.2 teams used null_resource tricks or manual taint commands to force this behaviour. The lifecycle argument makes the rule explicit, reviewable and part of the dependency graph.
- Version note: it requires Terraform 1.2 or newer, so set
required_versionaccordingly before adopting it in shared modules.
Hands-on: replace_triggered_by in a real configuration
A common use is rolling a resource that consumes a changed object without its own diff, such as an instance reading a rendered config.
variable "rollout_id" { type = string }
resource "terraform_data" "rollout" {
input = var.rollout_id # change this to force a roll
}
resource "aws_instance" "app" {
ami = data.aws_ami.al2023.id
instance_type = "t3.small"
user_data = file("${path.module}/boot.sh")
lifecycle {
replace_triggered_by = [
terraform_data.rollout, # any change replaces the instance
aws_iam_instance_profile.app.name,
]
}
}
- Dependent resource: an EC2 instance should be rebuilt when its configuration object changes, because user data is only read at first boot.
- Variable-driven trigger: wrap a version string in
terraform_dataand reference it, so bumping the variable forces replacement. - Plan output: the plan marks the resource as replaced and notes the reason, so reviewers can see which trigger fired. Always read this line in review.
- Count and for_each: triggers can reference indexed instances, but be explicit about which instance you mean to avoid replacing an entire set.
Compare with -replace, taint and create_before_destroy
Interviews often hinge on knowing which tool expresses a permanent rule versus a one-off action.
# one-off, imperative
terraform apply -replace="aws_instance.app"
# preview what a trigger would do
terraform plan -out=tfplan
terraform show tfplan | grep -B2 -A2 "replaced"
- replace_triggered_by: a declarative, permanent rule stored in code. Every future change to the trigger replaces the resource.
- terraform apply -replace=ADDR: a one-time imperative action for a single run. It is recorded in the plan, and is the modern replacement for the deprecated
terraform taint. - create_before_destroy: controls the order of replacement, not whether it happens. Combine it with
replace_triggered_byfor zero-gap swaps when names allow two copies to exist. - ignore_changes: the opposite intent, suppressing diffs on an attribute. Do not use both on the same attribute without a reason.
Failure modes and edge cases
Automatic replacement is powerful, which is exactly why the failure modes matter.
- Cascading replacements: if the trigger is a whole resource that changes frequently, your resource is replaced on every tag or minor update. Prefer a specific attribute.
- Name collisions: with
create_before_destroy, a fixednameargument collides because both copies exist briefly. Usename_prefixor generated names. - Cycles: a trigger cannot create a dependency loop. Terraform reports a cycle if the target also depends on the resource.
- Stateful data: replacing a database, volume or queue loses data unless snapshots, retention or
prevent_destroyare in place. - Chained triggers: if A triggers B and B triggers C, one change replaces all three. Draw the chain before merging and confirm the plan lists exactly the replacements you expect.
Production practice and review
Use it for immutable-infrastructure patterns, and gate the blast radius.
- Immutable patterns: roll instances, ECS task roles or Lambda aliases when an upstream template or config changes and the resource has no changing argument.
- Guardrails: add
prevent_destroy = trueto stateful resources; a trigger that would replace them then fails the plan instead of destroying data. - Review: require plan output in the pull request so replacements caused by triggers are visible before merge.
- Document the trigger: add a comment explaining why the dependency forces replacement, so the next engineer does not remove it as unused.
- <code>replace_triggered_by</code> is a lifecycle argument from Terraform 1.2 that replaces a resource when a referenced resource or attribute changes.
- I check the plan output to confirm which trigger fired, and use <code>terraform_data</code> to turn a variable into a trigger.
- I pair it with <code>prevent_destroy</code> on stateful resources and use <code>-replace</code> for one-off needs.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Pointing at a whole frequently-updated resource Every update to it, even a tag change, replaces your resource; reference one attribute.
- Referencing a data source or variable directly Triggers must be managed resources or their attributes; route values through <code>terraform_data</code>.
- Using it on a database without snapshots or <code>prevent_destroy</code> A trigger silently turns a config change into destroy and recreate, losing data.
- Combining <code>create_before_destroy</code> with a fixed resource name Two copies must coexist during replacement, so the name collides; use <code>name_prefix</code>.