Migrate Terraform State

Abhishek Amralkar
1 min readJan 28, 2024

--

Recently I encountered a weird situation where my Terraform module got changed, earlier I was booting VPC and Security Groups creation together and keeping the state for the same in a single state file.

With the newer version of the module, we decided to split the VPC and Security Groups into their module and each of them should have a state file to speed up the terraform bootstrap. But this is a breaking change for my existing environment as the module update will destroy the security groups. I decided to hack the state file manually and split the modules but it's a tedious process and may corrupt the state file.

One of my colleagues introduced me to terraform migrate and it's the easiest way to migrate a terraform state

cd vpc
terraform state pull > vpc.tfstate

touch sg.tfstate

terraform state mv -state=vpc.tfstate -state-out=sg.tfstate module.sg module.sg

terraform state push sg.tfstate

# verify state was moved
terraform state list | grep sg

--

--

Abhishek Amralkar
Abhishek Amralkar

No responses yet