Skip to main content
Many teams begin with Terraform Open Source and later want to move their existing infrastructure into Terraform Cloud without destroying or recreating resources. This guide walks through migrating a local Terraform workspace (the HashiCat AWS example) to Terraform Cloud while preserving state and enabling remote runs. Overview
  • Clone the HashiCat repository
  • Validate the configuration and provision locally with Terraform Open Source
  • Add a Terraform Cloud cloud block to your configuration
  • Authenticate the CLI and run terraform init to migrate state
  • Perform post-migration tasks: workspace variables, team access, and a remote apply
  • Remove local state files after verifying the migration
This lesson demonstrates copying an existing local state into a newly created Terraform Cloud workspace. During the migration prompt you can choose to copy the state or activate the remote workspace without copying state—pick the option that matches your workflow.

Prerequisites

  • A Terraform Cloud account and organization
  • Terraform CLI installed
  • AWS credentials with permissions to create the HashiCat resources
  • Basic familiarity with Terraform commands: init, plan, apply, and show
Useful references:

1. Clone the HashiCat repository

Get the example repository that contains the Terraform configuration:
Example output:

2. Export AWS credentials

Set AWS credentials in your shell so Terraform can provision resources:
Replace the placeholder values with your credentials, or use another supported authentication method (environment variables, AWS CLI profile, or instance profile).

3. Inspect the configuration

Open the repository to review the Terraform configuration. The example includes required_providers and an aws provider:
Confirm any required variables, providers, modules, or provisioners so you understand what the state contains before migrating.

4. Initialize and apply locally (optional verification)

Change into the project directory and run the standard lifecycle commands to validate the configuration and create resources locally (using Terraform Open Source):
You may be prompted for required variables (for example var.prefix):
If the plan shows resources to add, apply them:
Example summary:
You might see provisioner logs (e.g., remote-exec) during apply. Confirm the application is reachable via the catapp_url output.

5. Add a Terraform Cloud block

To move state to Terraform Cloud, add a terraform cloud block in a file such as backend.tf:
Notes:
  • If you want to bind this configuration to a specific existing workspace, include name = "workspace-name" inside workspaces.
  • If you omit name, the migration flow will prompt you to create or provide a workspace during terraform init.

6. Authenticate the Terraform CLI with Terraform Cloud

Log the CLI into Terraform Cloud so Terraform can manage the remote workspace and state:
This will open a browser or provide a token URL. Follow the prompts to generate and store an API token locally for the CLI.

7. Initialize and migrate state with terraform init

Run:
Terraform will detect the cloud block and the Terraform Cloud organization and workspace configuration. During initialization you’ll be prompted whether to migrate the existing local state into the configured Terraform Cloud workspace:
  • Choosing yes copies your local state snapshot to Terraform Cloud and creates (or activates) the workspace you specify.
  • Choosing no will activate the workspace without copying local state (useful if you want to start with an empty or existing remote state).
After a successful migration, you should see:
At this point the workspace devops-aws-hashicat-dev exists in the Mastering-Terraform-Cloud organization with your migrated state.

8. Verify remote state

To confirm the state is now in Terraform Cloud:
The CLI will read the remote state and print outputs and resource information. Example JSON excerpt (remote state snapshot):

9. Configure workspace variables and team access

Set required variables in the Terraform Cloud workspace UI to prevent interactive prompts during runs. For example, add prefix = meow as a workspace variable. Manage access:
  • Create a team (e.g., HashiCat Devs) under Organization → Teams
  • Grant the team appropriate access (e.g., write) to the devops-aws-hashicat-dev workspace

10. Trigger a remote apply (Terraform Cloud run)

With variables set and the workspace active, running terraform apply from your local machine will trigger a remote plan and apply in Terraform Cloud. The CLI will stream the run output back to your terminal:
Example streamed output:
Note: This example includes a null_resource that uses a timestamp() trigger; that can cause a small change and result in a single resource being changed during the remote apply. Example null_resource excerpt:
You can always view the run and logs directly in the Terraform Cloud UI.
Only delete local state files after you have verified the workspace exists in Terraform Cloud and that terraform show reads the remote state correctly. Removing local state prematurely can make recovery more difficult.

11. Clean up local state files

After confirming state is managed remotely, remove the local state files to avoid confusion:
From this point forward, your state is stored in Terraform Cloud and you should use the remote workspace and Terraform Cloud workflows.

Quick reference: key commands and files

Congratulations — you’ve migrated a Terraform Open Source workspace and its state into Terraform Cloud, configured workspace variables and access, and verified remote applies.
A screenshot of the HashiCorp Terraform Cloud "Workspaces" page listing four workspaces (devops-aws-hashicat-dev and three devops-aws-myapp environments). Each workspace shows a green "Applied" run status, repo names, and recent change timestamps.

Watch Video

Practice Lab