Skip to content

Terraform Cloud

Infrastructure as Code platform for collaborative infrastructure management.

Authentication

COMMANDDESCRIPTION
terraform loginLogin to Terraform Cloud
export TF_API_TOKEN=xxxSet API token
terraform logoutLogout from Terraform Cloud

Workspaces

COMMANDDESCRIPTION
tfe workspaces listList all workspaces
tfe workspaces show <workspace-id>Show workspace details
tfe workspaces create --name myworkspace --organization myorgCreate workspace
tfe workspaces delete <workspace-id>Delete workspace
tfe workspaces lock <workspace-id>Lock workspace
tfe workspaces unlock <workspace-id>Unlock workspace

Organization

COMMANDDESCRIPTION
tfe organizations listList organizations
tfe organizations show <organization-name>Show organization details
tfe organizations create-membership --email user@example.com --organization myorgAdd member

Variables

COMMANDDESCRIPTION
tfe variables list --workspace <workspace-id>List variables
tfe variables create --workspace <workspace-id> --key AWS_ACCESS_KEY_ID --value xxx --sensitiveCreate variable
tfe variables update <variable-id> --value newvalueUpdate variable
tfe variables delete <variable-id>Delete variable

Environment Variables

COMMANDDESCRIPTION
tfe workspace-variables set-sensitive <workspace-id> TF_VAR_db_password securepasswordSet sensitive variable
tfe workspace-variables set-terraform <workspace-id> TF_VERSION 1.5.0Set Terraform version

Runs

COMMANDDESCRIPTION
tfe runs list --workspace <workspace-id>List runs
tfe runs show <run-id>Show run details
tfe runs apply <run-id>Apply run
tfe runs discard <run-id>Discard run
tfe runs cancel <run-id>Cancel run
tfe runs watch <run-id>Watch run progress

State

COMMANDDESCRIPTION
tfe state-versions list --workspace <workspace-id>List state versions
tfe state-versions show <state-version-id>Show state version
tfe state-versions download <workspace-id> --output state.jsonDownload state
tfe state-versions rollback <workspace-id> --state-version <version-id>Rollback state

Configuration

Remote Backend

hcl
terraform {
  cloud {
    organization = "my-org"
    workspaces {
      name = "my-workspace"
    }
  }
}

Multiple Workspaces

hcl
terraform {
  cloud {
    organization = "my-org"
    workspaces {
      tags = ["production", "app"]
    }
  }
}

Terraform Cloud Agents

COMMANDDESCRIPTION
tfe agents list --pool <pool-id>List agents
tfe agents show <agent-id>Show agent details

Sentinel Policies

COMMANDDESCRIPTION
tfe policy-sets list --organization myorgList policy sets
tfe policies list --policy-set <policy-set-id>List policies
tfe policy-checks list --run <run-id>List policy checks

Cost Estimation

COMMANDDESCRIPTION
tfe cost-estimation show --run <run-id>Show cost estimation

VCS Integration

COMMANDDESCRIPTION
tfe oauth-tokens listList OAuth tokens
tfe oauth-clients listList OAuth clients

Teams & Access

COMMANDDESCRIPTION
tfe teams list --organization myorgList teams
tfe team-access list --workspace <workspace-id>List team access
tfe team-access add --workspace <workspace-id> --team <team-id> --role adminAdd team access

CLI Configuration

bash
# Configure Terraform Cloud
cat > ~/.terraformrc <<EOF
credentials "app.terraform.io" {
  token = "your-api-token"
}
EOF

Terraform Cloud API

Get Workspace Variables

bash
curl \
  --header "Authorization: Bearer $TOKEN" \
  --header "Content-Type: application/vnd.api+json" \
  https://app.terraform.io/api/v2/workspaces/$WORKSPACE_ID/vars

Trigger Run via API

bash
curl \
  --header "Authorization: Bearer $TOKEN" \
  --header "Content-Type: application/vnd.api+json" \
  --request POST \
  --data '{"data":{"type":"runs","attributes":{"message":"API triggered run"}}}' \
  https://app.terraform.io/api/v2/workspaces/$WORKSPACE_ID/runs

Terraform Cloud Features

Run Tasks

hcl
terraform {
  cloud {
    organization = "my-org"
    workspaces {
      name = "my-workspace"
    }
  }
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t3.micro"
  # Run tasks can validate resources before apply
}

Policy Checks

hcl
# Sentinel policy example
import "tfplan/v2" as tfplan
import "tfconfig/v2" as tfconfig

# Enforce tagging
all_resources = tfplan.resource_changes

main = rule {
  length(all_resources) > 0
  all(all_resources as _, r {
    r.change.after.tags is not null
  })
}

Best Practices

  • Use separate workspaces for different environments (dev, staging, prod)
  • Tag resources for organization and cost tracking
  • Use sensitive variables for secrets
  • Implement workspace locking to prevent concurrent runs
  • Use Sentinel policies for governance
  • Enable cost estimation for runs
  • Use VCS integration for automatic triggers
  • Review and approve changes via pull requests
  • Use workspace-level variables for environment-specific values
  • Monitor runs and notifications
  • Implement proper IAM roles for teams
  • Use agent pools for private network resources

Troubleshooting

Check Workspace Status

bash
terraform workspace show

Refresh State

bash
terraform refresh

Import Existing Resources

bash
terraform import aws_instance.example i-0123456789abcdef0

Force Unlock Workspace

bash
tfe workspaces unlock <workspace-id> --force

Debug Mode

bash
TF_LOG=DEBUG terraform apply

TIP

Use Terraform Cloud's VCS integration to automatically trigger runs on git commits for a smooth CI/CD workflow.

Released under MIT License.