When you’re about to refactor a big codebase of terraform to make it more modular and DRY, what things do you keep in mind to make sure you don’t mess up the state?
I found this 2017 article that tackles this issue. The summary is: use tf state mv. I wonder if this is still the state of the art?
I guess it could involve steps around drift management?
Refactoring existing states required careful planning. Test carefully, lest you accidentally destroy something you didn’t want to.
Have you read about the
moved blocks introduced in more recent Terraform versions?
They can make simpler refactoring easier, by codifying resource renames to happen at the right time, without manual scripting.
terraform state mv and
terraform state rm may be needed.
For some more complex refactoring edge cases, it may even be necessary to manually manipulate the state JSON, but usually that’s only to work around implementation bugs in a Terraform provider.