This article explores the Terraform variable block, including variable definition, type constraints, and complex data structures for efficient infrastructure code.
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.
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:
Copy
Ask AI
variable "filename" { default = "/root/pets.txt" type = string description = "The path of the local file"}variable "content" { default = "I love pets!" type = string description = "The content of the file"}variable "prefix" { default = "Mrs" type = string description = "The prefix to be set"}variable "separator" { default = "."}
If a type constraint is not specified, Terraform defaults to the type “Any”.
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:
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:
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:
Copy
Ask AI
variable "bella" { type = object({ name = string color = string age = number food = list(string) favorite_pet = bool })}
Assign default values that adhere to the defined structure:
Copy
Ask AI
variable "bella" { type = object({ name = string color = string age = number food = list(string) favorite_pet = bool }) default = { name = "Bella" color = "brown" age = 7 food = ["fish", "chicken", "turkey"] favorite_pet = true }}
This configuration defines a cat named Bella, complete with color, age, favorite foods, and a flag indicating she is a favorite pet.
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:
Copy
Ask AI
variable "kitty" { type = tuple([string, number, bool]) default = ["cat", 7, true, "dog"] # This will cause 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.