Variables
HCP Terraform workspace variables let you customize configurations, modify Terraform's behavior, setup dynamic provider credentials, and store information like static provider credentials.
You can set variables specifically for each workspace or you can create variable sets to reuse the same variables across multiple workspaces. For example, you could define a variable set of provider credentials and automatically apply it to all of the workspaces using that provider. You can use the command line to specify variable values for each plan or apply. Otherwise, HCP Terraform applies workspace variables to all runs within that workspace.
Types
You can create both environment variables and Terraform variables in HCP Terraform.
Hands-on: Try the Create and Use a Variable Sets and Create Infrastructure tutorials to set environment and Terraform variables in HCP Terraform.
Environment Variables
HCP Terraform performs Terraform runs on disposable Linux worker VMs using a POSIX-compatible shell. Before running Terraform operations, HCP Terraform uses the export
command to populate the shell with environment variables.
Environment variables can store provider credentials and other data. Refer to your provider's Terraform Registry documentation for a full list of supported shell environment variables (e.g., authentication variables for AWS, Google Cloud Platform, and Azure). Environment variables can also modify Terraform's behavior. For example, TF_LOG
enables detailed logs for debugging.
Parallelism
You can use the TFE_PARALLELISM
environment variable when your infrastructure providers produce errors on concurrent operations or use non-standard rate limiting. The TFE_PARALLELISM
variable sets the -parallelism=<N>
flag for terraform plan
and terraform apply
(more about parallelism
). Valid values are between 1 and 256, inclusive, and the default is 10
. Terraform Cloud agents do not support TFE_PARALLELISM
, but you can specify flags as environment variables directly via TF_CLI_ARGS_name
. In these cases, use TF_CLI_ARGS_plan="-parallelism=<N>"
or TF_CLI_ARGS_apply="-parallelism=<N>"
instead.
Warning: We recommend talking to HashiCorp support before setting TFE_PARALLELISM
.
Dynamic Credentials
You can configure dynamic credentials for certain providers using environment variables at the workspace level or using variable sets.
Dynamic credentials allows for using temporary per-run credentials and eliminates the need to manually rotate secrets.
Terraform Variables
Terraform variables refer to input variables that define parameters without hardcoding them into the configuration. For example, you could create variables that let users specify the number and type of Amazon Web Services EC2 instances they want to provision with a Terraform module.
You can then reference this variable in your configuration.
If a required input variable is missing, Terraform plans in the workspace will fail and print an explanation in the log.
Scope
Each environment and Terraform variable can have one of the following scopes:
Scope | Description | Resources |
---|---|---|
Run-Specific | Apply to a specific run within a single workspace. | Specify Run-Specific Variables |
Workspace-Specific | Apply to a single workspace. | Create Workspace-Specific Variables, Loading Variables from Files, Workspace-Specific Variables API. |
Workspace-Scoped Variable Set | Apply to multiple workspaces within the same organization. | Create Variable Sets and Variable Sets API |
Project-Scoped Variable Set | Automatically applied to all current and future workspaces within a project. | Create Variable Sets and Variable Sets API |
Global Variable Set | Automatically applied to all current and future workspaces within an organization. | Create Variable Sets and Variable Sets API |
Precedence
Hands On: The Manage Multiple Variable Sets in HCP Terraform tutorial shows how to manage multiple variable sets and demonstrates variable precedence.
There may be cases when a workspace contains conflicting variables of the same type with the same key. HCP Terraform marks overwritten variables in the UI.
HCP Terraform prioritizes and overwrites conflicting variables according to the following precedence:
1. Priority Global Variable Sets
If prioritized, variables in a global variable set have precedence over all other variables with the same key.
2. Priority Project-Scoped Variable Sets
If prioritized, variables in a priority project-scoped variable set have precedence over variables with the same key set at a more specific scope.
3. Priority Workspace-Scoped Variable Sets
If prioritized, variables in a priority workspace-scoped variable set have precedence over variables with the same key set at a more specific scope.
4. Command Line Argument Variables
When using a CLI workflow, variables applied to a run with either -var
or -var-file
overwrite workspace-specific and variable set variables that have the same key.
5. Local Environment Variables Prefixed with TF_VAR_
When using a CLI workflow, local environment variables prefixed with TF_VAR_
(e.g., TF_VAR_replicas
) overwrite workspace-specific, variable set, and .auto.tfvars
file variables that have the same key.
6. Workspace-Specific Variables
Workspace-specific variables always overwrite variables from variable sets that have the same key. Refer to overwrite variables from variable sets for details.
7. Workspace-Scoped Variable Sets
Variables in workspace-scoped variable sets are only applied to a subset of workspaces in an organization.
When workspace-scoped variable sets have conflicting variables, HCP Terraform compares the variable set names and uses values from the variable set with lexical precedence. Terraform and HCP Terraform operate on UTF-8 strings, and HCP Terraform sorts variable set names based on the lexical order of Unicode code points.
For example, if you apply A_Variable_Set
and B_Variable_Set
to the same workspace, HCP Terraform will use any conflicting variables from A_Variable_Set
. This is the case regardless of which variable set has been edited most recently. HCP Terraform only considers the lexical ordering of variable set names when determining precedence.
8. Project-Scoped Variable Sets
Workspace-specific variables and workspace-scoped variable sets always take precedence over project-scoped variable sets that are applied to workspaces within a project.
Variables in project-scoped variable sets are only applied to the workspaces within the specified projects.
When project-scoped variable sets have conflicting variables, HCP Terraform compares the variable set names and uses values from the variable set with lexical precedence. Terraform and HCP Terraform operate on UTF-8 strings, and HCP Terraform sorts variable set names based the on lexical order of Unicode code points.
For example, if you apply A_Variable_Set
and B_Variable_Set
to the same project, HCP Terraform uses any conflicting variables from A_Variable_Set
. This is the case regardless of which variable set has been edited most recently. HCP Terraform only considers the lexical ordering of variable set names when determining precedence.
9. Global Variable Sets
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.
10. *.auto.tfvars
Variable Files
Variables in the HCP Terraform workspace and variables provided through the command line always overwrite variables with the same key from files ending in .auto.tfvars
.
11. terraform.tfvars
Variable File
Variables in the .auto.tfvars
files take precedence over variables in the terraform.tfvars
file.
Note: Although HCP Terraform uses variables from terraform.tfvars
, Terraform Enterprise currently ignores this file.
Precedence Example
Consider an example workspace that has the following variables applied:
Source | Scope | REGION | ACCESS_KEY | ACCESS_ID | VAR1 | KEY1 | VAR2 | KEY2 | replicas |
---|---|---|---|---|---|---|---|---|---|
1_Variable_Set | Priority Global | "us-east-1" | |||||||
2_Variable_Set | Priority Project-Scoped | "us-east-2" | |||||||
3_Variable_Set | Priority Workspace-Scoped | "us-west-1" | |||||||
Command Line Argument | Run-Specific | "us-west-2" | 9 | ||||||
Local Environment | Run-Specific | 8 | |||||||
Variables | Workspace-Specific | h | 1 | ||||||
A_Variable_Set | Workspace-Scoped | y | x | 2 | |||||
B_Variable_Set | Project-Scoped | g47fh474 | 874hf7u4 | b | z | 3 | |||
C_Variable_Set | Global | a | c | 4 |
When you trigger a run through the command line, HCP Terraform applies the following variables:
1_Variable_Set:
REGION
. For this run,REGION
equalsus-east-1
, overwriting the value in all other run-specific, workspace-specific, and variable set same key variables.Run-Specific:
replicas
from the command line. That meansreplicas
equals9
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-specificTF_VAR_replicas
value set in your local environment.Workspace-Specific:
VAR1
. For this run,VAR1
equalsh
, overwriting the value in A_Variable_Set.A_Variable_Set:
KEY1
. For this run,KEY1
equalsx
, overwriting the value in B_Variable_Set.B_Variable_Set:
ACCESS_KEY
,ACCESS_ID
, andVAR2
. For this run,VAR2
equalsz
, overwriting the value in C_Variable_Set.C_Variable_Set:
KEY2
. For this run,KEY2
equalsc
. This is a global variable set with no overwritten values.
Precedence with Priority Variable Sets
You can select to prioritize all values of the variables in a variable set. When a variable set is priority, the values take precedence over any variables with the same key set at a more specific scope.
For example, variables in a priority global variable set would take precedence over all variables with the same key.
If two priority variable sets with the same scope include the same variable key, HCP Terraform will determine precedence by the alphabetical order of the variable sets' names.
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.