`terraform state rm module.legacy` was run in the wrong workspace (prod instead of staging).
AdvancedTroubleshootingImport, Already Exists and State Surgery
Answer
Likely causes → Wrong workspace selected, or TF_WORKSPACE set in the shell.
Diagnose → terraform workspace show; terraform plan in prod now shows module.legacy resources to be created.
Fix → Do not apply. Restore the previous state version from S3 versioning (env:/prod/... key), or re-import every removed resource. With a backup:
terraform state push -force prod-before-rm.json
Prevent → Take terraform state pull > backup-$(date +%s).json before any state command, and show the workspace in your shell prompt.
More on Import, Already Exists and State Surgery
Apply fails with `EntityAlreadyExists: Role with name app-role already exists.`Beginner`terraform apply` on a new bucket fails with `BucketAlreadyExists`. You have never created that bucket.Beginner`Error: Resource already managed by Terraform` when running `terraform import aws_s3_bucket.logs acme-logs`.Beginner`Error: Cannot import non-existent remote object` for a resource you can see in the console.Intermediate`Error: Configuration for import target does not exist` when running `terraform plan` with an `import` block.Beginner`terraform plan -generate-config-out=generated.tf` fails with `Conflicting configuration arguments` in the generated file.Intermediate