Terraform state is the record that connects your configuration to real infrastructure. This guide provides a reusable checklist for remote backends, state locking, drift detection, imports, resource moves, failed applies, and safe recovery—so you can investigate state changes without making an incident worse.
Overview
Terraform uses state to track resource identities, provider attributes, and the relationship between your configuration and deployed infrastructure. A plan is only as reliable as the state and configuration used to produce it. If state is missing, stale, shared incorrectly, or changed without review, Terraform may propose unexpected replacements, duplicate resources, or destructive actions.
Safe Terraform state management starts with four controls:
- Use a remote backend when more than one person or automation process manages the same infrastructure.
- Enable state locking where the selected backend supports it, so concurrent operations do not overwrite each other.
- Review plans carefully, especially changes involving replacement, deletion, or identity-related attributes.
- Keep recovery options available, including backend versioning or snapshots, controlled access, and documented restoration steps.
State is not a general-purpose backup of every cloud setting. It should be treated as sensitive infrastructure data because it can contain resource details and, depending on provider behavior and configuration, sensitive values. Restrict access to the backend and avoid copying state into tickets, chat messages, or public repositories.
For related delivery controls, use this CI/CD pipeline best practices checklist to add plan review, approval, and controlled apply steps to your workflow.
Checklist by scenario
Before adopting a remote backend
- Choose a backend that fits your team’s access controls, recovery features, and automation model.
- Separate state by environment, account, region, or service boundary where that reduces blast radius and makes ownership clear.
- Configure encryption and least-privilege access according to your cloud platform and organization requirements.
- Confirm whether the backend provides locking, state history, object versioning, or an equivalent recovery mechanism.
- Document who can read, write, unlock, migrate, and restore state.
- Test initialization and recovery in a non-production workspace before migrating important state.
After configuring the backend, run terraform init and review any migration prompt carefully. Do not automatically accept a state move simply because initialization succeeds. Confirm the source and destination, take a backup when appropriate, and make sure no other Terraform operation is running.
When state is locked
- Check whether another plan or apply is genuinely running.
- Inspect the lock information supplied by the backend, such as the operation, workspace, user, and timestamp.
- Check CI/CD jobs, shell sessions, and scheduled automation before taking action.
- If the operation is active, wait for it or stop it through the normal process.
- If the lock is stale and you have verified that no operation is running, use the backend’s documented unlock procedure.
terraform force-unlock can remove a lock for backends that support the command, but it should not be used as a routine fix. Removing a live lock can allow two writers to operate on the same state. Record why an unlock was necessary and preserve relevant logs.
When Terraform reports drift
- Run
terraform planwithout applying the result. - Identify whether the difference was caused by a manual cloud-console change, an external automation process, a provider update, a changed default, or a resource replacement.
- Decide whether the desired outcome is represented by configuration or by the real infrastructure.
- If the real infrastructure is correct, update configuration and review the resulting plan.
- If configuration is correct, plan the change needed to bring the infrastructure back into alignment.
- Use
terraform plan -refresh-onlywhen you need to inspect or record refreshed values without proposing normal configuration changes. Review the output before applying any refresh-only operation.
Drift detection is not a substitute for ownership. If another system is allowed to modify Terraform-managed resources, document that boundary or expect recurring differences.
When a resource was created outside Terraform
- Write the resource block and required provider configuration first.
- Confirm that the address you will import matches the intended module and resource address.
- Use the provider’s supported import workflow, which may be a command such as
terraform importor a configuration-driven import supported by your Terraform version. - Run a plan after import and populate configuration for attributes Terraform expects.
- Do not assume that an import alone produces complete, maintainable configuration.
When renaming or moving resources
If a resource is being renamed or moved between modules without changing the underlying object, use a reviewed moved block or an equivalent state-move workflow supported by your Terraform version. This tells Terraform that the address changed while the object identity should remain. Verify the plan shows a move rather than a destroy-and-create sequence.
When an apply fails
- Read the provider error and identify which resources completed, failed, or may have partially changed.
- Do not immediately rerun apply without understanding the failure and checking the state.
- Run a fresh plan after the failed operation, using the same workspace and backend.
- Check the cloud provider directly when the error suggests eventual consistency, permissions, quotas, or an asynchronous operation.
- Correct the underlying issue, then apply the smallest reviewed change possible.
- If state appears damaged or a resource is missing from state, stop and take a state backup before editing anything.
What to double-check
Use this review list before approving a state-changing operation:
- Workspace and backend: Confirm the account, subscription, project, region, workspace, and backend key. A correct plan against the wrong environment is still dangerous.
- Provider and credentials: Verify the provider configuration and identity used by local Terraform or CI. Different credentials can produce confusing reads or permission errors.
- Plan actions: Search for
-/+, replacement indicators, deletes, changes to networking, identity, encryption, or persistent storage. - Variable inputs: Confirm variable files, environment variables, secrets references, and command-line overrides.
- Concurrency: Check that no second pipeline, scheduled job, or engineer is planning or applying against the same state.
- Provider behavior: A provider or schema change can alter refresh results. Review release notes and test upgrades in a non-production workspace.
- Backup and auditability: Confirm that state history or a manual backup is available before migration, bulk import, state surgery, or recovery.
For production, separate planning from applying where practical. Store plan artifacts securely, require an appropriate review, and ensure the apply uses inputs consistent with the reviewed plan. This complements broader infrastructure and container controls, such as the Docker image optimization checklist.
Common mistakes
Editing the state file by hand
Manual JSON edits can create subtle address, lineage, or provider inconsistencies. Prefer configuration changes, import workflows, moved blocks, and documented Terraform state commands. If direct editing is unavoidable, create a verified backup and perform the work in a controlled environment.
Deleting state to “start fresh”
Removing state does not remove cloud resources. It makes Terraform forget them, which can lead to duplicate creation attempts or unmanaged infrastructure. Recover or reconcile the state instead.
Running unlock commands without investigation
A lock is a safety control, not an error to bypass automatically. Confirm the original operation has stopped and capture evidence before unlocking.
Using terraform state rm as a repair shortcut
Removing an address from state makes Terraform stop managing that object; it does not safely fix drift. Use it only when the ownership decision is intentional, documented, and followed by a plan.
Ignoring sensitive state access
Do not expose state in logs or commit it to source control. Review backend permissions, CI secrets, local file handling, and who can download historical versions.
When to revisit
Revisit this checklist before seasonal planning cycles, environment migrations, major provider or Terraform upgrades, backend changes, and onboarding of a new platform team. It should also be part of the response checklist for a failed apply, unexplained drift, a stale lock, an unexpected replacement plan, or a suspected state-file problem.
As a practical maintenance routine, schedule a quarterly review of backend access, state separation, recovery procedures, CI concurrency, and recent drift events. Run a recovery exercise in a non-production workspace so the team knows how to locate state history, identify the correct version, restore safely, and validate the next plan. When workflows or tools change, update the commands and approval steps in your runbook rather than relying on memory.
Before the next production apply, ask three final questions: Am I using the correct state? Do I understand every destructive action in the plan? Can I recover if the operation fails? If any answer is unclear, pause the apply and resolve that uncertainty first.