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
If you are used to traditional programming languages such as C#, Python, Java, etc., you’ll be familiar with the concept of if / else statements. Terraform has no if or else statement but instead uses ternary conditional operators.
What is the conditional structure of Terraform?
The syntax of a conditional expression is as follows:
condition ? true_val : false_valA conditional expression uses the value of a boolean expression to select one of two values. This expression evaluates to true_val if the value of condition is true, and otherwise, to false_val. This is the equivalent of an If-statement.
This logic is particularly useful when fed into the Terraform count statement to deploy multiple of resources. In Terraform, deploying 0 resources is also fine if the condition is not met.
Read more about conditional expressions in Terraform.
Example 1 - Check if a variable is null in Terraform
To check if a variable is null, run:
variable "optional_tag" {
type = string
default = null
}
locals {
has_optional_tag = var.optional_tag != null
}
output "has_optional_tag" {
value = local.has_optional_tag
}Instead of using a ternary to return true or false, you can rely on the expression var.optional_tag != null, which already evaluates to a boolean. In the example above, has_optional_tag is true whenever optional_tag is set to a non-null value, and false when it’s null or not explicitly provided, reflecting how Terraform treats null as “not provided” in many contexts.
Example 2 - Run a Terraform block if the condition is true
The example below will create the null resource if the condition is true, and because two is greater than one, this will be true.
variable "create_resource" {
type = bool
default = true
}
resource "null_resource" "this" {
count = var.create_resource ? 1 : 0
}This is a common Terraform pattern: you use a boolean variable and a conditional expression to control whether a resource is created (count = 1) or skipped (count = 0). It works equally well for real resources such as S3 buckets, security groups, or virtual machines.
Example 3 - Check if a variable is set to a defined value
For example, the statement below checks if the variable var.server is 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 : 1When you change the condition so that count switches between 0 and 1, Terraform will destroy or create that resource accordingly. This is expected, but it also means that using count for conditionals can change resource identities over time.
Example 4 - Define defaults to replace invalid values
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 5 - Different result types
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"Example 6 - If statements inside dynamic blocks
In this example, our security list has egress security rules only if each.value.enable_egress is set to true, otherwise, the dynamic block won’t be generated.
resource "oci_core_security_list" "this" {
for_each = var.sl_params
compartment_id = oci_core_virtual_network.this[each.value.vcn_name].compartment_id
vcn_id = oci_core_virtual_network.this[each.value.vcn_name].id
display_name = each.value.display_name
dynamic "egress_security_rules" {
iterator = egress_rules
for_each = each.value.enable_egress ? each.value.egress_rules : []
content {
stateless = egress_rules.value.stateless
protocol = egress_rules.value.protocol
destination = egress_rules.value.destination
}
}
}Terraform Coalesce Function
One way to create an equivalent to the if / else / elseif statement is to use the coalesce function. coalesce takes any number of arguments and returns the first one that isn’t null or an empty string. When using coalesce all of the arguments must be of the same type.
The example below combines checks the value of var.environment for DEV, UIT and PROD, and uses its value to select a region.
locals{
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)
}Read more about Terraform Locals: What Are They, How to Use Them.

As it pursues its mission to transform grocery delivery logistics technology, Picnic Technologies wants to free its infrastructure team to do impactful work. Spacelift helps them to create the infrastructure they need, without the pain of manual Terraform processes. Now developers can work efficiently on more enjoyable work.
Key Points
Terraform uses conditional expression logic to build If statements. This can be combined with other functions to form more complex expressions.
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 are 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 expands on Terraform’s existing concepts and offerings. It is a viable alternative to HashiCorp’s Terraform, being forked from Terraform version 1.5.6.
I hope you enjoyed this article!
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.
Frequently asked questions
Does Terraform support traditional if/else statements?
Terraform doesn’t have classic if/else blocks; instead, it uses conditional (ternary) expressions. These let you choose between two values based on a boolean condition.
How do I write an if statement in Terraform?
You use the ternary syntax: condition ? value_if_true : value_if_false. The condition must be a boolean expression, and both results should be of the same type.
Can I conditionally create a resource in Terraform?
Yes, you can use a conditional expression with count (or for_each) to control whether a resource is created. A common pattern is count = var.enabled ? 1 : 0.
