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
Log in to Terraform Cloud/HCP
terraform login

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

Log out from Terraform Cloud/HCP
terraform logout

# Remove saved credentials for the current host.

CLI credentials snippet
credentials "app.terraform.io" {
  token = var.tfc_token
}

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

## CI and Automation
Plan without color
terraform plan -no-color

# Disable ANSI color codes for cleaner CI logs.

Apply for CI
terraform apply -auto-approve -input=false

# Apply non-interactively in automation.

Init for CI
terraform init -input=false

# Avoid interactive backend/provider prompts in automation.

Select or create workspace
terraform workspace select dev || terraform workspace new dev

# Common automation pattern for ephemeral or environment-specific workspaces.

## Remote Run Patterns
Remote backend with prefix
terraform {
  backend "remote" {
    organization = "acme"
    workspaces {
      prefix = "networking-"
    }
  }
}

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

Custom TF_DATA_DIR in CI
export TF_DATA_DIR=$PWD/.tfdata

# Isolate Terraform local data in ephemeral build environments.

Alternate CLI config file
export TF_CLI_CONFIG_FILE=$PWD/.terraformrc

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

## Governance and Team Workflows
Save plan as pipeline artifact
terraform plan -out=tfplan && terraform show -json tfplan > tfplan.json

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

Basic validation step
terraform fmt -check -recursive && terraform init -backend=false && terraform validate

# Common fast feedback step before plan/apply.