An operator runs 'terraform plan' and sees that a resource will be replaced. They want to avoid destroying the resource, but still apply other changes. What should they do?
Trap 1: Use 'terraform apply -replace=resource_address' to replace only…
Using `terraform apply -replace=resource_address` explicitly forces Terraform to destroy and then recreate the specified resource, regardless of whether the current plan requires it. This command is designed to initiate a replacement, which is the direct opposite of the operator's objective to avoid destroying the resource. Therefore, this action would actively cause the very outcome the operator is trying to prevent, making it an incorrect approach.
Trap 2: Add a 'lifecycle' block with 'create_before_destroy = true'.
Adding a `lifecycle` block with `create_before_destroy = true` instructs Terraform to create the new version of a resource before destroying the old one when a replacement is necessary. While this minimizes downtime during a disruptive change, it explicitly still performs both a creation and a destruction operation. The operator's goal is to *avoid* destroying the resource entirely, not merely to reorder the creation and destruction steps, making this option unsuitable.
Trap 3: Add 'prevent_destroy = true' to the resource.
This prevents any destroy operation on the resource, but if a replacement is required (due to a change that cannot be applied in-place), the apply will fail, preventing other changes from being applied. Therefore, it does not allow the operator to apply other changes.
- A
Use 'terraform apply -replace=resource_address' to replace only that resource.
Why wrong: Using `terraform apply -replace=resource_address` explicitly forces Terraform to destroy and then recreate the specified resource, regardless of whether the current plan requires it. This command is designed to initiate a replacement, which is the direct opposite of the operator's objective to avoid destroying the resource. Therefore, this action would actively cause the very outcome the operator is trying to prevent, making it an incorrect approach.
- B
Add a 'lifecycle' block with 'create_before_destroy = true'.
Why wrong: Adding a `lifecycle` block with `create_before_destroy = true` instructs Terraform to create the new version of a resource before destroying the old one when a replacement is necessary. While this minimizes downtime during a disruptive change, it explicitly still performs both a creation and a destruction operation. The operator's goal is to *avoid* destroying the resource entirely, not merely to reorder the creation and destruction steps, making this option unsuitable.
- C
Set 'ignore_changes' to the attribute causing the replacement.
By setting `ignore_changes` within the resource's `lifecycle` block for the specific attribute causing the replacement, Terraform is instructed to disregard any future changes to that attribute. This prevents Terraform from planning a destruction and recreation of the resource based on that particular attribute's drift or configuration change. Consequently, the resource remains untouched, and other unrelated changes in the configuration can still be applied successfully without impacting the resource in question.
- D
Add 'prevent_destroy = true' to the resource.
Why wrong: This prevents any destroy operation on the resource, but if a replacement is required (due to a change that cannot be applied in-place), the apply will fail, preventing other changes from being applied. Therefore, it does not allow the operator to apply other changes.