In mid-2021, Tim Davis wrote a blog on migrating the state from TFC (Terraform Cloud) to another IaC management platform.
It does a great job of highlighting how TFC and env0 function in terms of both runs and state, or at least it did. Since this original post, the env0 Platform evolution has been quick and with features and improvements coming out every week, it can be hard to keep up. Let me elaborate…
The original article mentions “env0 does not offer remote state backend capabilities…yet (Stay tuned)”. We do now! env0 recently added remote backend support that stores customer state files in a remote location (per customer, per environment). This now allows our customers to pick between three methods of managing state files.
The key takeaway here is that these three options allow you as the customer to make design decisions based on your desired solution.
Now with these three options available to our users, we are able to seamlessly migrate both the runs and state to env0 and provide the same governance and compliance for the state.
In these examples, we will assume you are migrating your run and state from TFC to env0.
This migration method can be somewhat challenging because you have to move the state via a proxy, typically a local machine or jump box/bastion host. It also requires a login to TFC and env0. The below procedure can be used to migrate TFC local state to the env0 local state.
Login to TFC: [.code]terraform login[.code]
[.code]mkdir -p terraform.tfstate.d/tfc-migration[.code]
[.code]terraform state pull > terraform.tfstate.d/tfc-migration/terraform.tfstate[.code]
[.code]mv .terraform/terraform.tfstate .terraform/terraform.tfstate.old[.code]
[.code]cp terraform.tfstate.d/tfc-migration/terraform.tfstate .terraform/terraform.tfstate[.code]
Remove the cloud block in the config (if configured)
[.code] terraform plan[.code]. No changes should be seen.
Login to env0: [.code]terraform login backend.api.env0.com[.code]
Add the env0 cloud block: https://docs.env0.com/docs/state-migration
[.code]terraform init -migrate-state -force-copy[.code]
Update the env0 template with the correct VCS configuration.
We are working hard to have this scripted. Keep your eyes peeled.
Possibly the easiest type of migration is when using TFC with a remote state such as s3. This migrates the run logic while leaving the state untouched.
The only part of the migration that can become troublesome is the replication of variables and environmental variables. To assist in the migration, we have created a script that will pull the data from your TFC workspace and create an equivalent env0 workspace, including moving your variables from one to the other.
This method is identical to the “TFC remote state to env0 local state” with one additional step at the end of the procedure. Enable “Use env0 remote backend” in the environment settings and redeploy, moving your state from env0 local to env0 remote backend.
These three migration methods allow you to migrate from TFC to env0 and have the state stored in any way that meets your business needs. We want to provide as much flexibility when moving over to env0 and we understand the state is an important part of the conversation.
If you are looking for features around executing remote runs locally, then I suggest you check out another recent post.
If you have any questions about state migration or env0 in general, please reach out to us and our team would be happy to help.