- Clone the HashiCat repository
- Validate the configuration and provision locally with Terraform Open Source
- Add a Terraform Cloud
cloudblock to your configuration - Authenticate the CLI and run
terraform initto 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, andshow
1. Clone the HashiCat repository
Get the example repository that contains the Terraform configuration:2. Export AWS credentials
Set AWS credentials in your shell so Terraform can provision resources:3. Inspect the configuration
Open the repository to review the Terraform configuration. The example includesrequired_providers and an aws provider:
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):var.prefix):
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 aterraform cloud block in a file such as backend.tf:
- If you want to bind this configuration to a specific existing workspace, include
name = "workspace-name"insideworkspaces. - If you omit
name, the migration flow will prompt you to create or provide a workspace duringterraform init.
6. Authenticate the Terraform CLI with Terraform Cloud
Log the CLI into Terraform Cloud so Terraform can manage the remote workspace and state:7. Initialize and migrate state with terraform init
Run: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
yescopies your local state snapshot to Terraform Cloud and creates (or activates) the workspace you specify. - Choosing
nowill activate the workspace without copying local state (useful if you want to start with an empty or existing remote state).
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:- View the Workspace → States in the Terraform Cloud UI: https://app.terraform.io
- Or run:
9. Configure workspace variables and team access
Set required variables in the Terraform Cloud workspace UI to prevent interactive prompts during runs. For example, addprefix = 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-devworkspace
10. Trigger a remote apply (Terraform Cloud run)
With variables set and the workspace active, runningterraform 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:
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:
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:Quick reference: key commands and files
Links and further reading
Congratulations — you’ve migrated a Terraform Open Source workspace and its state into Terraform Cloud, configured workspace variables and access, and verified remote applies.