terraform apply, and the state file is stored remotely (for example, in an S3 bucket). Configuration, state, and cloud resources are all in sync.


terraform plan or terraform apply makes Terraform behave as if it’s starting from scratch.

- Restore the state file
- If you use a remote backend (for example, an Amazon S3 bucket), check for backups or enablement of versioning. Restoring a prior
terraform.tfstateis often the fastest way to recover. - Restore the backup/version of
terraform.tfstateinto the remote backend so Terraform can pick it up.

Enable remote backend versioning and periodic backups (for example, S3 versioning or GCS object versioning). Store copies of
terraform.tfstate so state restoration is straightforward when needed.- Re-import existing resources into state
If you cannot restore a prior state file, re-associate real resources with your Terraform configuration using
terraform import(official docs: https://developer.hashicorp.com/terraform/cli/commands/import). The workflow:
- Ensure your Terraform configuration contains resource blocks that match the resources you want to manage. Create or update resource blocks first so each real resource has a corresponding address.
- Use
terraform importto map each existing cloud resource into the matching resource block address.
aws_instance.web:
count or for_each, import using the indexed address:
Import is a per-resource operation and can be time-consuming for large fleets. Always run
terraform plan after imports and review planned changes before terraform apply to avoid unintended modifications or replacements.- Add resource blocks to your configuration before importing so you have deterministic addresses to import into.
- Import each resource (or each indexed element) individually; there’s no bulk-import native to Terraform core.
- Use
terraform state listandterraform state showafter importing to inspect entries and verify attributes. - When possible, prefer reconciling configuration to actual resource attributes rather than changing resources to match configuration.
- A configuration-first approach (define resources in code, then import real resources into those blocks) restores state while preserving a version-controlled source of truth. This avoids adhoc fixes and helps teams understand intended infrastructure.
| Recovery method | When to use | Notes |
|---|---|---|
| Restore state file | You have backups or backend versioning | Fastest, simplest; restores exact prior state |
| Re-import resources | No backups available | Safe but manual; ensure config matches real resources |
| Recreate resources | When resources are disposable or cheap | Risky for production—can cause duplicates or lost data |
- Losing Terraform state causes Terraform to treat existing resources as absent and plan to recreate them.
- Recovery approaches:
- Restore the state file from backend versioning or backups (recommended if available).
- Re-import existing resources into properly defined resource blocks using
terraform import.
- Best practices to avoid or mitigate state loss:
- Use remote backends (S3, GCS, Terraform Cloud).
- Enable object/version backups.
- Store Terraform configuration in source control.
- Periodically back up state snapshots and test recovery procedures.