In HCP Terraform, when a workspace variable and two variable sets all define `region`, which value wins, and how do you force one?
answer
- specific usually beats shared
- ownership before scope
- alphabetical order, not edit time
- one flag outranks the command line
- same key and same category
basics
~20 sA workspace-specific variable beats every ordinary variable set, and sets rank by ownership, then scope, then name order. To force a value, mark its variable set as priority, which outranks workspace variables and even command-line arguments.
solid answer
~40 sWithout priority sets, a workspace-specific variable overwrites any variable set with the same key. Among sets, project-owned ones outrank organization-owned ones, and within each owner a workspace-scoped set beats a project-scoped one, with non-priority global sets last. Two sets at the same level resolve by the lexical order of their names, so `A_Variable_Set` beats `B_Variable_Set` whichever was edited last. In a CLI-driven run, `-var`, `-var-file` and local `TF_VAR_` values overwrite all of those, and `*.auto.tfvars` files lose to everything set in the workspace. To force a value, mark the set **priority**: it then wins over more specific scopes and over command-line values. That still does not guarantee the configuration uses the variable; for that, the docs recommend policy checks or run tasks.
go deeper
Recall that a variable set is a reusable group of variables and that a workspace-specific variable overwrites a set with the same key. Know the UI flags the overwritten one.
Explain the ladder: run-specific values, workspace variables, then sets by ownership and scope, with lexical name order breaking ties. Say what a priority set changes and why edit time never matters.
Diagnose the surprising value: read the overwritten flag, check owner and scope rather than scope alone, and check the workspace's execution mode, because Local ignores all of this. Remember a queued run keeps its old values.
Decide what is centrally forced and what is delegated. Priority sets guarantee values, not use, so pair them with policy where it matters, and keep ownership at project level wherever a team can carry it.
## The objects involved In HCP Terraform, a variable is either a **Terraform variable**, which feeds an input variable, or an **environment variable**, which a worker exports into its shell. A variable can be set in several places: - **Workspace-specific:** applies to one workspace. - **Variable set:** a named group of variables reused across workspaces, for example a set of provider credentials. - **Run-specific:** supplied with one CLI-driven plan or apply. - **Files in the configuration:** `*.auto.tfvars` and `terraform.tfvars`. A variable set has a **scope** (global, project or workspace) and an **owner** (the organization or a project). A conflict only exists between variables of the same type with the same key, so a Terraform variable named `region` and an environment variable named `region` do not compete. ## The order, highest first 1. Priority variable sets, ranked internally: global, then organization-owned project-scoped, organization-owned workspace-scoped, project-owned project-scoped, project-owned workspace-scoped. 2. Command-line `-var` and `-var-file` arguments. 3. Local `TF_VAR_` environment variables, in a CLI workflow. 4. Workspace-specific variables. 5. Project-owned workspace-scoped sets. 6. Project-owned project-scoped sets. 7. Organization-owned workspace-scoped sets. 8. Organization-owned project-scoped sets. 9. Global variable sets. 10. `*.auto.tfvars` files. 11. `terraform.tfvars`. The point people miss is that, among ordinary sets, **ownership comes before scope**. A project-owned set scoped to a whole project outranks an organization-owned set scoped to one workspace. Among priority sets the ownership order reverses: organization-owned sets outrank project-owned ones, which is what lets a central team enforce a value. ## Ties When two non-priority sets sit at the same level, HCP Terraform compares their names by the lexical order of Unicode code points. `A_Variable_Set` beats `B_Variable_Set`, regardless of which was edited most recently. Global sets cannot conflict in this way, because Terraform does not allow them to contain variables with the same key. Two priority sets of the same scope and ownership resolve alphabetically too. ## A worked example The documentation's example, trimmed to three variables, for a run started from the command line: | Source | `Region` | `Var1` | `Replicas` | |---|---|---|---| | Priority global set | `us-east-1` | | | | Command-line argument | `us-west-2` | | `9` | | Local environment variable | | | `8` | | Workspace-specific variable | | `h` | `1` | | Project-owned workspace-scoped set | | `y` | `2` | | Final value | `us-east-1` | `h` | `9` | The `-var` value loses for `Region` only because a priority set holds it, and wins for `Replicas` because nothing priority does. ## Forcing a value, and its limit Marking a set **priority** applies to all of its variables, and is what a platform team reaches for when workspace owners must not override something, such as a default tag value. A priority set enforces the value of a Terraform variable, not its use. Someone can edit the configuration to stop using the variable and hard-code a value instead. For stricter enforcement, the documentation recommends policy checks or run tasks. ## When the rules do not apply - **Local execution mode:** HCP Terraform does not evaluate workspace variables or variable sets at all, so none of this ordering is in play. - **Terraform Enterprise:** it currently ignores `terraform.tfvars`, which HCP Terraform uses. - **A run in progress:** a run is locked to its variable values when it is initiated, so a change only affects future runs. ## Diagnosing a surprising value 1. Check the workspace's execution mode, because Local means none of these layers apply. 2. Open the workspace's Variables page and look for the yellow **OVERWRITTEN** flag on the variable you expected to win. 3. If the run came from a terminal, ask whether `-var`, `-var-file` or a `TF_VAR_` value on that machine supplied it. 4. Compare the owner and scope of each set holding the key, then their names if those match. 5. Check whether the run predates your change, since it keeps the values it started with. ## Using the layers well - Put shared values and credentials in a **project-owned set**, which only needs project-level permission to manage. Managing an organization-owned set takes a higher permission level, because it can be applied to any project. - Keep **workspace-specific variables** for true exceptions. HCP Terraform flags the set variable as overwritten in the UI, which makes them visible. - Mark secrets **sensitive**, which makes them write-only.
- Does a narrower variable-set scope always beat a wider one in HCP Terraform?No. Ownership is compared first among non-priority sets, so a project-owned project-scoped set outranks an organization-owned workspace-scoped set even though its scope is wider. Only within the same owner does a workspace-scoped set beat a project-scoped one, and non-priority global sets rank below all of them.
- Why would a team choose a project-owned variable set over an organization-owned one?Delegation. A project-owned set only needs permissions on that project, so a project's own admins can manage its credentials and defaults. An organization-owned set needs a higher permission level because it can be applied to any project. Use it for values that really span several projects.
- If a variable set is removed while a run is pending, does that run lose the value?No. HCP Terraform locks a run to its configuration version and variable values when the run is initiated, so changes affect only future runs. That also means a corrected value does not rescue a run already pending or awaiting apply: discard it and queue a new one.
- The most recently edited variable set wins a conflict.
- A -var argument on the command line always beats everything in HCP Terraform.
- A global variable set always wins, because it applies to every workspace.
- A priority variable set guarantees the configuration actually uses that value.
- A committed auto.tfvars file overrides a variable set in the workspace.
- Variable sets keep applying when the workspace is in Local execution mode.