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.
- Terraform Cloud / HCP documentation: https://www.terraform.io/cloud
- Terraform CLI documentation (init/login): https://www.terraform.io/cli
Project layout
| File | Purpose |
|---|---|
main.tf | Core resources (VPC and subnets) |
providers.tf | Provider configuration and Terraform settings |
variables.tf | Variable declarations with defaults |
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_IDandAWS_SECRET_ACCESS_KEYexported locally). - Run
terraform initto download providers and initialize the working directory:
2) Plan and apply locally
terraform planshould show three resources to add.- Apply with
terraform apply --auto-approveto create the resources locally.
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
Updateproviders.tf (or another file) to include the cloud block inside the terraform block so Terraform targets the HCP workspace:
4) Authenticate to HCP Terraform
Runterraform 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.
5) Re-initialize to connect to HCP and migrate state
Runterraform init again. With the cloud backend configured, Terraform will prompt to migrate your local state to the HCP workspace.
- Type
yesto 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:

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):| Variable name | Notes |
|---|---|
AWS_ACCESS_KEY_ID | Mark as sensitive |
AWS_SECRET_ACCESS_KEY | Mark as sensitive |
AWS_SESSION_TOKEN | Mark as sensitive (only if using STS session tokens) |

8) Re-run plan/apply via CLI (remote execution)
With workspace variables in place, runterraform plan. The plan runs remotely and streams output to your terminal:
9) Make and apply a change
Example: add anEnvironment tag to the VPC resource in main.tf:
terraform plan (remote). The plan will show an in-place update to the VPC tags:
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 localterraform.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.