Skip to main content
Terraform state is the mapping Terraform maintains between resource blocks in your configuration and the real-world infrastructure it manages. Losing access to that state file breaks Terraform’s ability to map configuration to existing resources. This article walks through that failure mode and practical recovery paths. Scenario: five VMs under Terraform management
  • You have a Terraform configuration that creates five virtual machines (VMs).
  • You run terraform plan and terraform apply. Terraform writes a state file (for example, to an Amazon S3 backend with versioning).
  • At this point, your configuration, the state file, and the cloud infrastructure (five VMs) are in sync.
What happens when Terraform loses the state file?
  • If Terraform cannot access the state file but your configuration files remain unchanged on disk, Terraform has no record linking the configured resources to the existing VMs.
  • Running terraform plan in this situation causes Terraform to assume those resources don’t exist and it will plan to create them again — resulting in duplicate VMs when you run terraform apply.
A presenter is drawing a cloud architecture on a lightboard, showing a boxed set of servers, an S3 component crossed out, and a larger diagram labeled "CLOUD." The words "terraform plan" are written beneath the sketches.
What you might run right away
Recovery options (overview) Restore a backup of the state file
  • If you use a remote backend such as Amazon S3 with versioning enabled, restoring a previous version of the state file is the fastest and safest approach.
  • Steps:
    1. Identify the correct state file version in your backend (S3, Terraform Cloud snapshots, etc.).
    2. Restore/overwrite the current state file with that version in the backend.
    3. Run terraform init (if required by backend changes) and terraform plan to confirm the state matches your configuration.
Recreate state by importing existing resources
  • Use terraform import to adopt existing infrastructure into the state. terraform import links an existing resource ID with a resource address defined in your configuration — it updates state only and does not modify HCL.
  • Important: the corresponding resource block must already exist in your configuration, and count/for_each usage and module addresses must match the resource addresses you plan to import.
Example: counted aws_instance resources
Import the first instance:
Repeat for the remaining instances (aws_instance.web[1], aws_instance.web[2], etc.) with their provider-specific IDs. After importing all instances, run:
to ensure the imported state aligns with your configuration. Use the terraform state subcommands for advanced repairs
  • For targeted state operations (move objects between addresses, remove broken entries, list state objects) use terraform state subcommands:
    • terraform state list
    • terraform state mv
    • terraform state rm
  • These commands are powerful and can easily leave state inconsistent if used incorrectly. Always:
    1. Back up the state file before making changes.
    2. Test changes in a safe environment if possible.
    3. Validate with terraform plan after modifications.
Always back up your state file before performing terraform state operations or manual edits. Incorrect state manipulation can lead to resource drift or accidental destruction when you later run terraform apply.
Why importing matters
  • If you do nothing and run terraform apply without state, Terraform will create resources to match the configuration, producing duplicates.
  • terraform import tells Terraform which existing cloud resource corresponds to which resource address in your configuration so Terraform can manage them from that point forward.
  • After successful imports, terraform plan should show zero changes (unless your HCL differs from actual settings).
Before running terraform import, ensure the corresponding resource block exists in your configuration. terraform import maps an existing resource ID into a resource address defined in your HCL.
Summary
  • Losing the state file causes Terraform to “forget” the resources it manages; terraform plan / terraform apply will then try to recreate configured resources.
  • Best recovery paths:
    1. Restore the state file from a backup or remote backend versioning.
    2. Reconstruct state by ensuring matching resource blocks and using terraform import.
  • Use terraform state subcommands for surgical fixes, but only after backing up state and validating changes.
  • Always verify results with terraform plan before applying changes to avoid accidental duplication or deletion.
Links and references

Watch Video