skip to content

After a team adds HCP Terraform's `cloud` block and runs `terraform login`, where does `terraform plan` execute, and whose credentials does it use?

level: middleimportance: must knowfreq 52%

answer

  1. two different credentials are involved
  2. what the login token can reach
  3. the directory gets uploaded
  4. a disposable worker's environment
  5. one command that stays local

basics

~20 s

With Remote execution the plan runs on a disposable HCP Terraform worker, using the workspace's variables and credentials. The terraform login token only authenticates the CLI to HCP Terraform; cloud credentials in the laptop's shell are not sent.

solid answer

~40 s

Two credentials are in play. `terraform login` obtains an API token, saved by default in plain text in `credentials.tfrc.json`, and the CLI uses it to talk to HCP Terraform. The `cloud` block names the organization and the workspace. In a Remote-mode workspace, `terraform plan` then uploads the configuration directory and starts a speculative plan on a disposable worker VM, streaming the logs back. That worker gets its environment from the workspace, so provider credentials must be set there; only `-var`, `-var-file` and `TF_VAR_` values travel from the laptop. The run uses a per-run token for state, and the workspace's Terraform version rather than the local binary. The exceptions run locally: `terraform import`, or any operation against a workspace in Local mode.

code

hcl · 11 lines
hcl
terraform {
  cloud {
    organization = "my-org"

    workspaces {
      tags = {
        layer = "networking"
      }
    }
  }
}

go deeper

for a junior

Recall the sequence: terraform login for a token, a cloud block naming organization and workspace, then terraform init. Know that the token is for HCP Terraform and not for your cloud account.

for a middle

Walk through the upload, the speculative plan on a worker VM and the log stream. Then explain why provider credentials must be workspace variables while TF_VAR_ and -var values still travel from the laptop.

for a senior

Diagnose the plan that works locally and fails remotely: missing provider credentials, a file outside the upload, a tool absent from the worker, or a different Terraform version. Name terraform import as the local exception.

for a principal

Connect the move to credential hygiene: secrets leave laptops only when execution actually moves. Local mode and terraform import keep credentials on machines, so a migration plan has to say what stays and who still holds them.

## The setup in three steps HCP Terraform is HashiCorp's service for running Terraform. Connecting a working directory to it takes three steps: 1. **`terraform login`** obtains an API token for the host, by default `app.terraform.io`. 2. **A `cloud` block** inside the `terraform` block names the `organization` and the workspace, through `workspaces.name` or `workspaces.tags`, optionally with `workspaces.project`. 3. **`terraform init`** completes the connection. If the directory has state from an existing `backend`, it prompts you to migrate that state. ```hcl terraform { cloud { organization = "my-org" workspaces { project = "networking" name = "networking-prod-us-east" } } } ``` The `cloud` block needs Terraform 1.1 or later, and key-value `tags` need 1.10. It is mutually exclusive with a `backend` block, and like the rest of the `terraform` block it cannot refer to input variables or other named values. ## Where the plan runs The answer depends on the workspace's **execution mode**. In **Remote** mode, this happens when you type `terraform plan`: - The CLI uploads an archive of the configuration directory. A `.terraformignore` file defines what to exclude; without one, Terraform still leaves out `.git/` and `.terraform/` (except `.terraform/modules`). - HCP Terraform starts a **speculative plan**, a plan-only run that cannot apply anything. - The plan runs on a **single-use Linux worker VM**, and the logs stream to your terminal with a link to the run. - `terraform apply` works the same way, but as a normal run: it plans remotely, then prompts for approval in the terminal. In a workspace linked to a repository, the CLI refuses a remote apply, because the repository is that workspace's source of truth. ## Whose credentials | Credential | Used by | Reaches | Where it lives | |---|---|---|---| | User or team API token | The CLI on your machine | HCP Terraform's API | `credentials.tfrc.json`, or `TF_TOKEN_app_terraform_io` | | Per-run token | The worker | State, the private registry | Generated per run, invalid afterwards | | Provider credentials | The worker's provider plugins | The cloud account | Workspace variables, a variable set, or dynamic credentials generated per run | This is the part that surprises people moving from hand-run applies. The worker cannot see your shell, so cloud keys exported on the laptop do nothing. The documentation says it directly: Terraform can use `TF_VAR_` shell variables as input variables for the run, but you must still set required environment variables, like provider credentials, inside the workspace. The same reasoning covers everything else local: - **Local files:** the worker only has what was uploaded, so a file outside the uploaded directory is not there. - **Local tools:** software your laptop has installed is not necessarily on the worker, and changes made to a worker do not persist to the next run. - **The Terraform binary:** the run uses the workspace's Terraform version, and the local version is irrelevant to a remote operation. A few values do travel from your side, and it helps to list them when someone asks why a variable reached the run but a credential did not: - `-var` and `-var-file` arguments, as run-specific values. - Shell variables prefixed with `TF_VAR_`, which become input variables for the run. - Any `*.auto.tfvars` files, because they are part of the uploaded configuration. ## What stays local - **`terraform import`** has no remote execution. The workspace acts only as state storage and its environment variables are not available, so provider credentials must exist in your local environment. - **State commands** such as `terraform state mv` also run locally, using your token, which needs permission to read and write state. - **A workspace in Local mode** runs everything on your machine, and HCP Terraform does not evaluate its workspace variables at all. ## Small behaviours worth naming - **Ctrl-C:** during a remote plan it stops the log stream, not the plan. During a remote apply it cancels the run only if the run is still pending. - **`-auto-approve`:** the workspace's auto-apply setting does not govern CLI runs, which use this flag to skip the prompt. - **`terraform login` in automation:** the command launches a browser, so it is for interactive use. A pipeline sets credentials in the CLI configuration or through `TF_TOKEN_app_terraform_io`. - **A missing token** fails with `Required token could not be found` and suggests `terraform login`.

  • How does a pipeline authenticate to HCP Terraform when `terraform login` cannot run?
    `terraform login` launches a web browser, so it suits interactive use only. In unattended automation, configure credentials in the CLI configuration file or set the host-specific environment variable `TF_TOKEN_app_terraform_io`, supported since Terraform 1.2. That token authenticates to HCP Terraform; it is not a provider credential.
  • Does pressing Ctrl-C stop a remote HCP Terraform run?
    Usually not. During `terraform plan` it stops streaming logs while the plan continues remotely. During `terraform apply` it cancels the run only if it is still pending; once the apply has started it just stops the stream. To stop a running plan or apply, use Cancel Run, which needs permission to apply runs.
  • Which common command still needs provider credentials on the laptop after this move?
    `terraform import`. HCP Terraform does not support remote execution for it, so it runs on your machine with the workspace acting only as state storage. Workspace environment variables are not available to it, so set provider credentials locally. An `import` block avoids this, because it runs inside a normal remote run.
  • What does `terraform init` do when the directory still has state from an S3 backend?
    Replace the `backend` block with `cloud`, because the two are mutually exclusive, and run `terraform init`. It sees the existing state and prompts you to migrate it into HCP Terraform workspaces. It may also ask you to rename CLI workspaces, since HCP Terraform workspace names must be unique in the organization.

  • Running terraform login gives the remote run access to my cloud account.
  • Exporting cloud keys in my shell is enough, because Terraform always reads the environment.
  • With the cloud block, a plan always runs on my laptop and only state is remote.
  • A cloud block and a backend block can sit side by side during a migration.
  • My local Terraform binary decides which version a remote run uses.
  • Pressing Ctrl-C always stops a remote plan or apply on the worker.

Assumes Terraform 1.17; the cloud block needs 1.1 or later, and key-value tags inside it need 1.10.