Ignore changes and replacement triggers
Assign ownership before ignoring changes
Terraform normally plans to restore managed attributes to the desired configuration when it detects differences. Sometimes another authorized system owns one attribute, such as a maintenance label. ignore_changes changes which differences Terraform considers for updates; it still uses configured values on creation. The following local model uses an owner field to illustrate selective ownership. No actual external system edits this resource.
variable "owner_label" { type = string default = "catalog-team"}
resource "terraform_data" "record" { input = { service = "catalog" owner = var.owner_label }
lifecycle { ignore_changes = [input["owner"]] }}
output "owner" { value = terraform_data.record.output.owner}After the initial creation with catalog-team, changing owner_label to platform-team should leave the existing owner unchanged during a normal update plan, because the path input["owner"] is excluded. It is a synthetic configuration change, not an observed external drift event. If the record is later replaced, the new configured value can be used during creation. Check the entire plan and actual output in a real test. Never manually edit state to manufacture drift.
Choose the narrowest exclusion
An exclusion must name an attribute or supported indexed path in the resource schema. An entire-map exclusion can hide unexpected changes alongside the intended label. Consider this illustrative fragment for a fictional provider that exposes tags and encryption; it cannot be run without a matching schema.
lifecycle { ignore_changes = [tags["maintenance-owner"]]}Keep encryption and access policy under explicit management and independently monitor them. Ignoring all attributes with all gives up ordinary update reconciliation for the resource; it is rarely appropriate. Neither form disables external modification, refresh or deletion. Document the external owner, the reason, the monitoring method and the conditions for removing the exclusion. If two tools need to manage the same attribute, resolve ownership before adding a rule.
Trigger replacement when another managed resource changes
replace_triggered_by follows planned changes to referenced managed resources, instances or attributes. A plain variable has no lifecycle action of its own. In this complete fixture, a terraform_data resource gives a revision value a managed lifecycle.
variable "revision" { type = string default = "v1"}
resource "terraform_data" "revision" { input = var.revision}
resource "terraform_data" "release" { input = "catalog"
lifecycle { replace_triggered_by = [terraform_data.revision] }}Create the initial revision v1; then plan with v2. The revision record should update in place, and the release record should be replaced even though its input stays catalog. Referencing a whole multi-instance resource may trigger more replacements than intended; when collections match, count.index or each.key can select the corresponding instance. For a single local resource, triggers_replace = var.revision is a simpler alternative if no cross-resource rule is needed. A trigger does not guarantee application health or atomic rollout.
Check assumptions and resulting values
The lifecycle block also supports conditions with their own expression rules. A precondition checks an assumption before proceeding; a postcondition checks a result and can stop dependent operations. Values unknown during planning may defer evaluation until apply. This local example permits only test and staging and checks the resulting stored label.
variable "environment" { type = string default = "test"}
resource "terraform_data" "record" { input = "catalog-${var.environment}"
lifecycle { precondition { condition = contains(["test", "staging"], var.environment) error_message = "Choose test or staging for this lab." } postcondition { condition = self.output == "catalog-${var.environment}" error_message = "The stored catalog label must match the requested environment." } }}The default environment should pass. A plan with environment=production should reject the precondition. A postcondition is not a transaction rollback: resources already changed before a later failed condition may remain changed. Use independent verification and a recovery plan. The next chapter covers validation, conditions and non-blocking checks in detail.
Distinguish provider actions from replacement rules
Terraform 1.14 introduced the provider action workflow documented by HashiCorp. A lifecycle action_trigger can invoke a configured provider action on events such as after_create or before_update, with an optional condition. This is a separate capability from replacing a resource. It requires a provider that actually implements the action, its schema and an approved environment; effects can occur outside the managed resource. No action block is supplied in this local lab because no approved action-capable provider is available. Review its side effects and version support before using it. In this batch, exact patch compatibility and execution remain unverified.
Practice ownership and trigger review
For the synthetic catalog service, an external maintenance system owns one label, Terraform owns encryption, and a release revision change requires replacing the release record. Choose a narrow exclusion and a managed revision trigger, then explain which plan actions you expect. Review why a changed label should not conceal an encryption change. Use the supplied cases and answer key; they teach reasoning rather than simulate a provider API.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Ignore changes and replacement triggers — knowledge check
Review replacement ordering, ownership and lifecycle safeguards.
When is ignore_changes considered?
Which exclusion best limits ignored changes to a maintenance label?
Why put a revision in terraform_data for replace_triggered_by?
What can happen when a postcondition fails after changes were made?
What additional requirement does a provider action trigger have?