Safely remediate Terraform infrastructure drift
When Terraform-managed infrastructure differs from its declared baseline, first decide whether the change was approved. Then choose whether to **restore the baseline, adopt the new configuration, or bring an unmanaged resource under Terraform management**. `terraform plan -refresh-only` lets you review differences that would be written to state without changing remote infrastructure. A normal plan may instead propose changes to make remote resources match code; applying it without review can undo manual work, replace resources, or delete them. ([HashiCorp: Manage resource drift](https://developer.hashicorp.com/terraform/tutorials/state/resource-drift), [Terraform plan](https://developer.hashicorp.com/terraform/cli/commands/plan))
Safely remediate Terraform infrastructure drift
When Terraform-managed infrastructure differs from its declared baseline, first decide whether the change was approved. Then choose whether to restore the baseline, adopt the new configuration, or bring an unmanaged resource under Terraform management. terraform plan -refresh-only lets you review differences that would be written to state without changing remote infrastructure. A normal plan may instead propose changes to make remote resources match code; applying it without review can undo manual work, replace resources, or delete them. (HashiCorp: Manage resource drift, Terraform plan)
This guide covers Terraform CLI remediation workflows for Terraform 0.15.4 or later, when the -refresh-only option became available. It focuses on the operator’s choice and on reconciling code, state, and live resources. Periodic HCP Terraform drift assessments, provider-specific discovery, and landing-zone baseline design are separate topics. (Terraform plan options)
What should you decide first when drift appears?
Drift means a managed resource’s real configuration differs from Terraform’s state or code. It may come from a console edit, another automation system, or an emergency change. Do not automatically “restore the original” just because a difference appears: it might be an approved emergency fix, evidence of a security incident, or a new resource Terraform does not manage yet.
Confirm the environment first: repository and branch, Terraform workspace, variable files, provider account or subscription, and Region. The wrong workspace or account can produce a large plan unrelated to the drift. Review change records, incident tickets, and resource ownership to identify who made the change and why.
How can you compare state safely?
Terraform state binds code addresses to remote objects and can contain sensitive values. When the backend supports it, Terraform automatically locks state during write operations; avoid concurrent runs and do not disable locking. Follow the backend’s backup and versioning process, or prepare a protected state backup in a secure location. Never commit state to a repository or edit the state file directly. (Terraform state, State locking)
With Terraform 0.15.4 or later, review proposed state-only updates with:
terraform plan -refresh-onlyTerraform reads remote values through the provider and shows how state and outputs would change. This command does not modify remote infrastructure. A normal terraform plan, by contrast, refreshes current values in memory and compares them with configuration; it may propose actions to bring remote resources back to code. A plan does not execute changes, so distinguish the two modes by their purpose. (Terraform refresh-only planning mode, Manage resource drift tutorial)
How do you choose a remediation path?
Suppose an operator manually changes an inbound rule on a Terraform-managed security group. Before undoing it, determine whether the security team approved an emergency change, whether the new rule is unsafe, or whether the change is now an approved long-term requirement.
| Finding | Direction | Terraform work |
|---|---|---|
| The change was not approved and the code remains correct | Restore remote infrastructure to the declared baseline | Review a normal-mode plan and apply only intended actions |
| The change was approved and should persist | Adopt it as the new baseline | Update HCL, then use a normal plan to reconcile code, state, and infrastructure |
| A valid resource exists but is absent from Terraform state | Bring it under code management | Plan the matching resource configuration and import workflow |
| Intent or ownership is unknown | Pause and investigate | Share or save the plan; do not apply before approval |
Applying a refresh-only plan after accepting a manual change updates state to match the real value, but it does not update Terraform code. If code remains at the old value, a later normal plan may try to change the resource again. For a lasting change, update HCL too and verify that a normal plan no longer proposes an unwanted remote change.
To manage a new, previously unmanaged resource, create a matching resource block and plan an import. Terraform 1.5 and later supports import blocks within the normal plan/apply workflow; the terraform import CLI command is another option. The provider resource type must support import, and do not bind the same remote object to multiple Terraform addresses. (Import existing resources, Import block, Terraform state bindings)
What sequence should you use to restore the baseline?
After deciding to revert the change, review it before making any remote changes:
**Check approval and security context.** If the change is part of incident response, preserve evidence and temporary controls; do not overwrite it without the incident owner’s decision.
**Confirm code and state context.** Verify workspace, provider scope, and the affected resource. Check for provider read errors or API issues that could look like drift.
**Create and review a normal plan.**
terraform plan -out=tfplan Review whether each resource will update, replace, be created, or be destroyed. Stop if a replacement (-/+) or deletion lacks a clear reason. A saved plan can contain configuration and sensitive planned values, so restrict access. (Terraform plan and saved plans)
4. Apply only the approved plan.
terraform apply tfplan**Verify the result.** Run `terraform plan` again to check for unexpected differences, then verify critical settings through the provider API or cloud console. If the result differs or apply fails, stop and investigate state locks, provider errors, and partial success. If another change occurred during review, create and review a fresh plan.
If the direction is unclear or the plan contains unexpected destroys or replacements, do not proceed merely to make the plan “clean.” Narrow the cause and split the work into smaller changes with separate approval. Terraform documents -target for exceptional recovery; routine use can leave other drift undetected. (Terraform plan targeting caveats)
What if remediation is blocked or the result is unexpected?
**The change keeps appearing:** Verify that HCL reflects the approved value and that the provider reads and plans the relevant attribute. Drift detection does not cover every provider attribute.
**A resource appears missing or a delete is planned:** Check account, Region, workspace, provider credentials, and read permissions before anything else. Do not apply immediately.
**The state lock cannot be acquired:** Check whether another run is active and wait. Do not bypass it with `-lock=false`. Use `force-unlock` only cautiously, and only for your own lock after automatic unlock failed. ([Terraform state locking](https://developer.hashicorp.com/terraform/language/state/locking))
**You need to restore state:** Follow the backend’s version-recovery and team procedure. Do not edit state by hand or push an older state blindly; overwriting remote state can cause data loss. ([Terraform state backends](https://developer.hashicorp.com/terraform/language/state/backends))
Drift remediation is not an automatic exercise in forcing state to match code. Confirm the change’s intent, choose which of code, state, and remote infrastructure is the approved baseline, review the plan, and verify the outcome.
Official documentation
[HashiCorp: Manage resource drift](https://developer.hashicorp.com/terraform/tutorials/state/resource-drift)
[Terraform plan command](https://developer.hashicorp.com/terraform/cli/commands/plan)
[Sync state with refresh-only mode](https://developer.hashicorp.com/terraform/tutorials/state/refresh)
[Terraform state and sensitive values](https://developer.hashicorp.com/terraform/language/state)
[Terraform state locking](https://developer.hashicorp.com/terraform/language/state/locking)
[Terraform backends and state storage](https://developer.hashicorp.com/terraform/language/state/backends)
[Import existing Terraform resources](https://developer.hashicorp.com/terraform/cli/import)
[Terraform import block](https://developer.hashicorp.com/terraform/language/block/import)