Operators and conditional expressions
Choose a capacity tier using an expression
An expression computes a value from other values. An operator combines or transforms values; a conditional expression chooses between two results. Imagine a fictional service that needs two units of capacity for ordinary demand and four for heavier demand. This configuration calculates a recommendation only; it does not provision servers. All results in this chapter are predictions, not recorded Terraform runs.
variable "requests" { type = number default = 80}
variable "enabled" { type = bool default = true}
locals { large = var.requests >= 100 tier = local.large ? "large" : "small" units = var.enabled ? (local.large ? 4 : 2) : 0}
output "capacity" { value = { tier = local.tier, units = local.units, active = var.enabled && local.units > 0 }}The default request count of 80 selects small, two units and active = true. Exactly 100 selects large and four units. Disabling the service selects zero units while preserving the tier label. Each reference connects the calculation to the input or local value it uses.
Calculate numbers and read precedence
The arithmetic symbols +, -, *, / and % mean addition, subtraction, multiplication, division and remainder. Prefix - negates a number. The following independent console expressions can be read one at a time; the comments give predicted results.
2 + 3 * 4 # 14(2 + 3) * 4 # 205 / 2 # 2.517 % 5 # 2-3 + 8 # 5Multiplication, division and remainder bind more tightly than addition and subtraction. Parentheses make intended grouping explicit. Terraform numbers are not restricted to integers, but a provider argument may require a whole number. Arithmetic is not string concatenation: use interpolation to assemble names.
Compare values and combine decisions
Use == and != for equality and inequality. Numeric comparisons use <, <=, > and >=. Logical AND (&&), OR (||) and NOT (!) combine Boolean conditions. Equality does not coerce its operands to a common type. For example:
100 >= 100 # true2 == "2" # falsetrue && false # falsefalse || true # true!false # true(5 > 3) && (8 <= 8) # trueFor precedence, unary operators come before multiplicative, additive, numeric comparison, equality, AND and OR groups, in that order. Use parentheses around mixed conditions for readability. An empty list input is not necessarily equal to the empty tuple literal []; prefer a length check when you intend to test emptiness. The length function counts collection elements here; functions receive a full lesson later.
Choose compatible conditional results
The general form is condition ? when_true : when_false. The first part must resolve to a Boolean. Both alternatives need compatible result types even if only one result is selected. Terraform can convert some combinations automatically; true ? 4 : "off" produces a string rather than a number. For a stable numeric interface, choose numeric alternatives such as var.enabled ? 4 : 0. If you want a textual interface, make both alternatives strings. The conditional form is an expression, not a series of imperative commands. Do not expect an unselected branch to excuse an undeclared name or incompatible types.
Practice a threshold decision
Predict the results for 99, 100 and 101 requests, then for 101 requests with the service disabled. Change the threshold to 200 and capacity choices to 3 and 6. Explain why parentheses are useful in the nested expression. A fictional threshold is not a real capacity planning recommendation: actual sizing needs measurement. Next: references, indexing and evaluation errors.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Operators and conditional expressions — knowledge check
Check expression results and evaluation behavior.
What does 2 + 3 * 4 produce?
At exactly 100 requests, which tier is selected?
What does 2 == "2" produce?
Which expression selects numeric capacity or zero?
With requests 101 and enabled false, what is units?