Arithmetic and Logical Operators
An operator is a type of expression that transforms or combines one or more other expressions. Operators either combine two values in some way to produce a third result value, or transform a single given value to produce a single result.
Operators that work on two values place an operator symbol between the two
values, similar to mathematical notation: 1 + 2
. Operators that work on
only one value place an operator symbol before that value, like
!true
.
The Terraform language has a set of operators for both arithmetic and logic, which are similar to operators in programming languages such as JavaScript or Ruby.
When multiple operators are used together in an expression, they are evaluated in the following order of operations:
Use parentheses to override the default order of operations. Without
parentheses, higher levels will be evaluated first, so Terraform will interpret
1 + 2 * 3
as 1 + (2 * 3)
and not as (1 + 2) * 3
.
The different operators can be gathered into a few different groups with similar behavior, as described below. Each group of operators expects its given values to be of a particular type. Terraform will attempt to convert values to the required type automatically, or will produce an error message if automatic conversion is impossible.
Arithmetic Operators
The arithmetic operators all expect number values and produce number values as results:
a + b
returns the result of addinga
andb
together.a - b
returns the result of subtractingb
froma
.a * b
returns the result of multiplyinga
andb
.a / b
returns the result of dividinga
byb
.a % b
returns the remainder of dividinga
byb
. This operator is generally useful only when used with whole numbers.-a
returns the result of multiplyinga
by-1
.
Terraform supports some other less-common numeric operations as
functions. For example, you can calculate exponents
using
the pow
function.
Equality Operators
The equality operators both take two values of any type and produce boolean values as results.
a == b
returnstrue
ifa
andb
both have the same type and the same value, orfalse
otherwise.a != b
is the opposite ofa == b
.
Because the equality operators require both arguments to be of exactly the same type in order to decide equality, we recommend using these operators only with values of primitive types or using explicit type conversion functions to indicate which type you are intending to use for comparison.
Comparisons between structural types may produce surprising results if you
are not sure about the types of each of the arguments. For example,
var.list == []
may seem like it would return true
if var.list
were an
empty list, but []
actually builds a value of type tuple([])
and so the
two values can never match. In this situation it's often clearer to write
length(var.list) == 0
instead.
Comparison Operators
The comparison operators all expect number values and produce boolean values as results.
a < b
returnstrue
ifa
is less thanb
, orfalse
otherwise.a <= b
returnstrue
ifa
is less than or equal tob
, orfalse
otherwise.a > b
returnstrue
ifa
is greater thanb
, orfalse
otherwise.a >= b
returnstrue
ifa
is greater than or equal tob
, orfalse
otherwise.
Logical Operators
The logical operators all expect bool values and produce bool values as results.
a || b
returnstrue
if eithera
orb
istrue
, orfalse
if both arefalse
.a && b
returnstrue
if botha
andb
aretrue
, orfalse
if either one isfalse
.!a
returnstrue
ifa
isfalse
, andfalse
ifa
istrue
.
Terraform does not have an operator for the "exclusive OR" operation. If you
know that both operators are boolean values then exclusive OR is equivalent
to the !=
("not equal") operator.
The logical operators in Terraform do not short-circuit, meaning var.foo || var.foo.bar
will produce an error message if var.foo
is null
because both var.foo
and var.foo.bar
are evaluated.