Skip to content

How to type variables in Terraform?

How to type Terraform variables? Declaring strings, lists, booleans and objects within your Infrastructure code is possible and will save you from many mistakes. Here's our take on how to deal with it.

How to type variables in Terraform code 1

This article is an transcript from a video interview series : Ask Me Anything on Infrastructure as Code with the Author of “Infrastructure as Code – Cook book”

How to type variables in Terraform?

People often ask : can you, and should you declare variables in Terraform?

One of the biggest issues I had in my “Chef” days was that I could multiply strings by booleans which used to create very nice issues in production.

So, yes you can type variables in Terraform. Let me show you an example :

How to type variables in Terraform code 1

To go a little higher in Terraform notions, you need to declare a variable. For example here, the first one is is the Azure region for Azure, but it can be a public key, or whatever… But this declaration is not where the variable is used. It is just a declaration. You can omit the type, so you’re not forced to do it but it’s highly recommended, though.

In this declaration, you can have several items, like a description, so you and all your team members know directly what it is about, you can have a default value, you can have a boolean, a string, a map, and you can even have an object with whatever you want in it. 

For example, you can have an object with strings, lists and booleans in it. So let’s say for example, that your variable is composed of a boolean, like true, an IP address and a SSH Key. Then this variable is always going to be checked for this same combination of elements.

The idea here is to ensure that you don’t mess with what’s expected. It’s very common that you declare your variables, but it’s not there that they are used.

Let’s take for example this network address space in the network resource. The address space here is set as network address space but could be declared in the terraform.tfvars to be overridden.

variable "network_address_space" {
default = ["10.0.0.0/16"]
description = "Virtual Network Address Space"
type = list
} 
How to type variables in Terraform code 2

You can either apply this as it is, and it’s going to work because it’s declared as a list, or you can decide that you want to override this volume here in the tfvar with something that really looks like a network address space.

How to type variables in Terraform code 3

You can check it with a terraform init command. terraform init by the way inits all your dependencies like all the providers all the modules etc …

Typically, this could be a source of error. Let’s explain in some details :

Azure virtual network requires an address space to be a list. This is something that I noticed when reading the documentation but I could forget about it in the future, or one of my fellow team members could make a mistake because it’s not documented enough. So I could just write “beware it’s a list” or whatever, but it’s much better to do using code.

By ensuring the type list is written, any apply on a future mistake won’t work.

 So the question was: “Should I type my variables?” The answer is: “Hell yes!” and it’s going to save your “ass” really often because we all make mistakes.

 Seriously it really looks weird on Azure that a network address needs to be a list (I would make subnets a list or whatever) but I would never think about this to be a list so you can you can make an error.

 Maybe you want to answer true or false as a string and you shouldn’t.

Maybe you expect numbers because this variable is mapped to something else, like the result of an API or whatever… and it’s supposed to be an integer and let’s say it works all the time except the day it returns error which is a string and you probably don’t want to have a count value of error in your code. You want to ensure that the string is a string, or the expected number is a number or the boolean is the correct answer or you can have a very bad time.

So please type your variable. It’s very easy and it is all documented on the Terraform website.

Key metrics for infrastructure automation

Check your progress in your infrastructure automation journey with metrics that matter.

Maintain reliability and security while moving at developers speed.

Track inconsistencies that were invisible so far.