Terraform | Data Types

I’ve recently been looking into how data types work in Terraform. Although this may not be considered as a deep dive but I found it useful to explain the different types and exactly how they work when specifying a variable.

So first lets discuss variables/values…

So before I delve into data types lets first explain the three main types of variables or values that can be specified either within the code of when the code is ran.

Input Variables

Input variables are supplied when the code is ran. A benefit of using input variables is that the code can be kept quite generic. If we were creating a new EC2 instance of Azure Virtual Machine, we could configure most of the specifications around machine sizing and usage but leave some information to be supplied when creating the machine, such as IP and Server name. Basically something unique.

Local Values

Local values are configured to be created either during the code run or before hand. The user running the code will not be presented to enter anything as the values will be populated automatically. The value maybe populated by querying the platform or by something that maybe created beforehand. This might be if we were building three servers and names might be auto populated by increasing the name by one each time. Server1, Server2 and finally Server3.

Output Values

Output values are simply information or values that we want presenting to the user running the code, after the code has been ran. This might the IP address that has been automatically assigned to the server. The code will build the server then present us the IP of the new server.

Data Types?

Theres three main groups for data types each having a number of data types that exist within:

Group Type Usage Example
Primitive String A word of string of characters One
Number A number 1-9 5
Boolean True or False False
Collection List An ordered group of above values 1,2,3,4,5
Set An un-ordered group of above values 1,3,7,3
Map Single or multiple value pairs One = 1

Two = 2

Structural Tuple Similar to a list but different value types can be used 1, One, 2, Two
Object Similar to Map but different value types can be used One = 1

2 = Two

3 = 3

Setting a variable

So we now know what data type we want to use, lets see how we can use what we’ve seen in Terraform code. So lets see some example code of setting a variable type:

variable "sample_variable" {
   type = string
   default = "Hello World"

The above example is setting the variable name ‘sample_variable’  to a type string (which means we’re going to be using characters) which we’re specify as ‘Hello World’ if nothing is entered.

We could then set this variable type with the following code:

sample_variable = "this is a sample"

And refer to it with:


If we look at a slightly more complex Collection variable:

variable "sample_variable" {
   type = map(string)
   default = {
      small = "small sample"
      medium = "medium sample"
      large = "large sample"

So if we look at the above sample. We again setting the variable of ‘sample_variable’ to a type of map (which means it can be either single or multiple value pairs). We can then specify with value we want in our code from the values we’re providing. Remember that one of the benefits of using Terraform is to make it easily reusable so using collection can help.

Right…so how do I use this variable

So to briefly recap, we’ve covered what data types there is and how there grouped. We’ve also briefly looked at how we can set values and make it easier to reuse code with collections.

To finish we now need to use these variable we’ve created within our code. We code set them each time we need them but this could be multiple times so that could be difficult. Instead we could reference the variable directly and, actually, change the variable to another option within the map. So lets bring back our map sample:

variable "sample_variable" {
   type = map(string)
   default = {
      small = "small sample"
      medium = "medium sample"
      large = "large sample"

If we wanted to reference our variable in our code we can do this with the following:

var.<variable name>.<map name>

We can then change the map name to small, medium or large through our code!

Terraform Data Types

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.