Terraform is a popular software tool for DevOps engineers and system administrators, primarily designed for creating and managing infrastructure in the cloud. Its main feature is the ability to automate all processes related to infrastructure deployment.
In Terraform, there is a set of core elements used to describe infrastructure. These include providers, resources, data sources, modules, expressions, and variables. We have already touched on variables in our article on Managing Private IP Addresses with Terraform and discussed their use in configurations.
Variables in Terraform are special elements that allow users to store and pass values into different aspects of modules without modifying the code of the main configuration file. They provide flexibility in managing infrastructure settings and parameters, making it easier to configure and maintain.
In this guide, we will focus on Terraform variables and explain how to use them in your configuration.
You can think of variables as containers where users store information (such as the deployment region, instance types, passwords, or access keys). You define their values once, using CLI parameters or environment variables, and can then use them throughout your configuration.
To use Terraform variables, you first need to declare them. This is usually done in the variables.tf
file using the variable
block. The syntax for declaring variables looks like this:
variable "variable_name" {
list_of_arguments
}
Each variable must have a unique name. This name is used to assign a value from outside and to reference it within a module. The name can be anything, but it must not conflict with meta-arguments such as version
, providers
, locals
, etc.
Arguments for variables are optional, but you should not avoid them, as they allow you to set additional parameters. The main arguments include:
type
— specifies the type of data allowed for the variable. We will discuss possible types in detail in the section “Variable Type Restrictions”.description
— adds a description explaining the purpose and usage of the variable.default
— sets a default value for the variable.validation
— defines custom validation rules.sensitive
— marks the variable as confidential in output.nullable
— accepts two values (true
or false
) and specifies whether the variable can take a null value.We’ll go over some of these arguments in detail in the next sections.
As mentioned above, you can restrict the type of data that a variable can accept using the type
argument.
Terraform supports the following data types:
{}
;[]
.Example of specifying a variable type:
variable "region" {
type = string
}
Since input variables in a module are part of its user interface, you can briefly describe their purpose using the optional description
argument.
Example:
variable "region" {
type = string
description = "Specifies the server region"
}
Descriptions help developers and other users better understand the role of a variable and the type of values it expects.
In Terraform, you can define custom validation rules for a variable using the validation
argument.
Each validation must contain two required arguments:
condition
— an expression that returns true
if the value is valid and false
otherwise;error_message
— the message displayed to the user if condition
returns false
.Example:
variable "email" {
type = string
description = "Email address"
validation {
condition = can(regex("^\\S+@\\S+\\.\\S+$", var.email))
error_message = "Invalid email address format"
}
}
In this example, we validate the email
variable against a regular expression for correct email formatting. If validation fails, the user will see the message “Invalid email address format.”
When the sensitive
argument is set, Terraform treats the variable in a special way to prevent accidental exposure of sensitive data in plan or apply output.
Example:
variable "user" {
type = object({
name = string
role = string
})
sensitive = true
}
resource "example_resource" "example1" {
name = var.user.name
role = var.user.role
}
Any resources or other Terraform elements associated with a sensitive variable also become sensitive. As a result, sensitive values will be hidden in the output.
After declaring variables in the root module, you can assign values to them in several ways:
You can pass values to variables using the -var
parameter when running terraform plan
or terraform apply
.
Example:
terraform apply -var="variable1=value1" -var="variable2=value2"
There is no limit to how many -var
parameters you can use in one command.
You can also specify variable values in a special file that must end with .tfvars
or .tfvars.json
.
Example .tfvars
file:
variable1 = "value1"
variable2 = "value2"
This is how to use the .tfvars
file:
terraform apply -var-file="filename.tfvars"
Another method is to use environment variables with the TF_VAR_
prefix.
Example:
export TF_VAR_variable1=value1
export TF_VAR_variable2=value2
terraform apply
In this guide, we explored Terraform variables, their declaration syntax, the main arguments they support, and the methods for assigning them values. Correct use of variables will help you create a more flexible and secure infrastructure with Terraform.