Lesson 2 of 3 HCP Terraform Workflows and CLI Integration

Run Workflows and Remote Operations

HCP Terraform runs Terraform in workspaces, started from version control, the CLI or the API. This lesson covers the three workflows, the stages of a run and the execution modes.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish VCS-driven, CLI-driven and API-driven workflows.
  • Describe the stages of a run and the execution modes.
  • Explain workspace variables and their categories.

Three workflows

Every run happens in a workspace, which holds one configuration's state, variables and settings. HCP Terraform supports three ways to start runs, and a workspace is set up for one of them.

WorkflowHow runs startTypical use
VCS-drivenCommits to a connected branch; pull requests start speculative plansTeams that review changes in a Git provider
CLI-driventerraform plan and terraform apply from a local terminal or CI jobExisting scripts and pipelines; local iteration with remote state
API-drivenOther tools upload configuration versions and create runs through the APICustom orchestration and integrations

In the VCS-driven workflow, the workspace is connected to a repository, branch and optionally a working directory in GitHub, GitLab, Bitbucket or Azure DevOps. A merge to the branch queues a plan. A pull request starts a speculative plan, a plan-only run that can never be applied, and its result appears as a status check on the pull request so that reviewers see the impact before merging.

The following diagram follows a change through a VCS-driven workspace.

A VCS-driven workspace. A pull request starts a speculative plan, shown as a check in GitHub, which cannot be applied; a merge to main queues a run that applies after approval; and credentials, preferably dynamic, live in the workspace, so developers never hold them.
Speculative plans cannot be applied.
Sort into groups

Which run workflow?

Exam skills 8.1.2

Identify the run workflow each situation describes.

Log in to try this activity Interactive activities are free with an account.

The stages of a run

A run moves through a fixed sequence of stages. Runs in one workspace are processed one at a time from a queue, which together with built-in state locking prevents conflicts.

  1. Pending

    The run waits in the workspace queue.

  2. Plan

    Terraform runs plan on a worker; optional pre-plan and post-plan run tasks call external services.

  3. Cost estimation and policy check

    Where enabled, estimated cost changes are shown and Sentinel or OPA policies are evaluated.

  4. Confirm

    A user with permission approves the apply, unless the workspace has auto-apply enabled.

  5. Apply

    Terraform applies the plan; pre-apply and post-apply run tasks can run around it.

Each run keeps its logs, plan, policy results and the state version it produced, which gives a full audit trail.

Execution modes and variables

A workspace's execution mode decides where Terraform runs. Remote, the default, uses disposable workers managed by HashiCorp. Agent runs on HCP Terraform agents that you deploy in your own network, for infrastructure that is not reachable from the internet. Local runs Terraform on the user's machine, with HCP Terraform storing state only; in local mode, workspace variables are not used.

Workspace variables come in two categories: Terraform variables, which set input variables like a .tfvars file, and environment variables, which are set in the run environment, typically for provider credentials such as AWS_ACCESS_KEY_ID or settings such as TF_LOG. Either can be marked sensitive, which makes it write-only: it can be used by runs and overwritten, but not read back in the UI or API. Because credentials live in the workspace, users can plan changes without holding production credentials themselves.

Speculative plan
A plan-only run, for pull requests or CLI plans, that cannot be applied.
Execution mode
Remote, agent or local: where a workspace's Terraform operations run.
HCP Terraform agent
A self-hosted process that runs workspace operations inside a private network.
Match the pairs

Execution modes and variables

Exam skills 8.1.3

Match each workspace setting with what it means.

Log in to try this activity Interactive activities are free with an account.
Worked example

Reviewing infrastructure changes

A team wants every infrastructure change reviewed in GitHub with its plan visible, applies to happen only after merge, and no developer to hold AWS credentials.

The decisive requirements are pull request plans and central credentials.

A VCS-driven workspace connected to the repository's main branch meets all three. Pull requests trigger speculative plans whose results appear as checks; merges queue runs that apply after approval; and the AWS credentials, or better, dynamic provider credentials, are configured in the workspace, so developers never need them.

Check yourself

What kind of run does a pull request start in a VCS-driven workspace?

Show answer

A speculative plan, which shows the changes but cannot be applied.

Exam Tips

Remember for the exam

  • Workflows: VCS-driven, CLI-driven, API-driven.
  • Pull requests produce speculative plans that cannot be applied.
  • Run stages: plan, cost estimation, policy check, apply.
  • Execution modes: remote (default), agent, local.