Search⌘ K
AI Features

Data Types: Collection Types

Explore Terraform's collection data types to understand how lists, sets, and maps work within HCL. Learn to declare variables, manage input values, and see how type conversion ensures consistency. This lesson enhances your ability to handle complex data structures and prepares you for more advanced Terraform scripting and debugging.

Underpinning Terraform is a configuration language called HCL, which stands for HashiCorp Configuration Language. As the name suggests, it’s a language designed for configuration created by HashiCorp, the same company that created Terraform itself.

Like other configuration languages, HCL is declarative. HCL also shares some characteristics with more robust programming languages, and in this lesson, we’ll look at Terraform’s data types.

This lesson is somewhat limited to only covering data types in HCL. Most of this you could probably figure out yourself as you continue to learn Terraform if you’re already proficient in other languages. However, you can return to this lesson whenever you feel like it as a place for revision.

It’s also helpful to have a proper grounding in data types if we start using Terraform’s looping capabilities, which we look at later in the course. Finally, understanding data types in HCL is also frequently useful when debugging Terraform scripts.

Simple data types

First, we create a folder named ltthw_data_types with a vars.tf file to store some examples of the basic data types available in Terraform. Then we create some output values to match in output.tf like so:

output a_number_output { 
value = var.a_number 
} 
output a_string_output { 
value = var.a_string 
} 
output a_boolean_output { 
value = var.a_boolean 
}
Simple data type

We’ll get a prompt for an input for each of the module’s variables. At this point, it’s a good idea to experiment with different inputs to see what effect they have. For example, try inputting a string to the number variable to see what happens. Also, try to figure out which values are true and false in a ...