Lesson 2 of 4 HCP Terraform Organization and Governance

Variable Sets and Run Triggers

Workspaces often share settings and depend on each other's outputs. This lesson covers variable sets for shared variables, and run triggers and output sharing for dependent workspaces.

Learning outcomes

After completing this lesson, you will be able to:

  • Explain variable set scopes and precedence.
  • Describe run triggers and ways to share outputs between workspaces.

Variable sets

A variable set is a named group of Terraform and environment variables that can be applied to many workspaces at once. Typical contents are shared cloud credentials, a default region or common tags. A variable set is scoped to the whole organisation (global), to selected projects or to selected workspaces. Changing a value in the set changes it for every workspace that uses it, which avoids copying credentials into dozens of workspaces.

When the same variable is defined in several places, the most specific definition wins: workspace-specific variables override variable sets, and among sets, workspace-scoped sets override project-scoped ones, which override global ones. In CLI-driven runs, run-specific values from -var, -var-file and local TF_VAR_ variables override workspace variables and variable sets. A set marked as a priority variable set overrides all of these, including workspace and run-specific variables, which lets administrators enforce a value, such as a mandatory tag.

  1. Global variable set

    Lowest precedence among sets.

  2. Project-scoped variable set

    Overrides global sets.

  3. Workspace-scoped variable set

    Overrides project-scoped sets.

  4. Workspace-specific variable

    Overrides all non-priority sets.

  5. Run-specific values in CLI-driven runs

    -var, -var-file and local TF_VAR_ values override workspace variables.

  6. Priority variable set

    Overrides everything, including workspace and run-specific variables.

Put in order

Which variable value wins?

Exam skills 8.3.2

Arrange the sources of a variable's value in HCP Terraform from lowest to highest precedence.

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

Run triggers

Infrastructure layers depend on each other: the application uses the network's subnets. A run trigger connects a source workspace to a destination workspace, so that when a run in the source applies successfully, HCP Terraform queues a run in the destination. A destination workspace can have up to 20 source workspaces. Runs queued by triggers follow the destination's own settings, so without auto-apply they still wait for approval.

The following diagram shows a run trigger and how one workspace reads another's outputs.

Chaining the network and application workspaces. A run trigger with network-prod as source queues an app-prod run after each successful network apply. The application reads subnet IDs with tfe_outputs, which exposes only outputs, and the network workspace's remote state sharing is limited to app-prod.
The workspace names are illustrative.
Scenario questions

Run triggers

Exam skills 8.3.3

Apply run triggers between workspaces.

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

Sharing outputs between workspaces

The destination reads the source's root outputs in one of two ways. The tfe_outputs data source from the hashicorp/tfe provider reads only outputs, and marks them sensitive. The terraform_remote_state data source with the remote backend reads the outputs but needs access to the full state. The source workspace's remote state sharing setting decides which workspaces may read its state: all workspaces in the organisation, or only selected ones.

data "tfe_outputs" "network" {
  organization = "example-org"
  workspace    = "network-prod"
}

resource "aws_instance" "app" {
  subnet_id = data.tfe_outputs.network.values.private_subnet_ids[0]
  # ...
}
tfe_outputs
Providerhashicorp/tfe
Access grantedOutputs only
Output valuesMarked sensitive
Choose when Recommended in HCP Terraform
terraform_remote_state
ProviderBuilt in
Access grantedFull state
Output valuesAs in the source
Choose when Other backends or legacy configurations
Scenario questions

Reading another workspace's outputs

Exam skills 8.3.3

Choose how one workspace reads another's outputs.

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

Chaining network and application

When the network team adds a subnet, the application workspace should re-plan so that its load balancer uses the new subnet. The application team should not be able to read the network state's other data.

The decisive requirements are automatic re-planning and limited access.

A run trigger with network-prod as source and app-prod as destination queues an application run after each successful network apply. The application configuration reads the subnet IDs with tfe_outputs, which exposes only outputs, and the network workspace's remote state sharing is limited to app-prod.

Check yourself

A variable is set in a global variable set and as a workspace variable. Which value applies?

Show answer

The workspace variable, unless the variable set is a priority variable set.

Exam Tips

Remember for the exam

  • Variable sets scope: global, projects or workspaces.
  • Workspace variables override non-priority sets; priority sets override all.
  • Run triggers queue a destination run after a source applies; up to 20 sources.
  • tfe_outputs reads outputs only; terraform_remote_state needs full state access.