minamijoyo / tfmigrate

A Terraform / OpenTofu state migration tool for GitOps
MIT License
1.14k stars 57 forks source link

Migrating states from codebases with different terraform version #125

Closed kishaningithub closed 1 year ago

kishaningithub commented 1 year ago

Hi, Currently have a use case similar to this where we are trying to move from a monolithic repo/state into smaller broken micro repositories/state

but the catch here is the monolithic repo is in an older version of terraform(0.13.x) and the new repo is in the latest terraform version(1.3.x)

The installed terraform exposed via TFMIGRATE_EXEC_PATH is the new terraform 1.3.x, due to this when the terraform plan runs in the old repo there are a lot of errors (for example map() function was removed and replaced with tomap() function etc)

Is there a way in the multi_state mode that we can specify two different terraform versions and ensure a smooth migration?

minamijoyo commented 1 year ago

I haven’t tried moving resources between versions before, but as TFMIGRATE_EXEC_PATH can be injected with any wrapper script, switching versions depending on the directory may be possible, as long as the tfstate format is compatible between versions.

However, Terraform v0.13.x to Terraform v1.3.x is too far away to be compatible with tfstate. The only way to do this seems to split them somewhere once with v0.13 as it is, take the upgrade steps, and merge them into the final destination.

kishaningithub commented 1 year ago

Thanks for the response @minamijoyo closing this issue

mdb commented 1 year ago

@kishaningithub While I think @minamijoyo 's state compatibility concerns are valid, I did create a demo reference example illustrating how to automatically switch the Terraform versions used by from_dir and and to_dir using tfenv and .terraform-version files under the hood: https://github.com/mdb/tfmigrate-demo

Perhaps that's helpful and gives you ideas?