liveBook · Manning

Replace_triggered_by

As explained in Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu

replace_triggered_by is a Terraform lifecycle setting that causes a resource to be replaced when specified resource attributes or referenced resources change.

replace_triggered_by is a Terraform lifecycle setting that forces a resource replacement when selected attributes or referenced resources change. section 2.7.2

Choose the trigger scope deliberately:

  • Reference a specific attribute when only that attribute should trigger replacement. section 2.7.2
  • Reference a resource itself when any change to that resource should trigger replacement. section 2.7.2
  • A changed aws_instance instance_type normally updates the existing machine in place; replace_triggered_by can require a new instance instead. section 2.7.2

Replacing an instance when its type changes

A null_resource records instance_type through its triggers map. The aws_instance then lists that null_resource in replace_triggered_by, coupling the variable change to instance replacement.

How Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu covers it

How deep it goes
Defines attribute-specific and resource-wide triggers, then demonstrates replacement versus in-place updating.
Examples
Uses an aws_instance, instance_type variable, and null_resource triggers map to demonstrate replacement coupling.
What you can do after
Configure Terraform to replace an aws_instance when instance_type changes instead of updating it in place.
How it connects
Connects Terraform lifecycle with ignore_changes, prevent_destroy, null provider triggers, and random provider keepers.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Terraform in Depth: Infrastructure as Code with Terraform and OpenTofu. The section links point to the parts of the book it draws on.