Summary
Variables used with Terraform configurations can be defined in and receive values from numerous sources. Configuration files, variable files, command line flags, and Terraform Enterprise (TFE) or Terraform Cloud (TFC) can all specify variables and their values.
Variables in Terraform configuration files
Typically, Terraform configuration files are the most frequently used sources for defining variable values, namely the default values which can be overridden by other sources. This is performed using the variable
configuration block together with the default
parameter.
The following configuration is an example of a variable declared with no default. When no default is provided the value must be provided using another source. All variables that are used in Terraform configurations must at least be declared, whether or not a default value is supplied.
variable "foo" { }
The following configuration shows how to provide a default value for a variable. Frequently, default values are not overridden at all, but represent the only value intended for the variable.
variable "foo" {
default = "bar"
}
When using the terraform
command at the command line, it is possible to override these default values with a file named terraform.tfvars
, specify the values at the command line using -var
, and/or specify a differently named tfvars
file using -var-file
.
Variables in Terraform Enterprise or Terraform Cloud
When Terraform is used with TFE/TFC, the variables declared with default values in tf
files will receive their specified default values. In contrast, tfvars
files are not used by TFE/TFC, even if a terraform.tfvars
or .auto.tfvars
file is present in a set of configuration files.
Instead, when using tfvars
files with TFE/TFC, variables and their values should be pushed into the TFE/TFC environment using terraform push
. Following the initial push, variable values can be managed in TFE/TFC using either the “Variables” section of the environment online, or using terraform push
with the -overwrite
parameter (and optionally specifying the tfvars
file if it is not one of the automatically loaded file names).
Recommendations - push
, tfvars
, Terraform Enterprise or Cloud
The use of tfvars
files is more directed at running terraform
at the command line than using TFE/TFC. TFE/TFC enables teams to collaborate on environments and therefore the Variables section of the TFE/TFC environment provides a central place to have a definitive source of truth for variable values in a collaborative way. Also, TFE/TFC provides a place to keep sensitive variables and tfvars
files are rarely used with TFE/TFC.
At the command line, the automatically loaded terraform.tfvars
provides a convenient place to override all variable values where the values can be recorded (verses specifying the values at the command line). This is good for teams collaborating on configurations using just the open source terraform
tool. If there is a desire to have multiple sets of variables, for example, for using the one configuration for multiple infrastructure environments such as test, stage, and prod, then you can use multiple tfvars files, one per environment.
The equivalent of using a terraform.tfvars file
as described (at the command line when collaborating with the open source terraform
tool) with TFE/TFC is to use the Variables section in TFE/TFC. So that feature is intended to replace the functionality rather than complement it.
Therefore, TFE/TFC users are advised to store variable values in variables defined in the Terraform Enterprise or Cloud UI and/or tf
files using the default
parameter. Variables can still be managed using push
, but be aware that anything that exists in the UI overrides the defaults in the tf
files.
Using tfvars
files are not recommended for the management of test, stage, or production workflows. A first class method of managing infrastructure environments is available in Terraform called Workspaces, which is fully integrated into Terraform Enterprise or Cloud organization projects. Terraform Enterprise and Cloud has released the concept of variable sets.