Terraform migrate local state to remote

Terraform migrate local state to remote

Terraform is very good tool and if you use it at local you quickly see the limite of local state and you will need to go to remote but if you doesn’t wan to remove all your prevouis terraform plan you can migrate manualy your state.

For the first time, copy your terraform state (by default the terraform state saved on your root directory to file with name terraform.tfstate) or use this command to backup all your local state

terraform state pull | tee backup_out.tfstate

After this you can go to you remote terraform folder and get the remote conf. This will be need to empty.

joel@pc-admin:~/$ terraform state pull
{
"version": 4,
"terraform_version": "1.1.9",
"serial": 87,
"lineage": "30j440e5-863a-96b1-1b68-11rhe23f2e7b",
"outputs": {},
"resources": []
}

save the value of « lineage » and serial variable.

Now you need to copie you prevouis state file (backup_out.tfstate or terraform.tfstate depending of you copie default file or generate new file) to root path of you terraform remote state folder.

Edit the new copie of your state file and change lineage value with the last remote value, for serial value append one and put the result to local serial value. you will have this

joel@pc-admin:~/$ head backup_out.tfstate
{
"version": 4,
"terraform_version": "1.1.9",
"serial": 88,
"lineage": "30j440e5-863a-96b1-1b68-11rhe23f2e7b",
"outputs": {},
"resources": [
{

Now, save the new fille and run the command.

joel@pc-admin:~/$  terraform state push ./backup_out.tfstate

if you don’t have output error everything is right. you can make terraform plan or terraform state list to check your terraform state.

joel

Laisser un commentaire

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.