terraform expressions

Core Idea

Terraform’s conditional is the ternary operator: condition ? true_part : false_part, used to test values and vary configuration on the fly.

  • How the three operands resolve to a Boolean and one of the two values.
  • When to use it: testing for a variable’s value, and configuring settings based on conditions.
  • Usage: creating a resource with a conditional expression.

Ternary operator

The condition operand is any expression whose value resolves to a Boolean, like article == engaging. true_part is the value returned when the condition evaluates to true. false_part is the value when the condition evaluates to false.

account_tier = var.environment == "dev" ? "Standard" : "Premium"
Condition?true part:false part
If the environment variable is equal to “dev”thenassign the value “Standard” to the account_tier attributeelseassign “Premium”

If the data types are different, terraform will attempt to automatically convert them to a common type.
For example, Terraform will automatically convert the result of the following expression to string since numbers can be converted to string:

count = var.allow_public == true ? 1 : "0"

When to use ?

 1. Testing for the existence of a variable’s value

A common use case for conditional expressions is to test for the existence of a variable’s value and define a default value to replace invalid values:

var.environment == "" ? "dev" : var.environment

If the value of var.environment is an empty string then set its value to “dev”, otherwise use the actual value of var.environment.

2. Configuring settings differently based on certain conditions

Conditional expressions are often used to configure settings differently based on certain conditions. In this example, a conditional expression is used to configure an Azure storage account’s access_tier attribute.

If the var.environment value is “dev”, the access tier will be set to “Cool”. Otherwise, it will be “Hot”.

resource "azurerm_storage_account" "my_storage" {
  name                            = "stmystorage"
  resource_group_name             = "rg-conditional-demo"
  location                        = "eastus"
  access_tier                     = var.environment == "dev" ? "Cool" : "Hot"
}

Usage

In Terraform, you can write a condition in absolutely any parameter of a resource, data source, output, or local.

Create a resource using a conditional expression

By default, Terraform creates one instance of a resource.

count meta-argument  instructs Terraform to create several similar objects without writing a separate block for each one. If a resource or module block includes a count argument with a whole number value, Terraform creates that many instances of the resource. Setting the count to zero results in no instances of the resource being created.

When combined with a conditional expression, count can be used to create powerful logic to control whether to create a resource.

The following example evaluates the value of the add_storage_account Boolean variable.

If it is true, count will be assigned 1. When this happens, an Azure storage account will be created. However, if add_storage_account is false, the count will be zero, and no storage account will be created.

variable "add_storage_account" {
  description = "boolean to determine whether to create a storage account or not"
  type        = bool
}
 
resource "azurerm_storage_account" "my_storage_account" {
  count = var.add_storage_account ? 1 : 0
 
  resource_group_name      = "rg-conditional-demo"
  location                 = "eastus"
  account_tier             = "Standard"
  account_replication_type = "LRS"
 
  name = "stspacelift${count.index}${local.rand_suffix}"
}