State is gone and the bucket had no versioning. How do you recover a production stack?
AdvancedTroubleshootingLost, Corrupted and Drifted State
Answer
Likely causes → Unversioned bucket, local state on a lost laptop, or a CI workspace that used local state.
Diagnose → Search for any copy first: terraform.tfstate.backup files, CI artifacts, old laptops, HCP state history. Inventory real resources by tag:
aws resourcegroupstaggingapi get-resources --tag-filters Key=stack,Values=prod-app \
--query 'ResourceTagMappingList[].ResourceARN'
Fix → Rebuild state with import blocks (Terraform 1.5+), one per resource, in a fresh empty state:
import {
to = aws_vpc.main
id = "vpc-0a1b2c3d4e5f67890"
}
Iterate terraform plan until it shows 0 to add, 0 to change, 0 to destroy, fixing code where it differs.
Prevent → Versioned, KMS-encrypted remote state with restricted delete, plus consistent tagging (stack, env, managed-by=terraform) so an inventory is possible.
More on Lost, Corrupted and Drifted State
Someone deleted the state object from the S3 bucket. `terraform plan` now wants to create 140 resources that already exist.Intermediate`Error: Failed to load state` followed by a JSON error like `invalid character '}' looking for beginning of object key string`.IntermediateThe plan prints `Note: Objects have changed outside of Terraform` and lists a security group with an extra ingress rule.BeginnerEvery plan wants to change `desired_capacity` on an Auto Scaling group back to 2, though autoscaling has raised it to 6.BeginnerA resource was deleted manually in the console. The plan wants to create it, and an apply fails because a dependent resource still references the old ID.Intermediate`terraform plan -refresh-only` shows drift, and you apply it. The next normal plan still wants to revert the change. Why?Intermediate