Module 8: HCP Terraform

Lesson 8.2: Workspaces and Projects

What an HCP Terraform workspace contains

Component Notes
State Plus state version history. You can view earlier versions and roll back.
Variables Terraform and environment variables, optionally marked sensitive
Run history Every plan and apply, with logs, who triggered it, and the outcome
Settings Terraform version, execution mode, working directory, auto-apply, VCS connection, notifications
Permissions Team access levels
Policies and run tasks Inherited from policy sets and run-task configuration
Locking Automatic during runs, plus a manual lock in the UI

HCP Terraform workspaces vs. CLI workspaces

CLI workspace HCP Terraform workspace
Unit An extra named state in one backend A full working environment
Variables Shared configuration and tfvars Per-workspace, plus variable sets
Access control None of its own Per workspace or project, by team
Run history and audit None Yes
Typical mapping Temporary copies of one environment One workspace per environment per component, such as network-prod and app-dev

Organizing work

  • Split by component and environment. Use workspaces like network-prod and app-prod instead of one large workspace. Smaller states mean faster runs, smaller blast radius, and clearer ownership.
  • Group with projects. For example, a payments project holds the payments-* workspaces, and the payments team gets project-level access.
  • Share outputs between workspaces with the tfe_outputs data source from the tfe provider, or with terraform_remote_state if the workspace's remote state sharing settings allow it. HashiCorp recommends tfe_outputs, because it exposes outputs only.
HCL
data "tfe_outputs" "network" {
  organization = "acme"
  workspace    = "network-prod"
}

# data.tfe_outputs.network.values.vpc_id  (values are marked sensitive)
# data.tfe_outputs.network.nonsensitive_values.vpc_id
  • Run triggers connect workspaces, so that a successful apply in network-prod automatically queues a run in app-prod.