Skip to main content
In this guide, we’ll review how to use variables in a Terraform configuration. Variables allow you to externalize configuration parameters from your core configuration file. They enable default values, descriptions, and type constraints, making your Terraform configuration more flexible and maintainable. A variable block is valid even without any defined arguments. However, if no default value is provided, you must supply a value during Terraform execution. It’s a best practice to always include a description to explain the variable’s intended purpose. Additionally, you can enforce data types using the type argument. The optional sensitive argument (defaulting to false) hides variable values during operations like terraform plan or terraform apply when set to true. Below is an example of two variable blocks that include these common arguments:
Even if a variable is marked as sensitive, its value will still be stored in the Terraform state file.

Adding Validation to Variables

Terraform allows you to define validation rules within a variable block. For example, consider an AMI variable that should always start with “ami-”. You can enforce this requirement using a validation block, where the substring function extracts the first four characters and verifies they match “ami-”. If the condition is not met, Terraform displays the provided error message.
If you supply an invalid value with the -var flag, you’ll see an error similar to the following:

Simple Variable Types

Terraform supports several simple variable types, including string, number, and boolean. Here’s how you can define variables of these types:
It’s important to note that when you specify both a type and a default value, the default must match the declared type. Terraform attempts type conversion when possible, but if the conversion fails (for example, converting the number 1 to a boolean), an error will be raised. Below is an example demonstrating type conversion:
The following example will cause an error because type conversion is not possible:

Working with Lists

A list is an ordered collection of values. In the example below, the variable servers is defined as a list containing three elements. The first element has an index of 0, the second an index of 1, and so on:
To reference a specific element in the list, use its index in square brackets. For example, you can reference the first server in a resource block:

Using Maps

Maps store data as key-value pairs, providing a way to associate specific values with named keys. The following example defines a map variable for instance types, which differentiates between production and development environments:
You can also enforce more refined constraints with maps and lists. For example, if you need a list of strings or a map of numbers, you can specify the type accordingly:
And for maps:

Understanding Sets

Sets are similar to lists in that they are collections of values, but they do not allow duplicate elements. If duplicate values are provided, Terraform will signal an error during operations. Examples of valid and invalid set declarations:

Complex Data Structures

Terraform supports complex data structures, such as objects and tuples, to manage hierarchical and heterogeneous data.

Object Type

An object allows you to combine different types within a single variable. Consider the example of a variable representing details of a cat named Bella:
You would assign values to this variable by providing each attribute as specified (e.g., name as “Bella”, color as “brown”, age as 7, food as a list of items, and favorite_pet as true).

Tuple

Unlike lists, tuples can contain elements of different types. The types of each element in a tuple are explicitly defined within square brackets. The following example illustrates a tuple variable:
In this case, the first element must be a string, the second a number, and the third a boolean. If the number or type of elements does not match the definition, Terraform will return an error. For example:

That concludes our overview of using variables in Terraform. Mastery of these variable types and validation rules can significantly enhance the flexibility and reliability of your Terraform configurations. Future articles will delve deeper into advanced Terraform configurations and best practices. For additional reading, check out these Terraform documentation resources.

Watch Video