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.
More on Refactoring, Migration and Drift
What is your workflow for refactoring Terraform safely?IntermediateHow do you split one state into two without downtime?AdvancedHow do you merge two states?IntermediateHow do you import a large existing environment efficiently?IntermediateHow do you upgrade a major version of the AWS provider?AdvancedHow do you upgrade the Terraform CLI across many stacks?Intermediate