Expression Operators
An operator is an expression that performs an operation on one or more other expressions. Operators can do one of two things:
- Combine two expression to produce a new, third expression.
- Transform a single expression to produce a new, single expression.
Operators Types
There are four main types of operators in Terraform:
- Arithmetic Operators
- Equality Operators
- Comparison Operators
- Logical Operators
1. Arithmetic Operators
Arithmetic operators perform mathematical operations on numeric values. These operators accept number values as inputs and return number values as output:
a + b
Adds a and b together.
a - b
Adds a and b together.
a * b
Adds a and b together.
a / b
Adds a and b together.
a % b
Adds a and b together.
-a
Adds a and b together.
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).
a == b
Returns true if a and b have the same type and value, false otherwise.
a != b
Returns the opposite result of a == b.
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]
3. Comparison Operators
Comparison operators are used to compare two values and return a boolean result (true and false).
< (Less than)
a < b is true if a is smaller than b.
<= (Less than or equal to)
a <= b is true if a is smaller than or equal to b.
> (Greater than)
a > b is true if a is larger than b.
>= (Greater than or equal to)
a >= b is true if a is larger than or equal to b.
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.
a || b
is true if either a or b (or both) are true.
a && b
is true only if both a and b are true.
!a
is true if a is false, and false if a is true.
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:
- Unary operators (!, -)
- Multiplication and division (*, /)
- Addition and subtraction (+, -)
- Comparison operators (<, >, <=, >=)
- Equality operators (==, !=)
- Logical AND (&&)
- Logical OR (||)
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?