Terraform — Using Ternary Operators

Rafael Medeiros
3 min readMar 17, 2023

In this blog post, we’ll explore how multiple ternary operators can be used in Terraform, and provide a use case scenario to demonstrate their effectiveness.

But What are Ternary Operators?

Ternary operators are a way to express conditional logic in a concise way. They take the form of a single expression that evaluates to one of two values, depending on whether a given condition is true or false. In Terraform, the ternary operator is expressed as “condition ? true_value : false_value”.

For example, let’s say we want to define a variable that takes a different value depending on whether a specific condition is met. We could use a ternary operator to do this, like so:

variable "example_variable" {
default = var.condition_met ? "value_if_true" : "value_if_false"
}

In this case, the value of the “example_variable” variable will be “value_if_true” if the “condition_met” variable is true, and “value_if_false” otherwise.

Let’s say now you want to deploy an Azure Virtual Machine using Terraform, but you want to make sure that the sku value for the Virtual Machine's OS disk is set to Standard_LRS unless you are deploying to a certain Azure region, where it should be set to Premium_LRS.

Here’s how you could achieve this using a ternary operator in your Terraform code:

sku = var.region == "eastus" ? "Premium_LRS" : "Standard_LRS"

In this example, we are using the ternary operator to set the value of the sku variable. If the region variable is set to "eastus", then the sku value will be "Premium_LRS". Otherwise, the sku value will always be "Standard_LRS".

Using Multiple Ternary Operators in Terraform

While a single ternary operator can be useful in many scenarios, there are times when we need to evaluate multiple conditions before setting a variable value. In these cases, we can use multiple ternary operators chained together.

For example, let’s say we want to create a virtual machine in Azure, and we need to specify the size of the machine based on a number of different factors. We could use multiple ternary operators to set the size variable based on a variety of conditions, like so:

resource "azurerm_virtual_machine" "example_vm" {
name = "example-vm"
location = var.location
resource_group_name = var.resource_group_name

vm_size = (
var.location == "eastus" ? "Standard_B2s" :
var.location == "westus" ? "Standard_D2s_v3" :
"Standard_D2_v3"
)

disk_size_gb = (
var.location == "eastus" ? 64 :
var.location == "westus" ? 128 :
32
)
}

In this example, we are using two ternary operators. The first ternary operator is used to set the value of the vm_size variable based on the value of the location variable. If the location variable is set to "eastus", then the vm_size value will be "Standard_B2s. If the location variable is set to "westus", then the vm_size value will be Standard_D2s_V3. Otherwise, the vm_size value will be Standard_D2_v3.

The second ternary operator is used to set the value of the disk_size_gb variable is also based on the value of the location variable. If the location variable is set to "eastus", then the disk_size_gb value will be 64. If the location variable is set to "westus", then the disk_size_gb value will be 128. Otherwise, the disk_size_gb value will be 128.

By using multiple ternary operators in this way, you can set multiple variables based on different conditions or inputs, making your Terraform code more flexible and adaptable to different scenarios. However, it’s important to keep your code readable and maintainable, so use ternary operators judiciously and be sure to test your code thoroughly to ensure that it works as expected.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Rafael Medeiros
Rafael Medeiros

Written by Rafael Medeiros

DevOps Engineer | CNCF Kubestronaut | 3x Azure | Cloud | Security | Devops | Another IT Professional willing to help the community

Responses (2)

Write a response

This is wrong, can't use a variable in a variable block.
You will get following error:
Variables may not be used here.

typos maybe: Otherwise, the disk_size_gb value will be 32? you meant