Terraform Input Variables
Terraform input variables can be used to pass values from outside of the configuration or module. They are used to assign dynamic values to resource attributes.
Input variables must be declared using a variable
block in your Terraform configuration file.
Variables can then be assigned values using the command line, environment variables, interactive input, or a fallback default.
Refer to the Terraform documentation about the precedence of a variable definition when multiple assignment methods are used for the same variable.
Declaring a Variable
Each input variable must be declared in your Terraform configuration file using a variable
block.
The label after the variable
keyword is an arbitrary name for the variable, which must be unique among all variables in the same module. This name is used to assign a value to the variable from outside and to reference the variable's value from within the module.
The following example shows 4 input variables being declared in preparation for creating a user account in your Files.com site:
The name of a variable can be any valid identifier except the following: source
, version
, providers
, count
, for_each
, lifecycle
, depends_on
, locals
.
Using Input Variable Values
A variable's value can be accessed from within expressions as var.<NAME>
, where <NAME>
matches the label given in the declaration block. That is, input variables are created by a variable
block, but you reference them as attributes on an object named var
.
For example, to use the variables declared above for creating a user account:
Interactive Input of a Variable
The terraform CLI will automatically prompt you to enter the input values for declared variables.
Using the above examples, running the terraform apply
command will prompt you for the following inputs:
These manually entered values will be used for the creation of the user account resource.
Command Line Assignment of a Variable
Input variable values can be assigned on the Terraform command line using the -var
option when running the terraform plan
and terraform apply
commands:
Environment Variable Assignment of a Variable
Environment variables can used to assign values to input variables.
Terraform searches the environment of its own process for environment variables named TF_VAR_
followed by the name of a declared variable.
Windows Example
Linux/MacOS Example
Fallback Default Value of a Variable
The variable declaration can also include a default
argument. If present, the variable is considered to be optional and the default value will be used if no value is set when calling the module or running Terraform.
The default
argument requires a literal value and cannot reference other objects in the configuration.
For example, we can configure the company
input variable to be optional and have a default fallback value by using this definition in the configuration file:
Get Instant Access to Files.com
The button below will take you to our Free Trial signup page. Click on the white "Start My Free Trial" button, then fill out the short form on the next page. Your account will be activated instantly. You can dive in and start yourself or let us help. The choice is yours.