I work with Terraform a lot and sometimes with large legacy codebases that are hard to navigate and improve. In those cases, I want to refactor so that the code is actually structured like the infrastructure it defines, but Terraform makes that difficult. Its inherent statefulness makes moving a resource from one module to another quite tedious. Moving a lot of resources around is a lot of toil.
A few months back a colleague of mine had an idea: what if we could automatically move resources based on Terraform's plan? They implemented a quick and dirty tool that did just that for a specific project they were working on. And it worked! They saved a lot of time, reduced potential for human error and avoided a few headaches.
Another colleague and I decided to redesign the tool from scratch to make it more generic and rigorous (the original version sometimes made guesses). We have been using this new version where I work and it works well! I convinced my boss to make it open source, because I believe it can help many Terraform users who need to refactor their codebase.
Tfautomv's internal logic is provider-agnostic. Its only source of information is Terraform's plan. If Terraform plans to destroy a resource and create another with the same type and attributes, tfautomv considers this a match. If two resources match each other and only each other, then it writes the corresponding "moved" block. That is pretty much it.
I hope you will try it out. If you find any issues or have any ideas for additional features, let me know! All feedback is welcome :)
[+] [-] busser|3 years ago|reply
I work with Terraform a lot and sometimes with large legacy codebases that are hard to navigate and improve. In those cases, I want to refactor so that the code is actually structured like the infrastructure it defines, but Terraform makes that difficult. Its inherent statefulness makes moving a resource from one module to another quite tedious. Moving a lot of resources around is a lot of toil.
A few months back a colleague of mine had an idea: what if we could automatically move resources based on Terraform's plan? They implemented a quick and dirty tool that did just that for a specific project they were working on. And it worked! They saved a lot of time, reduced potential for human error and avoided a few headaches.
Another colleague and I decided to redesign the tool from scratch to make it more generic and rigorous (the original version sometimes made guesses). We have been using this new version where I work and it works well! I convinced my boss to make it open source, because I believe it can help many Terraform users who need to refactor their codebase.
Tfautomv's internal logic is provider-agnostic. Its only source of information is Terraform's plan. If Terraform plans to destroy a resource and create another with the same type and attributes, tfautomv considers this a match. If two resources match each other and only each other, then it writes the corresponding "moved" block. That is pretty much it.
I hope you will try it out. If you find any issues or have any ideas for additional features, let me know! All feedback is welcome :)