"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:
- Improve algorithm for resource matching.
- Currently it's just matching created resources with destroyed ones of the same type, in the order it comes across them. It could be better at guessing created/destroyed resources that are likely meant to be the same.
- Gain enough confidence in its functionality that it can be used in deployment pipelines, where it's hard to do
state mv
by hand. - Propose merging into Terraform core.
-
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