Interview Prep/Terraform/Import, Already Exists and State Surgery

A teammate edited the state JSON to change a resource ID, pushed it, and now refresh fails for that resource with a provider error.

AdvancedTroubleshootingImport, Already Exists and State Surgery
Answer

Likely causes → Only id was changed; other attributes (ARN, nested IDs) still reference the old object, or schema_version / private provider data no longer match, so the provider can't read it.

Diagnose → terraform state show <addr>; compare id with arn and other identifiers.

Fix → Remove and re-import cleanly:

terraform state rm aws_lb.main
terraform import aws_lb.main arn:aws:elasticloadbalancing:ap-south-1:111122223333:loadbalancer/app/prod/abc123

Prevent → No manual state edits; reviewers should reject PRs or runbooks that suggest them.

All Terraform questions Roadmap: Terraform in production

More on Import, Already Exists and State Surgery