⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Terraform & IaC Interview Questions Scenario 59 of 120 in Terraform & IaC
Senior DevOps / SRE [L2] Terraform Terraform Modules & State Production Scenario [L2]
🎯 Target Role / Context: Terraform Scenario #54 (L2) · Use VPC ID from another module

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:

#Terraform #L2 #IaC #Cloud Infrastructure #EC2
🎙️ Candidate Opening & Architectural Context
"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."
Advertisement
⚡ Recommended Practice Lab

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.

1️⃣

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_by takes 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_version accordingly before adopting it in shared modules.
Pro Tip: Replacement means destroy and create. For stateful resources such as databases this can mean data loss, so use it deliberately.
2️⃣

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_data and 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.
Advertisement
3️⃣

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_by for 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.
4️⃣

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 fixed name argument collides because both copies exist briefly. Use name_prefix or 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_destroy are 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.
5️⃣

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 = true to 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.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Reference a specific attribute such as <code>latest_version</code> instead of the whole resource in <code>replace_triggered_by</code>, so only the change you care about forces replacement."
⚡ 60-Second Elevator Pitch Talking Points
  • <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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. Pointing at a whole frequently-updated resource Every update to it, even a tag change, replaces your resource; reference one attribute.
  2. Referencing a data source or variable directly Triggers must be managed resources or their attributes; route values through <code>terraform_data</code>.
  3. 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.
  4. 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>.
Advertisement
Want more Terraform & IaC scenarios?
Explore our complete collection of scenario-based Terraform & IaC interview runbooks.
Browse All Terraform & IaC Questions →