How to Use the If / Else Statement in Terraform – Examples

Terraform IF Statement

In this short article, we will take a look at the Terraform if statement (hint, it doesn’t exist) and look at a few examples. Let’s dive in!

Conditional Expressions in Terraform

condition ? true_val : false_val

Read more about conditional expressions in Terraform.

Example 1

For example, the statement below checks if the variable var.serveris set to “UbuntuServer”. If it is true, then count = 0 and will be deployed zero times. If it is set to anything else, then count = 1, and the resource will be deployed 1 time.

Note that Terraform does support traditional logical, equality, and comparison operators such as == (equal to) or != (not equal to) && (and), etc. These operators can be added together to make more complex conditionals.

count = var.server == "UbuntuServer" ? 0 : 1

Example 2

Another common use of conditional expressions is to define defaults to replace invalid values. The example below checks if the variable var.server is an empty string. If it is, then the value is “MicrosoftWindowsServer”. If not, then it is the actual value of var.server .

var.server != "" ? var.server : "MicrosoftWindowsServer"

Example 3

When creating a conditional expression, the two result types can be of any type. In the example below, we have an integer of 100 if the condition is true, and a string “UbuntuServer” if the condition is false.

var.server ? 100 : "UbuntuServer"

However, this can cause confusion as Terraform will attempt to find a type that they can both convert to and make those conversions automatically if so. In the above case, both can be converted to a String.

To avoid this, writing the condition with a specific conversion function is recommended (see below using the toString function):

var.server ? tostring(100) : "UbuntuServer"

Terraform Coalesce Function

  dev    = var.environment == "DEV" ? "uksouth" : ""
  uit 	 = var.environment == "UIT" ? "ukwest" : ""
  prod 	 = var.environment != "PROD" && var.environment != "UIT" ? "useast2" : ""
  region = coalesce(local.dev, local.uit, local.prod)

Key Points

We encourage you also to explore how Spacelift makes it easy to work with Terraform. If you need any help managing your Terraform infrastructure, building more complex workflows based on Terraform, and managing AWS credentials per run, instead of using a static pair on your local machine, Spacelift is a fantastic tool for this. If you want to learn more, create a free account today or book a demo with one of our engineers.

Note: New versions of Terraform will be placed under the BUSL license, but everything created before version 1.5.x stays open-source. OpenTofu is an open-source version of Terraform that will expand on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6. OpenTofu retained all the features and functionalities that had made Terraform popular among developers while also introducing improvements and enhancements. OpenTofu works with your existing Terraform state file, so you won’t have any issues when you are migrating to it.

Manage Terraform Better with Spacelift

Build more complex workflows based on Terraform using policy as code, programmatic configuration, context sharing, drift detection, resource visualization and many more.

Start free trial
Terraform CLI Commands Cheatsheet

Initialize/ plan/ apply your IaC, manage modules, state, and more.

Share your data and download the cheatsheet