I have some Terraform being managed and its IaC deployed via a CI/CD pipeline in GitLab. I currently store the Terraform state in GitLab as well as all other related environments.
However, I have recently run into an issue that is requiring me to change the name of my GitLab managed Terraform state file. How can I modify the name of my state file without having to destroy and/or redeploy infrastructure already managed by the old state file?
I haven't managed to find any resources online indicating how to do so without letting infrastructure go down (which is what I'm trying to avoid).
You can migrate the state, but as you'll be doing so within the same project, it'll just be the same as if you have renamed it.
The steps for this process look as follows:
terraform init
with the-migrate-state
with the new name of the state so it will be copied. You'll have to respond withyes
on the terminal for the process to kick in.Having run the above two commands, you can navigate to
Operate > Terraform States
and double-check that a new state has been created according to the value of$NEW_STATE_NAME
. And from now on if your CI variable$TF_STATE_NAME
makes use of this new value the config will be able to use it.Hope it helps!