Expression Operators

An operator is an expression that performs an operation on one or more other expressions. Operators can do one of two things:

Operators Types

There are four main types of operators in Terraform:

1. Arithmetic Operators

Arithmetic operators perform mathematical operations on numeric values. These operators accept number values as inputs and return number values as output:
variable "a" {
    value = 1
}
variable "b" {
    value = 90
}
null_resource "operator" {
    provisioner "local-exec" {
        command = "echo $add $sub $mul $div $mod $neg"
        environment = {
            add = var.a + var.b 
            sub = var.a - var.b 
            mul = var.a * var.b 
            div = var.a / var.b 
            mod = var.a % var.b 
            neg = -var.a 
        }
    }
}
Terraform will perform the following actions:
    # module.executor.null_resource.module_executor will be created
    + resource "null_resource" "operator" {
        + id = (known after apply)
    }
Plan: 1 to add, 0 to change, 0 to destroy.

Enter a value: yes

null_resource.operator: Creating...
null_resource.operator: Provisioning with 'local-exec'...
null_resource.operator: Executing: ["/bin/sh" "-c" "echo $add $sub $mul $div $mod $neg"]
null_resource.operator: 91 -89 90 0.0111111111 1 -1
null_resource.operator: Creation complete after 0s [id=4425172414869377883]

2. Equality Operators

The equality operators compare two values of any type and return a boolean result (true and false).
variable "a" {
    value = "Linux"
}
variable "b" {
    value = "Windows"
}
null_resource "operator" {
    provisioner "local-exec" {
        command = "echo $equal $notequal"
        environment = {
            equal = var.a == var.b 
            notequal = var.a - var.b 
        }
    }
}
Terraform will perform the following actions:
    # module.executor.null_resource.module_executor will be created
    + resource "null_resource" "operator" {
        + id = (known after apply)
    }
Plan: 1 to add, 0 to change, 0 to destroy.

Enter a value: yes

null_resource.operator: Creating...
null_resource.operator: Provisioning with 'local-exec'...
null_resource.operator: Executing: ["/bin/sh" "-c" "echo $equal $notequal"]
null_resource.operator: false true
null_resource.operator: Creation complete after 0s [id=4425172414869377883]
When using equality operators (== and !=), both arguments must be of the same type to get an accurate result. Comparing values of different types you may get unexpected results. For example, comparing a string to a number may not work as you expect it to.
When comparing structural types (like lists, tuples, maps), the comparators will only work if the types are identical. Hence A list is not equal to a tuple even if they contain exactly the same elements. For example, An empty list is not equal to an empty tuple.

3. Comparison Operators

Comparison operators are used to compare two values and return a boolean result (true and false).
variable "a" {
    value = 9
}
variable "b" {
    value = 10
}
null_resource "operator" {
    provisioner "local-exec" {
        command = "echo $less $lessequal $greater $greaterequal"
        environment = {
            less = var.a < var.b 
            lessequal = var.a <= var.b
            greater = var.a > var.b 
            greaterequal = var.a >= var.b 
        }
    }
}
Terraform will perform the following actions:
    # module.executor.null_resource.module_executor will be created
    + resource "null_resource" "operator" {
        + id = (known after apply)
    }
Plan: 1 to add, 0 to change, 0 to destroy.

Enter a value: yes

null_resource.operator: Creating...
null_resource.operator: Provisioning with 'local-exec'...
null_resource.operator: Executing: ["/bin/sh" "-c" "echo $less $lessequal $greater $greaterequal"]
null_resource.operator: true true false false
null_resource.operator: Creation complete after 0s [id=4425172414869377883]

4. Logical Operators

Logical operators operate on two bool values (true or false), returning a bool result.
variable "a" {
    value = 2
}
variable "b" {
    value = 8
}
null_resource "operator" {
    provisioner "local-exec" {
        command = "echo $less $or $and $not"
        environment = {
            or = (var.a < var.b) || (var.a > var.b) 
            and = (var.a == var.b) && (var.a < var.b)
            not = !(var.a <= var.b) 
        }
    }
}
Terraform will perform the following actions:
    # module.executor.null_resource.module_executor will be created
    + resource "null_resource" "operator" {
        + id = (known after apply)
    }
Plan: 1 to add, 0 to change, 0 to destroy.

Enter a value: yes

null_resource.operator: Creating...
null_resource.operator: Provisioning with 'local-exec'...
null_resource.operator: Executing: ["/bin/sh" "-c" "echo $or $and $not"]
null_resource.operator: true false false
null_resource.operator: Creation complete after 0s [id=4425172414869377883]

Order of Operations

The order of operations is a set of precedence rules that define which operations should be performed first when more than one different operation appears in an expression. When more than one operator is present in an expression, the order of evaluation is as follows:
This order of operation is similar to the mathematical order of operations known as PEMDAS (Parentheses, Exponents, Multiplication and Division, and Addition and Subtraction). It's expanded by the inclusion of unary operators, comparison operators, and logical operators. By following this order of operations, an expression will be evaluated as intended and should return consistent expected results.

Related Pages

Feedback

Was this page helpful?