HCP Terraform · Topic 1 of 2

HCP Terraform Workflows and CLI Integration

Terraform Associate 004: create infrastructure with HCP Terraform VCS, CLI and API workflows, terraform login, the cloud block and execution modes.

  • 5 min
  • Includes a diagram
In a nutshell
  • The three workflows are VCS driven, CLI driven and API driven.
  • Pull requests in a VCS driven workspace produce speculative plans that cannot be applied.
  • terraform login stores an API token in the CLI credentials file.

What it is

HCP Terraform (formerly Terraform Cloud) runs Terraform for you as a hosted service. It stores each workspace's state, keeps variables and credentials secure, runs plans and applies on disposable machines it manages, and records every run.

You create infrastructure in HCP Terraform through one of three workflows, and you connect a local configuration to it with terraform login and the cloud block.

terraform logincloud blockorganization and workspaceterraform initWorkspace connectedterraform applyRemote runplan, policy, applyState storedin HCP Terraform

How it works

The three run workflows

HCP Terraform run workflows: VCS driven runs start on a commit or merge, with speculative plans for pull requests; CLI driven runs start from terraform plan or apply with a cloud block; API driven runs upload a configuration version; all run in a workspace as plan, policy checks, apply.

WorkflowWhat starts a runTypical use
VCS drivenA commit or merge to the connected repository branchTeams that review changes as pull requests
CLI driventerraform plan or terraform apply on your machineEngineers who want the CLI with remote state and runs
API drivenYour pipeline uploads a configuration version through the APICustom CI/CD tooling

In the VCS driven workflow, a pull request triggers a speculative plan: a plan only run that shows the effect of the change and cannot be applied. Merging to the tracked branch queues a real run. A workspace connected to VCS does not accept terraform apply from the CLI; changes must come through the repository.

A run in HCP Terraform

A run moves through stages: plan, then optional cost estimation and policy checks, then apply. Run tasks can call outside tools at set points, such as before or after the plan. If auto apply is off, someone with apply permission must confirm the plan before it is applied.

Every run, its logs and the state it produced stay in the workspace history. Each workspace also selects the Terraform version its runs use, and you upgrade it in the workspace settings.

Logging in from the CLI

terraform login

terraform login opens a browser so you can create an API token, then saves it in the CLI credentials file (credentials.tfrc.json). For Terraform Enterprise, pass the hostname, as in terraform login tfe.example.com. Avoid putting tokens in configuration.

The cloud block

terraform {
  cloud {
    organization = "acme"
    workspaces {
      project = "networking"
      name    = "network-prod"
    }
  }
}
ArgumentMeaning
organizationThe HCP Terraform organization
workspaces.nameOne workspace for this configuration
workspaces.tagsMatch all workspaces with these tags; cannot be combined with name
workspaces.projectThe project the workspaces belong to
hostnameDefaults to app.terraform.io; set it for Terraform Enterprise

A configuration uses the cloud block or a backend block, never both. Environment variables can fill missing values: TF_CLOUD_ORGANIZATION, TF_CLOUD_HOSTNAME, TF_CLOUD_PROJECT and TF_WORKSPACE. If you rely only on these, you still need an empty cloud {} block, and values in the block take precedence.

Migrating local state to HCP Terraform

Add the cloud block to a configuration that already has local state, then run terraform init. Terraform detects the change and offers to migrate the existing state into the HCP Terraform workspace. After migration, the workspace holds the state and you can remove the local terraform.tfstate file.

Local stateterraform.tfstateAdd cloud blockterraform initConfirm migrationWorkspace statein HCP Terraform

Execution modes

ModeWhere runs happenState
RemoteOn HCP Terraform's disposable virtual machinesIn HCP Terraform
LocalOn your own machine through the CLIStill in HCP Terraform
AgentOn HCP Terraform agents you run in your own networkIn HCP Terraform

In remote mode, terraform plan and terraform apply from the CLI stream output from the remote run, and workspace variables are used. In local mode, HCP Terraform only stores state, and workspace variables and variable sets are not evaluated. Agent mode suits private or on premises infrastructure that HCP Terraform cannot reach directly.

Sharing values between workspaces

A workspace can read another workspace's root outputs with the terraform_remote_state data source, given access. Run triggers connect workspaces so that a successful apply in a source workspace queues a run in the dependent one.

How the exam asks about this

  • "Developers must review infrastructure changes as pull requests before anything is applied." Use the VCS driven workflow; pull requests get speculative plans.
  • "You want CLI plan and apply but with state and runs in HCP Terraform." Add a cloud block, run terraform login and terraform init.
  • "Runs must reach servers in a private data center." Use agent execution mode.

What to remember for the exam

  • The three workflows are VCS driven, CLI driven and API driven.
  • Pull requests in a VCS driven workspace produce speculative plans that cannot be applied.
  • terraform login stores an API token in the CLI credentials file.
  • The cloud block takes organization and a workspaces block with name or tags, plus optional project.
  • Add the cloud block and run terraform init to migrate existing state to HCP Terraform.
  • Execution modes are remote, local and agent; state stays in HCP Terraform in all three.
Watch out

In local execution mode, workspace variables and variable sets are not used. Runs use only what your own machine provides, so credentials stored in the workspace do not apply.

Common mistakes

Using a backend block and a cloud block together. Both store state, so a configuration can contain only one of them. Pick the cloud block for HCP Terraform.

Running terraform apply against a VCS connected workspace. HCP Terraform rejects it. Commit and merge the change, or use a workspace with the CLI driven workflow.

Thinking local execution means local state. Local mode changes where the run happens. The workspace still stores the state in HCP Terraform.

Check yourself

  • What does terraform login do?

    It obtains an HCP Terraform API token through the browser and saves it in the CLI credentials file for later commands.

  • Can a speculative plan be applied?

    No. A speculative plan is a plan only run, for example on a pull request, that shows the effect of a change but cannot be applied.

  • Which execution mode runs Terraform inside your private network?

    Agent mode, which sends runs to HCP Terraform agents you install where they can reach private or on premises resources.

Topic 1 of 2 in HCP Terraform

Test yourself on HCP Terraform

The full HashiCorp Terraform Associate 004 question bank has 600 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by HashiCorp. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.