One of my rule of thumb is never touch terraform state file if I want to live a happy life but recently I found myself having to separate a state file into 2. At first I was very irritated because I should've done this in the beginning but what happened, happened so lets deal with it.
With some quick online searching, to my surprised there are some terraform built-in command that can make my life easier.
Let says all the current terraform code are in foo/
and I would like to move module.bar
out of foo/
to bar/
and also rename the module to module.balloon
I want to know
- what do we current have
- get a backup copy of the current tfstate file just in case if things goes horribly wrong
NOTE another thing that I do to make sure I can save myself from disaster is to have versioning on for whatever backend storage I am using
cd foo
terraform init
terraform state list > tf_resource_list
terrafor state pull > ../bar/tf_state_$(date +%s).tfstate
terraform mv -state-out=../bar/bar.tfstate 'module.bar' 'module.balloon'
Add all the required terraform code to bar/
that match the bar.tfstate file
cd bar
terraform push bar.tfstate
To make sure the migration is sucessful
cd bar
terraform init
terraform plan # should see no changes
And to make sure the original state correspond to the current terraform state
cd foo
terraform init
terraform plan # should see no changes
https://www.terraform.io/docs/commands/state/mv.html