Moving Terraform resource from one remote state to another (with different versions)

Featured image

The task

I need to move Terraform resources from a v11 to a newer (up-to-date, v12) backend.

Limitations

  • some resources couldn’t be recreated (destroyed);
  • some resources couldn’t be imported.

The basic concepts

When we move resources to a new backend we usually use this formula:

$ cd <NEW_BACKEND_FOLDER>
$ terraform state pull > moving_terraform.tfstate
$ cd <ACTUAL_BACKEND_FOLDER>
$ terraform state mv -state-out=<NEW_BACKEND_FOLDER>/moving_terraform.tfstate module.some_name module.some_name
$ cd <NEW_BACKEND_FOLDER>
$ terraform state push moving_terraform.tfstate

Issues

  • state mv from v11 backend to v12 isn’t supported:
    Failed to load state: Terraform 0.11.8 does not support state version 4, please update.
    
  • all docs I found only refer state mv between backends using the same version.

The solution

$ cd <ACTUAL_BACKEND_FOLDER>
$ terraform state pull > moving_terraform_v11.tfstate
$ cd <NEW_BACKEND_FOLDER>
$ terraform state pull > moving_terraform_v12.tfstate
$ terraform state mv -state <ACTUAL_BACKEND_FOLDER>/moving_terraform_v11.tfstate -state-out=<NEW_BACKEND_FOLDER>/moving_terraform_v12.tfstate module.some_name module.some_name
$ terraform state push moving_terraform_v12.tfstate
$ cd <ACTUAL_BACKEND_FOLDER>
$ terraform state rm module.some_name

Done!

Relax and grab a coffee…

Photo on Visual Hunt