https://github.com/afeld/tfmv
proof of concept: a tool to help refactoring Terraform resources
https://github.com/afeld/tfmv
Last synced: about 1 year ago
JSON representation
proof of concept: a tool to help refactoring Terraform resources
- Host: GitHub
- URL: https://github.com/afeld/tfmv
- Owner: afeld
- License: mit
- Created: 2017-11-20T04:33:26.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2020-04-15T16:07:29.000Z (about 6 years ago)
- Last Synced: 2025-03-30T12:11:51.801Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 43 KB
- Stars: 11
- Watchers: 4
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: changes.go
- License: LICENSE.md
Awesome Lists containing this project
README
# tfmv [](https://circleci.com/gh/afeld/tfmv)
"Terraform Move", a tool to help with Terraform refactoring. The use case: Let's say you have a resource:
```hcl
// terraform/main.tf
resource "aws_instance" "app" {
// ...
}
```
If you change the resource name:
```hcl
// "app" --> "webapp"
resource "aws_instance" "webapp" {
// ...
}
```
or move it into a module:
```hcl
// terraform/main.tf --> terraform/shared/main.tf
resource "aws_instance" "app" {
// ...
}
```
the [resource address](https://www.terraform.io/docs/internals/resource-addressing.html) 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`](https://www.terraform.io/docs/commands/state/mv.html) allows you to update the addresses of resources by hand, but this can be tedious if you are moving more than a few resources.
### Goals
In order:
1. 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.
1. Gain enough confidence in its functionality that it can be used in deployment pipelines, where it's hard to do `state mv` by hand.
1. Propose merging into Terraform core.
## Setup
1. [Install Go 1.6+.](https://golang.org/doc/install)
1. Install the package.
```sh
go get github.com/afeld/tfmv
```
## Usage
1. Create a plan.
```sh
cd
terraform plan -out=tfplan
```
1. 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
...
```
1. After double-checking the output, you can run the commands to avoid deleting and recreating resources unnecessarily.
## See also
- [Google Groups discussion](https://groups.google.com/forum/#!topic/terraform-tool/CE2ScmDBTIE)
- [GitHub issue about resource equivalence maps](https://github.com/hashicorp/terraform/issues/9048)
## Development
1. Install Terraform.
1. [Install dep.](https://golang.github.io/dep/docs/installation.html)
1. Install the dependencies.
```sh
dep ensure
```
1. Run tests.
```sh
go test
```