Terraform HCP Remote and CI Cheat Sheet

Commands and snippets for HCP Terraform remote runs, CLI auth, automation, and CI-friendly usage.

View
StandardDetailedCompact
Export
Copy the compact sheet, download it, or print it.
Download
`D` dense toggle · `C` copy all

CLI Auth and Credentials

Authenticate Terraform CLI and configure credentials files.

Log in to Terraform Cloud/HCP

Open a browser flow to save an API token in the CLI credentials file.

bashANYterraformloginhcp
bash
terraform login
Notes

Open a browser flow to save an API token in the CLI credentials file.

Log out from Terraform Cloud/HCP

Remove saved credentials for the current host.

bashANYterraformlogouthcp
bash
terraform logout
Notes

Remove saved credentials for the current host.

CLI credentials snippet

Example `.terraformrc` / `terraform.rc` credentials block.

hclANYterraformcredentialsterraformrc
hcl
credentials "app.terraform.io" {
  token = var.tfc_token
}
Notes

Example `.terraformrc` / `terraform.rc` credentials block.

CI and Automation

Useful flags and patterns for pipelines.

Plan without color

Disable ANSI color codes for cleaner CI logs.

bashANYterraformplanno-colorci
bash
terraform plan -no-color
Notes

Disable ANSI color codes for cleaner CI logs.

Apply for CI

Apply non-interactively in automation.

bashANYterraformapplyci
bash
terraform apply -auto-approve -input=false
Notes

Apply non-interactively in automation.

Init for CI

Avoid interactive backend/provider prompts in automation.

bashANYterraforminitci
bash
terraform init -input=false
Notes

Avoid interactive backend/provider prompts in automation.

Select or create workspace

Common automation pattern for ephemeral or environment-specific workspaces.

bashANYterraformworkspaceci
bash
terraform workspace select dev || terraform workspace new dev
Notes

Common automation pattern for ephemeral or environment-specific workspaces.

Remote Run Patterns

Patterns for remote execution and uploaded configuration.

Remote backend with prefix

Map multiple CLI workspaces to remote workspaces with a shared prefix.

hclANYterraformremotebackendworkspaces
hcl
terraform {
  backend "remote" {
    organization = "acme"
    workspaces {
      prefix = "networking-"
    }
  }
}
Notes

Map multiple CLI workspaces to remote workspaces with a shared prefix.

Custom TF_DATA_DIR in CI

Isolate Terraform local data in ephemeral build environments.

bashANYterraformcitf_data_dir
bash
export TF_DATA_DIR=$PWD/.tfdata
Notes

Isolate Terraform local data in ephemeral build environments.

Alternate CLI config file

Use a project-specific CLI configuration in CI or containers.

bashANYterraformcli-configci
bash
export TF_CLI_CONFIG_FILE=$PWD/.terraformrc
Notes

Use a project-specific CLI configuration in CI or containers.

Governance and Team Workflows

Practical remote workflow patterns for teams.

Save plan as pipeline artifact

Persist both binary and JSON plan outputs for later review or policy checks.

bashANYterraformplanartifactci
bash
terraform plan -out=tfplan && terraform show -json tfplan > tfplan.json
Notes

Persist both binary and JSON plan outputs for later review or policy checks.

Basic validation step

Common fast feedback step before plan/apply.

bashANYterraformvalidatecifmt
bash
terraform fmt -check -recursive && terraform init -backend=false && terraform validate
Notes

Common fast feedback step before plan/apply.