Skip to content

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

max(number1, number2, ...)
# Or for a list:
max(list_of_numbers...)

💡 Examples

1. Basic Usage:

# Result: 100
output "greatest" {
  value = max(12, 54, 100, 3)
}

2. Working with a List (Expansion):

Terraform won't accept a raw list [1, 2, 3], so we expand it with ....

locals {
  thresholds = [10, 50, 25]
  max_val    = max(local.thresholds...) # Result: 50
}

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

  • [[min]] — The opposite: returns the smallest value.

  • [[abs]] — Use this if you need to compare absolute values.

  • Official HashiCorp Docs