Skip to main content
This guide walks through migrating a local Terraform configuration and its terraform.tfstate into an HCP (HashiCorp Cloud Platform) Terraform workspace named hcp-demo (Terraform version 1.2.2 configured for the workspace). You’ll initialize locally, apply to create resources, add the HCP cloud backend, authenticate with HCP, migrate the state, and run remote plans/applies.

Prerequisites

  • Terraform CLI installed (>= 1.2.2).
  • An HCP Terraform organization and workspace (hcp-demo) already created.
  • AWS account credentials for creating VPC/subnets.
  • Browser access to complete terraform login.
Useful references:

Project layout

Example resources from main.tf (simple VPC and two subnets):

Original provider configuration

Before adding the HCP cloud block, providers.tf looks like:

1) Initialize locally

  • Ensure AWS credentials are present in your environment (for example, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY exported locally).
  • Run terraform init to download providers and initialize the working directory:

2) Plan and apply locally

  • terraform plan should show three resources to add.
  • Apply with terraform apply --auto-approve to create the resources locally.
Example apply output:
After this step your local directory contains terraform.tfstate managing the three resources. Next you will configure the HCP workspace as the remote backend and migrate that state.
Before migrating state, consider creating a backup of your local terraform.tfstate. While the migration process copies your current state snapshot into the workspace by default, keeping a local backup can help you recover if anything unexpected occurs.

3) Add the HCP cloud block to your configuration

Update providers.tf (or another file) to include the cloud block inside the terraform block so Terraform targets the HCP workspace:
Format files if needed:

4) Authenticate to HCP Terraform

Run terraform login to generate and store an API token for app.terraform.io (HCP Terraform). The command will open a browser flow to create a token which you paste back into the CLI prompt.
After successful login, Terraform CLI can interact with your HCP organization and workspaces.

5) Re-initialize to connect to HCP and migrate state

Run terraform init again. With the cloud backend configured, Terraform will prompt to migrate your local state to the HCP workspace.
  • Type yes to copy the current local state into the HCP workspace. Terraform completes initialization and activates the workspace with your migrated state.

6) Remote runs and the HCP UI

Once the workspace is activated, terraform plan and terraform apply will execute remotely in HCP Terraform. The CLI streams logs while the plan/apply runs on HCP workers. You can also open the workspace Runs in the HCP UI to view details. Example of starting a remote plan:
A run triggered via CLI will appear in the workspace Run List in the HCP UI (status: Planning):
The image shows a Terraform Cloud workspace interface titled "hcp-demo," with options for managing runs, states, variables, and settings. A run triggered via CLI is listed under "Run List," with its status marked as "Planning."
When using remote execution, the Terraform process runs on HCP Terraform’s workers and cannot access environment variables on your local machine. Configure workspace environment variables (or another credential method) in the HCP workspace so remote runs can authenticate to your cloud provider.

7) Add AWS credentials to workspace variables

In the HCP UI, go to your workspace → Variables and add the following environment variables (mark them as sensitive): After saving a variable you should see confirmation that it was saved (sensitive flag enabled):
The image shows a Terraform Cloud workspace variables page, displaying a sensitive workspace variable named "AWS_ACCESS_KEY_ID." There's also an indication that a variable was successfully saved.

8) Re-run plan/apply via CLI (remote execution)

With workspace variables in place, run terraform plan. The plan runs remotely and streams output to your terminal:

9) Make and apply a change

Example: add an Environment tag to the VPC resource in main.tf:
Run terraform plan (remote). The plan will show an in-place update to the VPC tags:
Apply the change:
The remote apply triggers a run in the HCP workspace that performs both the plan and the apply steps. Each run can be reviewed in the workspace Runs page, including plan output and apply results.

Workspace overview and resource list

In the HCP Terraform workspace Overview you can inspect:
  • Latest run and detailed logs
  • Resource counts and the list of managed resources (for this example: aws_vpc.main, aws_subnet.private, aws_subnet.public)
  • Provider and module versions used by the workspace

Conclusion

You have migrated a local Terraform configuration and the local terraform.tfstate into an HCP Terraform workspace (hcp-demo). Remote execution is now enabled: plans and applies run on HCP workers and stream their output to your CLI, with provider credentials provided via workspace variables. This setup centralizes state and execution to HCP while preserving your existing configuration and resource graph.

Watch Video