Terraform
- Terraform Enterprise
- 1.0.x (latest)
- v202507-1
- v202506-1
- v202505-1
- v202504-1
- v202503-1
- v202502-2
- v202502-1
- v202501-1
- v202411-1
- v202410-1
- v202409-3
- v202409-2
- v202409-1
- v202408-1
- No versions of this document exist before v202408-1. Click below to redirect to the version homepage.
- v202407-1
- v202406-1
- v202405-1
- v202404-2
- v202404-1
- v202402-2
- v202402-1
- v202401-2
- v202401-1
- v202312-1
- v202311-1
- v202310-1
- v202309-1
- v202308-1
- v202307-1
- v202306-1
- v202305-2
- v202305-1
- v202304-1
- v202303-1
- v202302-1
- v202301-2
- v202301-1
- v202212-2
- v202212-1
- v202211-1
- v202210-1
- v202209-2
- v202209-1
- v202208-3
- v202208-2
- v202208-1
- v202207-2
- v202207-1
- v202206-1
Migrate Terraform state to HCP Terraform or Terraform Enterprise
This topic describes how to migrate existing Terraform state files to HCP Terraform or Terraform Enterprise without first de-provisioning them. Refer to State in the Terraform configuration language reference for additional information about Terraform state.
Overview
Perform the following actions to migrate existing resources to one or more workspaces in HCP Terraform or Terraform Enterprise:
- Stop all Terraform operations associated with the state files.
- Migrate Terraform state files using either the Terraform CLI or the HCP Terraform or Terraform Enterprise API.
Hands-on: Complete the Migrate State to HCP Terraform tutorial for additional guidance on how to migrate Terraform state using the CLI.
Requirements
- Terraform v1.1 and later is required to configure the cloudblock. For Terraform v1.0 and older, use theremotebackend instead.
- You must present a token linked to appropriate permissions to use the API. Refer to the following topics for additional information:
Stop Terraform operations
Stop all Terraform operations associated with the state files. This may require locking or deleting CI jobs, restricting access to the state backend, and communicating with other teams. You should also only migrate state files into HCP Terraform or Terraform Enterprise workspaces that have never performed a run.
Migrate state using the CLI
- Add the - cloudblock to your Terraform configuration and specify the following fields:- hostnamefield: Specify either- app.terraform.iofor HCP Terraform or the hostname of your Terraform Enterprise deployment.
- organizationfield: Specify your HCP Terraform or Terraform Enterprise organization.
- workspacesblock: Add a- tagsor- namefield and specify one or more destination workspaces as a list of strings.
 - Refer to The - cloudBlock in the Terraform CLI documentation for additional information. The following example migrates the state associated with the configuration to the- networkingworkspace on HCP Terraform:- terraform { cloud { hostname = "app.terraform.io" organization = "my-org" workspaces { tags = ["networking"] } } }
- Run - terraform init. Terraform creates any workspaces specified in the configuration if they do not already exist in the organization.
Migrate state using the API
- Encode your state files as a base64 string and generate an MD5 hash. The following example generates the string and hash file for a single - terraform.tfstatefile:- $ cat terraform.tfstate | base64 dGVycmFmb3JtLnRmc3RhdGUK $ md5sum terraform.tfstate 690a3f8ae079c629494a52c68757d585 terraform.tfstate
- If the workspace does not yet exist in your organization, send a - POSTrequest to the- /organizations/:organization_name/workspacesendpoint to create it. Otherwise, proceed to the next step. Refer to the following topics for details:- Create a workspace in the HCP Terraform API reference documentation.
- Create a workspace in the Terraform Enterprise API reference documentation.
 
- Send a - POSTrequest to the- /workspaces/:workspace_id/actions/lockendpoint to lock the workspace. Refer to the following topics for details:- Lock a workspace in the HCP Terraform API reference documentation.
- Lock a workspace in the Terraform Enterprise API reference documentation.
 
- To upload your state files to the workspace, send a - POSTrequest to the- /workspaces/:workspace_id/state-versionsendpoint. Specify the MD5 string in the- data.attributes.md5field and encoded state file in the- data.attributes.statefield of the request body. Refer to the following topics for details:- Create a state version in the HCP Terraform API reference documentation.
- Create a state version in the Terraform Enterprise API reference documentation.
 
- Send a - POSTrequest to the- /workspaces/:workspace_id/actions/unlockendpoint to unlock the workspace.
Refer to the following external article for an example of how to create a script in Python to automate multiple state files: Migrating A Lot of State with Python and the HCP Terraform (previously Terraform Cloud) API. The example uses the Workspaces API to create the necessary workspaces in HCP Terraform and the State Versions API to migrate the state files to those workspaces.
Migrate state using Terraform migrate
You can use the Terraform migrate CLI tool to automatically migrate state to HCP Terraform and Terraform Enterprise. The tool does not ship with HCP Terraform. You must download and install the binary for the CLI tool separately. Refer to the Terraform migrate documentation for more information.