Type Constraints - Simple Types

This lesson will cover the types of variables used in Terraform.

So far, we have been setting variables and not specified the type. This means that whatever type you pass to a variable will be the type that the variable assumes. A type constraint allows you to be specific about the type of a variable. There are three simple types:

  • string
  • number
  • bool

Project example#

To specify a type constraint, use the type property when defining a variable. Let’s see an example of using the three simple types:

Terraform simple types type constraint project example

In this project, we are defining three variables a, b, and c. We are using the type parameter to define:

  • a as a string,
  • b as a bool, and
  • c as a number.

We are using defaults to set initial values for these variables so we do not have to worry about setting them using one of the techniques described earlier in this chapter. Then, we are outputting them by using outputs so we can see the values.

Using type constraint#

By using a type constraint, we make it illegal to set the variable to anything other than the type defined. So, for example, if we try to set b to "hello" or 123 and run Terraform, it will print an error saying that the value we have provided is not compatible with the type constraint.

There are a few interesting quirks with how the value you give is interpreted that are worth noting.

  • When we define the type to be bool, the following values will be valid:

    • true
    • false
    • “true”
    • "false"
    • "1" (evaluated to true)
    • "0" (evaluated to false)
  • "1" is valid but 1 (without the quotes) is not valid, which may be puzzling at first.

    The reason that this is the case is that when you define a string (by using quotes), Terraform will attempt to evaluate what is inside that string. If you do not use quotes, Terraform will just see 1 as a number and not attempt to convert it to a bool.

  • With a number, any valid number will be allowed with or without quotes.

  • If you specify quotes then you are essentially defining a string. As long as the string only contains digits, Terraform will realise this and implicitly convert the value to a number.

  • With a string, any value in quotes will be allowed.

Running the project#

Clicking the terminal will run terraform init and terraform apply commands. When prompted, type yes and press enter.

This code requires the following environment variables to execute:
access_key_id
Not Specified...
secret_access_key
Not Specified...
Terminal 1
Terminal

Click to Connect...

📝Note: Once you are done with the project, do not forget to run terraform destroy and then confirm with yes to delete all of the resources in this project.

Why use type constraint#

The fact that Terraform coalesces variables (attempts to see what a variable is) is one of the reasons that it’s a good idea to use a type constraint. Another reason is that it guides the person using the Terraform code as to what value to use for the variable and improves code readability.

Complex types#

In addition to the three simple types above, these types can be combined into the following more complex types:

  • list(<TYPE>)
  • set(<TYPE>)
  • map(<TYPE>)
  • object()
  • tuple([<TYPE>, …])

For each of the complex types, you can use another complex type or simple type where you see the word <TYPE>. So you can have a list of numbers, a list of strings, a list of maps of strings, etc.

More Complex Variables
Type constraints - List
Mark as Completed
Report an Issue