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".
More on Infrastructure as Code & Terraform state
What did your Terraform state setup look like?IntermediateHow did you structure your Terraform code?BeginnerWhy did you use folders per environment rather than Terraform workspaces (or vice versa)?IntermediateHow did different Terraform stacks share information?IntermediateDid you ever have a state lock or corrupted state? What did you do?AdvancedHow did you handle Terraform drift?Intermediate