Terraform Conditionals

Terraform conditionals are expressions that allow you to dynamically set values based on the evaluation of specified conditions, enabling greater flexibility and adaptability in your infrastructure configuration.

conditional_expression & if-statement with count parameter

In Terraform, the conditional_expression is a built-in function that evaluates a given condition and returns one value if the condition is true and another value if the condition is false. The syntax for the conditional_expression is as follows:

condition ? value_if_true : value_if_false

A real-world example is asking the operator a yes/no question at run-time, which triggers the code to react accordingly. For example, when creating multiple virtual machines, a typical question is whether to place them in a proximity group.

The operator is asked the question like this, and they answer yes.

create_proximity_group = "yes"
  • The create_proximity_group is a variable with a boolean type
  • A local takes the variable’s value and creates a boolean response used at the resource level
  • The resource block uses the value of the count variable as a condition to determine whether or not to create the resource
  • If count equals 1, the resource is created
  • If count equals 0, the resource is not created

If the local.create_proximity_group returns a true answer based on yes being set by the operator, then that translates to 1 in the count variable, and the resource is created.


variable "create_proximity_group" {
  type        = string
  description = <<EOT
  Defines if a proximity placement group is created. If yes, then it
  is associated with an availability set.

  Options:
  - yes
  - no

  EOT

  validation {
    condition     = can(regex("^yes$|^no$", var.create_proximity_group))
    error_message = "Err: Valid options are yes or no."
  }
}

create_proximity_group = var.create_proximity_group == "yes" ? true : false

resource "azurerm_proximity_placement_group" "this" {
  count               = local.create_proximity_group ? 1 : 0
  name                = "ppg-${var.prefix}-${var.workload}"
  resource_group_name = var.resource_group_name
  location            = var.resource_group_location

  tags = local.tags
}

conditional_expression using length function

The length function is used to return the number of elements in a list or a string. It takes a single argument, which can be a list or a string, and returns an integer that represents the number of elements or characters in the input.

public_ip_address_id = length(azurerm_public_ip.this) > 0 ? azurerm_public_ip.this[0].id : null

This expression checks the length of the azurerm_public_ip.this list using the length function. If the length of the list is greater than zero (meaning that at least one public IP address was created), the expression returns the ID of the first element in the list using the [0] index operator: azurerm_public_ip.this[0].id. If the length of the list is zero (meaning that no public IP address was created), the expression returns null.

Last modified July 21, 2024: update (e2ae86c)