Skip to main content
In this lesson, we take an in-depth look at the Terraform variable block, exploring how to define variables, enforce type constraints, and work with complex data structures. Learn how to leverage Terraform’s variable capabilities to write more efficient and maintainable infrastructure code.

Basic Variable Definition

Terraform variable blocks can include several parameters, including a default value that sets a fallback for the variable. Below is an example that defines several variables with default values:
Terraform variable blocks can also include the optional parameters type and description. The description provides clarity on the variable’s purpose, while the type enforces the kind of data the variable can hold. For example:
If a type constraint is not specified, Terraform defaults to the type “Any”.

Simple Variable Types

Terraform supports several simple variable types. Here’s a quick overview:
  • String: Accepts alphanumeric values.
  • Number: Accepts numeric values (both positive and negative).
  • Boolean: Accepts values of either true or false.
In addition to these, Terraform supports more advanced types such as list, map, set, object, and tuple.

List Variables

A list is an ordered collection of values where each element can be accessed by its index (starting at 0). For example, consider a variable that uses a list of prefixes:
In this configuration:
  • var.prefix[0] returns “Mr”
  • var.prefix[1] returns “Mrs”
  • var.prefix[2] returns “Sir”

Map Variables

Maps allow you to define key-value pairs for storing related data. For example, the following variable stores file content:
To reference a specific value from this map in a resource, use the key inside square brackets:
This fetches the value “We love animals!” from the map.

List and Map Type Constraints

You can enforce type constraints on lists and maps to ensure all elements are of a specific type. For example:
If the default values do not match the declared type, Terraform will produce an error when running terraform plan or terraform apply. For instance, using a string when a number is required will trigger an error.
Similarly, you can enforce type constraints on maps:

Set Variables

Sets in Terraform are similar to lists but automatically remove duplicate elements. Consider these examples of valid set declarations:
Using duplicate values will trigger an error:
Ensure that when using sets, duplicate values are removed to avoid configuration errors.

Object Variables

Objects allow you to create complex structures by combining various data types. For example, you can define an object representing a cat with multiple attributes:
Assign default values that adhere to the defined structure:
This configuration defines a cat named Bella, complete with color, age, favorite foods, and a flag indicating she is a favorite pet.

Tuple Variables

Tuples in Terraform are like lists but allow elements of different types. The order and type of the elements are strictly defined. For example, consider this tuple variable:
This tuple expects exactly three elements: a string, a number, and a boolean. Adding an extra element or an incorrect type will produce an error:
Running terraform plan with the above configuration will generate an error indicating that the default value does not match the tuple’s required type structure.

That’s the end of this lesson. In the next section, we will explore how to work with variable types in Terraform in greater detail. For additional resources, refer to the Terraform Documentation and other related materials.

Watch Video

Practice Lab