Lesson 3 of 3 HCP Terraform Workflows and CLI Integration

Configuring the CLI Integration

The Terraform CLI connects to HCP Terraform through the cloud block. This lesson covers authenticating, configuring the block, migrating state and running remote operations from a terminal.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how the CLI authenticates to HCP Terraform.
  • Interpret a cloud block and its workspace mapping.
  • Explain how state migrates to HCP Terraform and how remote operations run.

Authenticating

terraform login opens the HCP Terraform token page in a browser, or prints its URL, and asks you to paste an API token. Terraform saves the token in credentials.tfrc.json in the .terraform.d directory of your home directory and uses it for every request to app.terraform.io. terraform login HOSTNAME authenticates to Terraform Enterprise, and terraform logout removes the stored token.

In automation, where no one can paste a token, set an environment variable named after the host, with dots replaced by underscores: TF_TOKEN_app_terraform_io. Use a team token with only the permissions the pipeline needs, and store it in the CI system's secret store.

Scenario questions

Authenticating the CLI

Exam skills 8.4.1

Authenticate the Terraform CLI to HCP Terraform.

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

The cloud block

The cloud block inside the terraform block connects a working directory to HCP Terraform.

terraform {
  cloud {
    organization = "example-org"

    workspaces {
      name    = "network-prod"
      project = "networking"
    }
  }
}

The workspaces block uses name to map to exactly one workspace, or tags to map to every workspace with the given tags, in which case terraform workspace list and terraform workspace select switch between them and terraform workspace new creates tagged workspaces. project sets the project for new workspaces and limits which existing ones are used. hostname defaults to app.terraform.io and is set for Terraform Enterprise.

The cloud block replaces a backend block; a configuration cannot have both. Like a backend block, it cannot use variables, but the organisation, project and workspace can be supplied by the TF_CLOUD_ORGANIZATION, TF_CLOUD_PROJECT and TF_WORKSPACE environment variables, with TF_CLOUD_HOSTNAME for the host.

True or false

The cloud block

Exam skills 8.4.2

Check how the cloud block connects a configuration to HCP Terraform.

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

Migrating state

An existing configuration with local or backend state moves to HCP Terraform in a few steps.

  1. Authenticate

    Run terraform login, or set TF_TOKEN_app_terraform_io.

  2. Add the cloud block

    Replace any backend block with a cloud block naming the organisation and workspace.

  3. Run terraform init

    Terraform detects the change and offers to migrate the existing state into the workspace, creating the workspace if needed.

  4. Configure the workspace

    Add variables and credentials, and choose the execution mode.

  5. Remove local state

    Delete the old terraform.tfstate files once the migration is confirmed.

After migration, HCP Terraform stores and locks the state, and every state version is kept in the workspace's history.

The following diagram shows the configuration change and the migration steps.

Moving a team from S3 state and laptop runs to HCP Terraform while keeping the CLI. Each engineer runs terraform login; one replaces the S3 backend block with a cloud block naming a CLI-driven workspace and runs init, accepting the state migration. Credentials move to sensitive workspace variables, and plan and apply then run remotely and stream to the terminal.
Organisation, workspace and bucket names are illustrative.
Put in order

Moving to HCP Terraform

Exam skills 8.4.3

Arrange the steps of moving an S3-backed configuration to HCP Terraform.

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

Remote operations from the CLI

With a workspace in remote execution mode, terraform plan uploads the configuration, excluding paths listed in a .terraformignore file, starts a speculative plan on HCP Terraform and streams its output to the terminal. terraform apply starts a normal run, streams the plan, and asks for confirmation in the terminal or the UI. The run uses the workspace's variables and credentials. Values passed with -var or -var-file, and local TF_VAR_ environment variables, are sent as run-specific variables that override workspace variables with the same name; other local environment variables, such as cloud credentials, are not sent, so credentials must be configured in the workspace.

VCS workspaces and CLI applies

A workspace connected to a VCS repository rejects terraform apply from the CLI, because its configuration must come from the repository. Speculative plans from the CLI are still allowed. Use a CLI-driven workspace if applies must start from the command line.

Sort into groups

Sent to the remote run?

Exam skills 8.4.3

Decide whether each local value reaches a remote run started from the CLI.

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

Moving a team to HCP Terraform

A team stores state in S3 and runs Terraform from laptops. They create an HCP Terraform organisation and want to keep running commands from their terminals while state and runs move to HCP Terraform.

The decisive requirements are CLI-started runs and migrated state.

Each engineer runs terraform login. One engineer replaces the S3 backend block with a cloud block naming a CLI-driven workspace and runs terraform init, accepting the offer to migrate the state. The team moves AWS credentials into workspace environment variables marked sensitive. From then on, terraform plan and apply run remotely and stream to the terminal, and the S3 state is no longer used.

Check yourself

Can a configuration have both a cloud block and a backend block?

Show answer

No. The cloud block replaces the backend block.

Exam Tips

Remember for the exam

  • terraform login stores a token in credentials.tfrc.json; TF_TOKEN_app_terraform_io in automation.
  • cloud block: organization plus workspaces name or tags (and project).
  • cloud replaces backend; init migrates existing state.
  • VCS-driven workspaces reject CLI applies.