Interview Prep/Terraform/Refactoring, Migration and Drift

How do you rename a resource inside a shared module without breaking consumers?

AdvancedProductionRefactoring, Migration and Drift
Answer

Add a moved block inside the module so each consumer's state migrates on its next apply.

moved {
  from = aws_security_group.this
  to   = aws_security_group.main
}

Moved blocks inside a module apply to every instance of it, including for_each keys. Keep them for at least a couple of releases because consumers may skip versions, and document the release as a minor with a migration note.

All Terraform questions Roadmap: Terraform in production

More on Refactoring, Migration and Drift