Skip to content

Commit e0b0896

Browse files
rbereckaritsok
andcommitted
TF-9289 document priority variable sets.
Co-authored-by: rita <[email protected]>
1 parent 54af4a4 commit e0b0896

File tree

5 files changed

+54
-13
lines changed

5 files changed

+54
-13
lines changed

website/docs/cloud-docs/api-docs/changelog.mdx

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ description: >-
99

1010
Keep track of changes to the API for Terraform Cloud and Terraform Enterprise.
1111

12+
## 2023-10-11
13+
* Add `priority` attribute to the [Variable Sets API](/terraform/cloud-docs/api-docs/variable-sets).
14+
1215
## 2023-09-25
1316
* Add `intermediate` boolean attribute to the [State Versions API](/terraform/cloud-docs/api-docs/state-versions).
1417

website/docs/cloud-docs/api-docs/variable-sets.mdx

+12-4
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ Properties without a default value are required.
2727
| `data.name` | string | | The name of the variable set. |
2828
| `data.description` | string | `""` | Text displayed in the UI to contextualize the variable set and its purpose. |
2929
| `data.global` | boolean | `false` | When true, Terraform Cloud automatically applies the variable set to all current and future workspaces in the organization. |
30+
| `data.priority` | boolean | `false` | When true, the variables in the set override any other variable values with a more specific scope, including values set on the command line. |
3031
| `data.relationships.workspaces` | array | \`[]` | Array of references to workspaces that the variable set should be assigned to. |
3132
| `data.relationships.projects` | array | \`[]` | Array of references to projects that the variable set should be assigned to. |
3233
| `data.relationships.vars` | array | \`[]` | Array of complete variable definitions that comprise the variable set. |
@@ -48,7 +49,8 @@ Terraform Cloud does not allow different global variable sets to contain conflic
4849
"attributes": {
4950
"name": "MyVarset",
5051
"description": "Full of vars and such for mass reuse",
51-
"global": false
52+
"global": false,
53+
"priority": false,
5254
},
5355
"relationships": {
5456
"workspaces": {
@@ -97,7 +99,8 @@ curl \
9799
"attributes": {
98100
"name": "MyVarset",
99101
"description": "Full of vars and such for mass reuse",
100-
"global": false
102+
"global": false,
103+
"priority": false,
101104
},
102105
"relationships": {
103106
"workspaces": {
@@ -151,6 +154,7 @@ Terraform Cloud does not allow global variable sets to contain conflicting varia
151154
| `data.name` | string | | The name of the variable set. |
152155
| `data.description` | string | | Text displayed in the UI to contextualize the variable set and its purpose. |
153156
| `data.global` | boolean | | When true, Terraform Cloud automatically applies the variable set to all current and future workspaces in the organization. |
157+
| `data.priority` | boolean | `false` | When true, the variables in the set override any other variable values set with a more specific scope, including values set on the command line. |
154158
| `data.relationships.workspaces` | array | | **Optional** Array of references to workspaces that the variable set should be assigned to. Sending an empty array clears all workspace assignments. |
155159
| `data.relationships.projects` | array | | **Optional** Array of references to projects that the variable set should be assigned to. Sending an empty array clears all project assignments. |
156160
| `data.relationships.vars` | array | | **Optional** Array of complete variable definitions to add to the variable set. |
@@ -170,7 +174,8 @@ Terraform Cloud does not allow global variable sets to contain conflicting varia
170174
"attributes": {
171175
"name": "MyVarset",
172176
"description": "Full of vars and such for mass reuse. Now global!",
173-
"global": true
177+
"global": true,
178+
"priority": true,
174179
},
175180
"relationships": {
176181
"workspaces": {
@@ -226,7 +231,8 @@ curl \
226231
"attributes": {
227232
"name": "MyVarset",
228233
"description": "Full of vars and such for mass reuse. Now global!",
229-
"global": true
234+
"global": true,
235+
"priority": true
230236
},
231237
"relationships": {
232238
"vars": {
@@ -298,6 +304,7 @@ curl \
298304
"name": "MyVarset",
299305
"description": "Full of vars and such for mass reuse",
300306
"global": false,
307+
"priority": false,
301308
"updated-at": "2023-03-06T21:48:33.588Z",
302309
"var-count": 5,
303310
"workspace-count": 2,
@@ -411,6 +418,7 @@ All of the list endpoints support pagination [with standard URL query parameters
411418
"name": "MyVarset",
412419
"description": "Full of vars and such for mass reuse",
413420
"global": false,
421+
"priority": false,
414422
"updated-at": "2023-03-06T21:48:33.588Z",
415423
"var-count": 5,
416424
"workspace-count": 2,

website/docs/cloud-docs/workspaces/variables/index.mdx

+32-9
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ When workspace-scoped variable sets have conflicting variables, Terraform Cloud
102102

103103
For example, if you apply `A_Variable_Set` and `B_Variable_Set` to the same workspace, Terraform Cloud will use any conflicting variables from `A_Variable_Set`. This is the case regardless of which variable set has been edited most recently. Terraform Cloud only considers the lexical ordering of variable set names when determining precedence.
104104

105+
If [prioritized](/terraform/cloud-docs/workspaces/variables/index#precedence-with-priority-variable-sets), variables in a workspace variable set have precedence over workspace or run applied variables with the same key.
106+
105107
### 5. Project-Scoped Variable Sets
106108

107109
Workspace-specific variables and workspace-scoped variable sets always take precedence over project-scoped variable sets that are applied to workspaces within a project.
@@ -112,10 +114,14 @@ When project-scoped variable sets have conflicting variables, Terraform Cloud co
112114

113115
For example, if you apply `A_Variable_Set` and `B_Variable_Set` to the same project, Terraform Cloud uses any conflicting variables from `A_Variable_Set`. This is the case regardless of which variable set has been edited most recently. Terraform Cloud only considers the lexical ordering of variable set names when determining precedence.
114116

117+
If prioritized [prioritized](/terraform/cloud-docs/workspaces/variables/index#precedence-with-priority-variable-sets), variables in a project variable set have precedence over project and workspace scoped variable sets, workspace applied, and run applied variables with the same key.
118+
115119
### 6. Global Variable Sets
116120

117121
Workspace and project-scoped variable sets always take precedence over global variable sets that are applied to all workspaces within an organization. Terraform does not allow global variable sets to contain variables with the same key, so they cannot conflict.
118122

123+
If prioritized [prioritized](/terraform/cloud-docs/workspaces/variables/index#precedence-with-priority-variable-sets), variables in a global variable set have precedence over all other variables with the same key.
124+
119125
### 7. `*.auto.tfvars` Variable Files
120126

121127
Variables in the Terraform Cloud workspace and variables provided through the command line always overwrite variables with the same key from files ending in `.auto.tfvars`.
@@ -130,18 +136,23 @@ Variables in the `.auto.tfvars` files take precedence over variables in the `ter
130136

131137
Consider an example workspace that has the following variables applied:
132138

133-
| Source | Scope | ACCESS_KEY | ACCESS_ID | VAR1 | KEY1 | VAR2 | KEY2 | replicas |
134-
| --------------------- | ---------------------- | ---------- | ---------- | ---- | ---- | ---- | ---- | -------- |
135-
| Command Line Argument | Run-Specific | | | | | | | `9` |
136-
| Local Environment | Run-Specific | | | | | | | `8` |
137-
| Variables | Workspace-Specific | | | `h` | | | | `1` |
138-
| A_Variable_Set | Workspace-Scoped | | | `y` | `x` | | | `2` |
139-
| B_Variable_Set | Project-Scoped | `g47fh474` | `874hf7u4` | | `b` | `z` | | `3` |
140-
| C_Variable_Set | Global | | | | | `a` | `c` | `4` |
139+
| Source | Scope | REGION | ACCESS_KEY | ACCESS_ID | VAR1 | KEY1 | VAR2 | KEY2 | replicas |
140+
| --------------------- | -------------------------- | ------------ | ---------- | ---------- | ---- | ---- | ---- | ---- | -------- |
141+
| 1_Variable_Set | Priority Global | "us-east-1" | | | | | | | |
142+
| 2_Variable_Set | Priority Project-Scoped | "us-east-2" | | | | | | | |
143+
| 3_Variable_Set | Priority Workspace-Scoped | "us-west-1" | | | | | | | |
144+
| Command Line Argument | Run-Specific | "us-west-2" | | | | | | | `9` |
145+
| Local Environment | Run-Specific | | | | | | | | `8` |
146+
| Variables | Workspace-Specific | | | | `h` | | | | `1` |
147+
| A_Variable_Set | Workspace-Scoped | | | | `y` | `x` | | | `2` |
148+
| B_Variable_Set | Project-Scoped | | `g47fh474` | `874hf7u4` | | `b` | `z` | | `3` |
149+
| C_Variable_Set | Global | | | | | | `a` | `c` | `4` |
141150

142151
When you trigger a run through the command line, Terraform Cloud applies the following variables:
143152

144-
- **Run-Specific:** `replicas` from the command line. That means `replicas` equals `9` for this run. Variables set from the command line take precedence over all other values, including the run-specific `TF_VAR_replicas` value set in your local environment.
153+
- **1_Variable_Set:** `REGION`. For this run, `REGION` equals `us-east-1`, overwriting the value in all other run-specific, workspace-specific, and variable set same key variables.
154+
155+
- **Run-Specific:** `replicas` from the command line. That means `replicas` equals `9` for this run. If a variable set is priority, it takes precedence over all other values set with a more specific scope. Here the TF_VAR_region is "us-east-1", because the variable from the priority global 1_Variable_Set takes precedence. Without priority variable sets, variables set from the command line take precedence over all other values, including the run-specific `TF_VAR_replicas` value set in your local environment.
145156

146157
- **Workspace-Specific:** `VAR1`. For this run, `VAR1` equals `h`, overwriting the value in A_Variable_Set.
147158

@@ -152,3 +163,15 @@ When you trigger a run through the command line, Terraform Cloud applies the fol
152163
- **C_Variable_Set:** `KEY2`. For this run, `KEY2` equals `c`. This is a global variable set with no overwritten values.
153164

154165
![An example scenario demonstrating variable precedence in Terraform Cloud](/img/docs/variable-precedence-example.png)
166+
167+
168+
## Precedence with Priority Variable Sets
169+
170+
You can select to prioritize all values of the variables in a variable set.
171+
When a variable set is priority, the values take precedence over any variables with the same key set at a more specific scope.
172+
173+
For example, variables in a priority global variable set would take precedence over all variables with the same key in a priority project or workspace scoped variable set. Variables in a priority variable set will take precedence over any variables with the same key set through the CLI on a run.
174+
175+
If two priority variable sets with the same scope include the same variable key, Terraform Cloud will determine precedence by the alphabetical order of the variable sets' names.
176+
177+
While a priority variable set can enforce that Terraform variables use designated values, it does not guarantee that the configuration uses the variable. A user can still directly modify the Terraform configuration to remove usage of a variable and replace it with a hard-coded value. For stricter enforcement, we recommend using policy checks or run tasks.

website/docs/cloud-docs/workspaces/variables/managing-variables.mdx

+7
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ To overwrite a variable from a variable set, [create a new workspace-specific va
148148

149149
Variables within a variable set can also automatically overwrite variables with the same key in other variable sets applied to the same workspace. Though variable sets are created for the organization, these overwrites occur within each workspace. Refer to [variable precedence](/terraform/cloud-docs/workspaces/variables#precedence) for more details.
150150

151+
## Priority Variable Sets
152+
153+
The values in priority variable sets overwrite any variables with the same key set at more specific scopes. This includes variables set using command line flags, or through`.*auto.tfvars` and `terraform.tfvars` files.
154+
155+
It is still possible for a user to directly modify the Terraform configuration and remove usage of a variable and replace it with a hard coded value. If you are looking for stricter enforcement than this, we recommend using policy checks or run tasks.
156+
Refer to [variable precedence](/terraform/cloud-docs/workspaces/variables#precedence-with-priority-variable-sets) for more details.
157+
151158
## Variable Values and Format
152159

153160
The limits, allowable values, and required format are the same for both workspace-specific variables and variable sets.
Loading

0 commit comments

Comments
 (0)