Replacement and lifecycle rules
Understand replacement before changing lifecycle
A resource update modifies an existing managed object; a replacement removes its old instance and creates a new one at the same configuration address. This difference matters for data, availability and cost. In the complete local example below, changing revision requests replacement of a synthetic record, not a server. The built-in terraform_data resource needs no external provider. Its input can ordinarily change in place, while triggers_replace deliberately changes identity when its value changes.
variable "revision" { type = string default = "v1"}
resource "terraform_data" "release" { input = "catalog" triggers_replace = var.revision}In a fresh fixture, first establish revision v1. A later plan with revision v2 should propose replacement, normally shown as -/+ (destroy then create). The identifier is expected to change only after a successful replacement. This example does not model actual service downtime. A real provider decides which attributes can be updated in place; read the plan rather than assuming every change replaces a resource.
Compare plan order and request replacement explicitly
Use these commands only in a dedicated local copy with the baseline v1 already applied. They request plans, not changes.
terraform plan -var="revision=v2"terraform plan -replace=terraform_data.releaseThe first requests a configuration change; the second explicitly requests replacement even with unchanged configuration. A replacement contributes to both the add and destroy totals in the plan summary. Inspect the resource address, action order and reason, not just totals. Never approve a replacement solely because the new object looks equivalent. For a database, check backup and tested restore; for an application, check traffic switching and readiness. Saved plans may contain sensitive values and must not be shared.
Create the new object before removing the old one
The following independent configuration reverses the normal replacement order.
variable "revision" { type = string default = "v1"}
resource "terraform_data" "release" { input = "catalog" triggers_replace = var.revision lifecycle { create_before_destroy = true }}With v1 already applied, a v2 plan should show +/− (CLI notation +/-, create then destroy). Temporary coexistence requires enough quota, capacity and distinct identifiers where names must be unique. For a fictional service with one allowed fixed name, creating another instance first may be impossible; changing names and designing a controlled handover may be necessary. Terraform can propagate create_before_destroy to dependencies and persist that behavior in state. It is not a readiness gate or a promise of zero downtime. Check the full plan, including dependencies, and independently verify application traffic and health.
Prevent accidental destruction without assuming permanent protection
For a valuable object, a lifecycle guard can make an otherwise destructive plan fail. This independent local fixture shows the guard.
variable "revision" { type = string default = "v1"}
resource "terraform_data" "release" { input = "catalog" triggers_replace = var.revision lifecycle { prevent_destroy = true }}After creating v1, a plan for v2 or a destroy plan should fail because a replacement includes deletion. However, removing the entire resource declaration also removes the guard; a normal plan can then propose deletion of the object remaining in state. The guard cannot prevent deletion outside Terraform or substitute for backups, permissions and recovery tests. Do not remove the guard just to make a production plan pass. Review whether the intended change can be redesigned, and obtain authorization for any destructive operation.
Review the lifecycle decision
A lifecycle rule influences the graph and therefore some settings must be statically known; for example, use a literal boolean for create_before_destroy, not var.use_safe_order. The individual condition rules and replacement references have their own expression forms, covered next. In the synthetic cases worksheet, classify updates, both replacement orders and blocked actions. Explain what evidence you need before approving a real change. All expected CLI behavior here is documentation-based, not an observed run.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Replacement and lifecycle rules — knowledge check
Review replacement ordering, ownership and lifecycle safeguards.
What distinguishes a replacement from an in-place update?
What must be checked before creating a replacement first?
What does prevent_destroy do when an active guard conflicts with a replacement?
What happens if the entire guarded resource declaration is removed?
What does create_before_destroy guarantee by itself?