Skip to content

Function min()

📝 Description

The min function takes one or more numbers and returns the lowest (smallest) value from the set.

Tip: Just like with max(), if you have a list of numbers, you must use the expansion symbol (...) to pass it into the function.


💻 Syntax

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

💡 Examples

1. Basic Usage:

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

2. Working with a List (Expansion):

locals {
  latency_ms = [150, 80, 240]
  best_case  = min(local.latency_ms...) # Result: 80
}

3. Setting a Hard Limit (Real-world scenario):

Ensure that the number of requested nodes never exceeds your cloud quota (e.g., 10 nodes):

variable "user_requested_nodes" { default = 15 }
locals {
  max_quota = 10
  actual_nodes = min(var.user_requested_nodes, local.max_quota) 
  # Result: 10 (it picks the lower value to prevent exceeding the quota)
}

⚠️ Comparison: min() vs max()

Input min() (Lowest) max() (Highest)
5, 15, 25 5 25
-10, -20, 0 -20 0
0.5, 0.1, 0.9 0.1 0.9

  • [[max]] — The opposite: returns the largest value.

  • [[floor]] — Round down to the nearest integer.

  • Official HashiCorp Docs