Function max()
📝 Description
The max function takes one or more numbers and returns the highest (greatest) value from the set.
Tip: If you have a list of numbers, you need to use the expansion symbol (
...) to pass it into the function.
💻 Syntax
💡 Examples
1. Basic Usage:
2. Working with a List (Expansion):
Terraform won't accept a raw list [1, 2, 3], so we expand it with ....
3. Infrastructure Guardrails (Real-world scenario):
Ensure your instance count is at least as high as your mandatory backup nodes:
variable "requested_instances" { default = 2 }
variable "min_required_nodes" { default = 5 }
locals {
final_instance_count = max(var.requested_instances, var.min_required_nodes)
# Result: 5 (it picks the higher value to satisfy the requirement)
}
⚠️ Comparison: max() vs min()
| Input | max() (Highest) | min() (Lowest) |
|---|---|---|
10, 20, 30 |
30 | 10 |
-5, -10, 0 |
0 | -10 |
1.5, 1.2, 1.9 |
1.9 | 1.2 |
🔗 Related Functions
-
[[min]] — The opposite: returns the smallest value.
-
[[abs]] — Use this if you need to compare absolute values.