Interview Prep/Real-World Interview/Infrastructure as Code & Terraform state

How did you refactor Terraform code without destroying resources?

IntermediateBehavioralInfrastructure as Code & Terraform state
Answer

Good answer looks like → "When moving resources into a module, I used moved blocks so Terraform updated state addresses instead of destroy and create. I ran plan to confirm zero changes before merging. For moving resources between states, I used terraform state rm in one and an import block in the other."

moved {
  from = aws_security_group.app
  to   = module.app.aws_security_group.this
}

Follow-ups to expect → When would you use state mv instead? How did you coordinate with others during the refactor?

Red flags → Accepting a plan with destroys during a "refactor".

All Real-World Interview questions Roadmap: Interview readiness

More on Infrastructure as Code & Terraform state