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

`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.

All Terraform questions Roadmap: Terraform in production

More on Import, Already Exists and State Surgery