Recent versions of terraform have come out with a `moved`[1] block, but it is really unintelligent. It is a step in the right direction, but I can't use variables with it or string interpolation or anything.
Trying to get the state to be happy has been very difficult.
I prefer ansible for this because it doesn't use state[2], but as my company uses terraform I am left with the unfortunate need to manage state.
The best thing that I've been able to find to help in this fight is a little script I created to help automatically make moved blocks[3]. In a refactor you typically want to take resource blocks from the main.tf file and move them into their own module. You first moved the blocks into the module, and then before you edit anything you run that script and it matches up the blocks that were moved using Levenshtein distance. This approach may seem hacky but it works very well. Then I just take the output of the script and append it to my tf file.
I found the article refreshing and poignant. I will definitely be hoping for innovations like this to finally improve The state of the art because now that OpenToFu[4] is on the scene features like this that might chew into the bottom line of HashiCorp will start finally start being looked at seriously.
1: https://developer.hashicorp.com/terraform/language/modules/d...
2: https://blog.djha.skin/blog/that-does-it-ansible-wins/
3: https://gist.github.com/djhaskin987/b856c157bcfce6d2e4ee19f9...