Q: You need to move a Terraform resource from one module to another without destroying and recreating it. How?
Use terraform state mv:
#Terraform #Import & Migrations #L3 #IaC #Cloud Infrastructure #S3
🎙️ Candidate Opening & Architectural Context
""Treat Terraform code with the same rigor as application code: pre-merge plans, state locks, and automated drift detection. When addressing this question, I walk the interviewer through our production incident runbook: isolating the blast radius, checking diagnostic logs and metrics, and applying a safe fix.""
Advertisement
🛠️ Production Runbook & Step-by-Step Resolution
1️⃣
Production Solution & Architecture
Use terraform state mv: Then update the configuration (move the resource block to the new module). Run terraform plan — should show no changes if the state move was done correctly. Terraform 1.1+ moved blocks — the modern approach, tracked as code: This is self-documenting and can be committed to Git. --- ## 🟡 Workspaces & CI/CD
# Move from root to a module
terraform state mv aws_s3_bucket.my_bucket module.storage.aws_s3_bucket.my_bucket
# Move between modules
terraform state mv module.old.aws_s3_bucket.bucket module.new.aws_s3_bucket.bucket
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Pro-Tip: Use terraform state mv:."
⚡ 60-Second Elevator Pitch Talking Points
- Immediate Triage: Use terraform state mv:
- Run targeted verification commands before modifying configuration.
- Automate permanent guardrails (CI check, alerts, IaC policy) to prevent recurrence.
Advertisement