2020-11-13 03:01:48 +01:00
|
|
|
---
|
2021-11-23 00:57:25 +01:00
|
|
|
layout: "language"
|
|
|
|
page_title: "Operators - Configuration Language"
|
|
|
|
description: "Operators transform or combine expressions. Learn about arithmetic, logical, equality, and comparison operators."
|
2020-11-13 03:01:48 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
# 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:
|
|
|
|
|
|
|
|
1. `!`, `-` (multiplication by `-1`)
|
|
|
|
1. `*`, `/`, `%`
|
|
|
|
1. `+`, `-` (subtraction)
|
|
|
|
1. `>`, `>=`, `<`, `<=`
|
|
|
|
1. `==`, `!=`
|
|
|
|
1. `&&`
|
|
|
|
1. `||`
|
|
|
|
|
2020-12-05 01:15:54 +01:00
|
|
|
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`.
|
2020-11-13 03:01:48 +01:00
|
|
|
|
|
|
|
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
|
2020-12-05 01:15:54 +01:00
|
|
|
if automatic conversion is impossible.
|
2020-11-13 03:01:48 +01:00
|
|
|
|
|
|
|
## Arithmetic Operators
|
|
|
|
|
|
|
|
The arithmetic operators all expect number values and produce number values
|
|
|
|
as results:
|
|
|
|
|
|
|
|
* `a + b` returns the result of adding `a` and `b` together.
|
|
|
|
* `a - b` returns the result of subtracting `b` from `a`.
|
|
|
|
* `a * b` returns the result of multiplying `a` and `b`.
|
|
|
|
* `a / b` returns the result of dividing `a` by `b`.
|
|
|
|
* `a % b` returns the remainder of dividing `a` by `b`. This operator is
|
|
|
|
generally useful only when used with whole numbers.
|
|
|
|
* `-a` returns the result of multiplying `a` by `-1`.
|
|
|
|
|
2020-12-05 01:15:54 +01:00
|
|
|
Terraform supports some other less-common numeric operations as
|
2021-11-23 00:57:25 +01:00
|
|
|
[functions](function-calls.html). For example, you can calculate exponents
|
2020-12-05 01:15:54 +01:00
|
|
|
using
|
2021-11-23 00:57:25 +01:00
|
|
|
[the `pow` function](/docs/language/functions/pow.html).
|
2020-12-05 01:15:54 +01:00
|
|
|
|
2020-11-13 03:01:48 +01:00
|
|
|
## Equality Operators
|
|
|
|
|
|
|
|
The equality operators both take two values of any type and produce boolean
|
|
|
|
values as results.
|
|
|
|
|
|
|
|
* `a == b` returns `true` if `a` and `b` both have the same type and the same
|
|
|
|
value, or `false` otherwise.
|
|
|
|
* `a != b` is the opposite of `a == b`.
|
|
|
|
|
2020-12-05 01:15:54 +01:00
|
|
|
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.
|
|
|
|
|
2020-11-13 03:01:48 +01:00
|
|
|
## Comparison Operators
|
|
|
|
|
|
|
|
The comparison operators all expect number values and produce boolean values
|
|
|
|
as results.
|
|
|
|
|
|
|
|
* `a < b` returns `true` if `a` is less than `b`, or `false` otherwise.
|
|
|
|
* `a <= b` returns `true` if `a` is less than or equal to `b`, or `false`
|
|
|
|
otherwise.
|
|
|
|
* `a > b` returns `true` if `a` is greater than `b`, or `false` otherwise.
|
|
|
|
* `a >= b` returns `true` if `a` is greater than or equal to `b`, or `false` otherwise.
|
|
|
|
|
|
|
|
## Logical Operators
|
|
|
|
|
|
|
|
The logical operators all expect bool values and produce bool values as results.
|
|
|
|
|
|
|
|
* `a || b` returns `true` if either `a` or `b` is `true`, or `false` if both are `false`.
|
|
|
|
* `a && b` returns `true` if both `a` and `b` are `true`, or `false` if either one is `false`.
|
|
|
|
* `!a` returns `true` if `a` is `false`, and `false` if `a` is `true`.
|
2020-12-05 01:15:54 +01:00
|
|
|
|
|
|
|
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.
|