What should you do if someone mistakenly deletes the Terraform state file?
💡 Model Answer
The Terraform state file is the source of truth for your infrastructure. If it is deleted, you should first check for backups. If you use remote state (S3, Terraform Cloud, etc.), the state is stored there and can be restored with terraform state pull or by re‑initializing the backend. If you only have local state, look for a .terraform.tfstate.backup file that Terraform creates automatically on each apply; you can rename it back to .tfstate. If no backup exists, you can use terraform import to re‑import existing resources into a new state file, but this requires knowing the resource IDs and may be time‑consuming. To prevent future loss, enable remote state with encryption and versioning, and enforce state locking (e.g., DynamoDB for S3). Additionally, store the state file in source control only if it contains no secrets, or use a dedicated state management service. Finally, run terraform plan to verify that the new state matches the real infrastructure before applying any changes.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500