The defining behaviour of HCP Terraform, from the perspective of a working directory, is that plans and applies run on a remote worker against state stored on the platform. The local working directory contributes the HCL configuration and the engineer's intent; HCP Terraform contributes the execution surface, the state store, the variable store, and the run history. Wiring a working directory to HCP Terraform replaces the open-source backend block with a cloud block, replaces local execution with a remote run, and replaces the local terraform.tfstate file with a versioned state object kept inside the workspace.
The cloud block
The cloud block grammar is narrow and intentional. It nests inside the terraform {} block (the same outer block that hosts required_version and required_providers), it accepts a single required organization argument, an optional hostname that defaults to app.terraform.io, and a workspaces child block. Inside workspaces, the configuration either names exactly one workspace through the name argument or matches a set of workspaces through the tags argument or the project argument. The two strategies are mutually exclusive: name targets one workspace and binds the working directory to it for every subsequent CLI invocation; tags targets a set of workspaces and requires the CLI session to pick one through terraform workspace select before a plan can run. Backend arguments inside the cloud block cannot reference variables, just as the backend block cannot, because Terraform Core has to resolve the workspace before any variable evaluation happens.
terraform {
required_version = ">= 1.12.0"
cloud {
organization = "example-co"
workspaces {
tags = ["env:prod", "layer:network"]
}
}
}
Authenticate the CLI (terraform login)
Authenticating the CLI to HCP Terraform uses the terraform login interactive command, which opens a browser to the named hostname, prompts for an API token, and writes that token into ~/.terraform.d/credentials.tfrc.json. The credentials file is a JSON document keyed by hostname, and any subsequent terraform invocation reads it transparently. Two alternatives exist for non-interactive contexts. The TF_TOKEN_app_terraform_io environment variable (the hostname is mapped to an env-var-safe name by replacing dots with underscores) is the most common form in CI because it can be injected from a secret store without writing to disk. A credentials block in the user CLI config file is the third option and is the right fit for engineer workstations that have to talk to multiple hosts. The token used here is a user API token, a team API token, or an organization API token, in decreasing order of permission scope; team tokens are the recommended form for automation because their permissions are narrow and they can be rotated without touching a human account.
Once the CLI is authenticated and the cloud block is wired up, the first terraform init reaches out to HCP Terraform, verifies that the named organization exists, creates the named workspace if it does not yet exist (auto-create applies only to the name form; in the tags form the workspaces must already exist), and pulls down any provider plugins required by the configuration. From that point forward, every plan, apply, and destroy invocation is a remote operation: the CLI bundles the working directory into a tarball, uploads the tarball as a new configuration version, creates a run that points at the configuration version, and then streams the run's structured log output back to the local terminal.
$ terraform login app.terraform.io
$ terraform init
$ terraform plan
Running plan in HCP Terraform. Output will stream here. Pressing Ctrl-C
will stop streaming the logs, but will not stop the plan running remotely.
Preparing the remote plan...
To view this run in a browser, visit:
https://app.terraform.io/app/example-co/platform-network/runs/run-h4Kx7Q2y
Workspace execution modes
The execution mode of a workspace decides where the Terraform binary actually runs and is the single most important setting for selecting how a workspace integrates with private networks and customer-managed runtime. Three modes exist. Remote is the default: HCP Terraform's hosted workers download the configuration, execute the binary, and emit logs. Agent mode is the same except that execution happens on a customer-managed HCP Terraform agent process, which polls HCP Terraform over an outbound HTTPS connection and runs in the customer's own network so it can reach private endpoints (a VPC-only RDS instance, an on-prem vCenter, an internal Vault cluster). Local mode disables remote execution entirely; HCP Terraform stores state and variables but the engineer runs plan and apply on their own machine, which makes the platform behave like a remote backend rather than a full execution surface.
A run progresses through a well-defined set of states the exam may name in scenario questions: pending, plan_queued, planning, planned, optionally cost_estimating, optionally policy_checking, apply_queued, applying, and finally applied (or errored, canceled, or discarded if the run does not complete). A successful applied run produces a new state version, which is an immutable snapshot of state stored in the workspace. The workspace UI exposes the full list of state versions, the diff between adjacent versions, and the ability to roll back to a previous version by promoting it to current.
Workspace variables
The input-variable model is the same model the CLI uses, with one addition: variables can also be set inside the workspace through the workspace variables surface. Workspace variables come in two flavours. Terraform variables map onto HCL input variables and behave as if -var had been passed on the command line. Environment variables are exposed to the Terraform process and to any provider it spawns, so they cover provider credentials and any plugin that reads from the environment. Both flavours can be flagged as sensitive, which encrypts them at rest and redacts them in the UI and run logs. Workspace variables sit at the same precedence slot as terraform.tfvars: CLI flags (-var and -var-file) win over them, TF_VAR_* environment variables also win over them in the precedence ordering, but auto.tfvars files in the working directory lose to them.
# Migrating an existing local-state working directory into HCP Terraform
$ terraform login app.terraform.io
$ # Edit terraform { cloud {} } block, removing any backend block, then:
$ terraform init -migrate-state
Do you want to copy existing state to HCP Terraform?
Enter a value: yes
Successfully configured the cloud backend "cloud"! Terraform will automatically
use this backend unless the backend configuration changes.
💡 Exam Trap: The
cloudblock and thebackendblock are mutually exclusive in the sameterraform {}block. A configuration with both blocks failsterraform initwith a parse error. Migrating from a remote backend to HCP Terraform requires removing thebackendblock first, adding thecloudblock, and runningterraform init -migrate-state. The migration prompt either copies the existing state into the workspace (the typical answer for a workspace that should keep its history) or starts from an empty state (the right answer for a brand-new workspace that just happens to have the same name as an old one).
💡 Exam Trap:
terraform loginandterraform logoutwrite to and remove from~/.terraform.d/credentials.tfrc.json. The file is keyed by hostname, so logging in toapp.terraform.iodoes not authenticate against a Terraform Enterprise hostname; each host needs its ownterraform logininvocation. Engineers who run against both the SaaS and a self-hosted Terraform Enterprise instance often add acredentialsblock per host in the CLI config file so that both tokens are present at once.
📊 Limit: A workspace name in HCP Terraform may be up to 90 characters; only letters, digits, hyphens, and underscores are accepted. The organization name has to already exist before
terraform initruns against thecloudblock, because HCP Terraform auto-creates workspaces but not organizations. Run logs and state versions are retained indefinitely by default; organization administrators can configure retention to a lower bound.
⚠️ Anti-Pattern: Hard-coding the workspace name in the
cloudblock for an environment-per-workspace layout (workspaces { name = "production" }) and then trying to switch to staging throughTF_WORKSPACEorterraform workspace select. The single-name form pins the working directory to one workspace; switching requires either editing the configuration or moving to thetagsform. The CLI workspace command is a no-op against a name-boundcloudblock, and any attempt to select a different workspace returns an error rather than silently switching context.
🎯 Scenario: A platform team owns a
networkconfiguration that was originally backed by an S3 plus DynamoDB remote backend. They want to move it to HCP Terraform so that downstream teams can read the network outputs throughterraform_remote_state. The steps: remove thebackend "s3"block, add acloudblock with the target organization and a workspace name, runterraform login, runterraform init -migrate-state, and accept the copy prompt. The nextterraform planruns remotely and the state is now stored in HCP Terraform; the S3 bucket can be retired once consumers have re-pointed at the workspace through theterraform_remote_statedata source.
Decision Anchor: Choose Remote execution mode when HCP Terraform's hosted workers can reach the target provider APIs over the public internet. Choose Agent execution mode when the target endpoints sit inside a private network (VPC-only services, on-prem vCenter, an internal Vault cluster) so that the worker process runs inside the customer network and dials HCP Terraform over an outbound connection. Choose Local execution mode only when the platform's primary value is centralised state and variables and the engineer wants to keep running Terraform from their own machine for interactive control.
