Migrate state to HCP Terraform
When using Terraform Community Edition, you are responsible for maintaining a state file as the source of truth for your cloud infrastructure. Terrafrom Cloud offers secure remote state storage, make it easier to collaborate on infrastructure development. You can migrate your state to HCP Terraform without interrupting services or recreating your existing infrastructure.
In this tutorial, you will migrate a local state file to HCP Terraform.
Warning
When uploading a state file to HCP Terraform using the steps in this tutorial, always use the same version of the Terraform CLI you used to create the resources. Using a newer version of Terraform may update the state file and cause state file corruption.
Prerequisites
This tutorial assumes that you have the following:
- The Terraform CLI version 1.1 or higher installed locally
- An HCP Terraform account
Note
Terraform versions older than 1.1 use the remote
backend
block to
configure the CLI workflow and migrate state.
Create state
Clone the example configuration for this tutorial. This configuration uses the random
provider to generate a random pet name.
Next, change into the directory.
Open main.tf
to review the configuration. It uses an input varaible to determine the length of the generated string and outputs the value.
Initialize the directory.
Now apply the configuration, typing yes
at the prompt to confirm the operation.
Terraform displays the generated name in the outputs.
Configure HCP Terraform integration
Now that you have a local state file, you need to create a cloud
code block in your configuration.
To use HCP Terraform as a backend for your configuration, you must include a
cloud
block in your configuration.
Add the cloud
block to your configuration as shown below, replacing
ORGANIZATION-NAME
with your own HCP Terraform organization name.
While the organization defined in the cloud
stanza must already exist, the workspace does not have to; HCP Terraform will create it if necessary. If you use an existing workspace, it must not have any existing states.
Note
HCP Terraform workspaces behave differently from Terraform CLI workspaces. Terraform CLI workspaces allow multiple state files to exist within a single directory, letting you use one configuration for multiple environments. HCP Terraform workspaces contain everything needed to manage a given set of infrastructure, and function like separate working directories.
Authenticate with HCP Terraform
After configuring your HCP Terraform integration, you must authenticate to HCP Terraform to use it for remote operations.
Run terraform login
and follow the prompts to log in, typing yes
at the confirmation prompt.
For more detailed instructions on logging in, review the login tutorial.
Migrate the state file
To migrate your existing state file to HCP Terraform, you must reinitialize your configuration to update the backend.
Reinitialize your configuration. Terraform detects your updated backend and confirms that you wish to migrate your state file to HCP Terraform. Type yes
to confirm the migration.
Configure the HCP Terraform workspace
After migrating your state to HCP Terraform, log in to the HCP Terraform web UI and navigate to your learn-terraform-migrate
workspace. Then, go to the workspace's States page. HCP Terraform lists the state you migrated to your new workspace.
Your configuration relies on an input variable. Navigate to the workspace's
Variables page and create a new Terraform variable named name_length
with
a value of 5
.
If the configuration relied on a cloud provider, you would set the provider credentials on this page as well.
Initiate a run in the new workspace
After verifying that Terraform migrated your state to HCP Terraform, remove your local state file.
Trigger a new run. Terraform will propose replacing your resource to reflect the update to the name_length
input variable. Confirm the operation by typing yes
.
Terraform streams the logs to your local console, and also displays the run details in the workspace UI.
Destroy your infrastructure
Run terraform destroy
to clean up your resources.
You may also optionally delete your HCP Terraform workspace from your workspace's settings page. Review the Destroy resources and workspace tutorial for detailed guidance.
Next steps
In this tutorial, you migrated a state file from your local machine to an HCP Terraform workspace. To learn more about related concepts and HCP Terraform features, review the following resources:
- Review the documentation on Migrating State from Multiple Local Workspaces
- Follow the tutorial on connecting workspaces using HCP Terraform run triggers
- Learn how to manage permissions in HCP Terraform