proof of concept: a tool to help refactoring Terraform resources
MIT License
"Terraform Move", a tool to help with Terraform refactoring. The use case: Let's say you have a resource:
// terraform/main.tf
resource "aws_instance" "app" {
// ...
}
If you change the resource name:
// "app" --> "webapp"
resource "aws_instance" "webapp" {
// ...
}
or move it into a module:
// terraform/main.tf --> terraform/shared/main.tf
resource "aws_instance" "app" {
// ...
}
the resource address is different, and Terraform treats it as the old resource
being deleted, and the new one being created. Why not reuse them? terraform state mv
allows you to update the addresses of resources by hand, but this can be tedious if you are moving more than a few resources.
In order:
state mv
by hand.Install the package.
go get github.com/afeld/tfmv
Create a plan.
cd <your module>
terraform plan -out=tfplan
Run the executable. It will compute state mv
commands to efficiently reuse resources.
$ tfmv
terraform state mv aws_instance.my_instance module.shared.aws_instance.my_instance
...
After double-checking the output, you can run the commands to avoid deleting and recreating resources unnecessarily.
Install Terraform.
Install the dependencies.
dep ensure
Run tests.
go test