Skip to content

Function abs()

📝 Description

The abs function returns the absolute value of a given number. In simpler terms, it removes the minus sign if the number is negative and leaves it as is if it's positive.

Note: The input must be a number (integer or float). If you pass a string that looks like a number, Terraform will try to convert it automatically.


💻 Syntax

abs(number)

💡 Examples

1. Basic Usage (Negative to Positive):

# Result: 15.45
output "abs_negative" {
  value = abs(-15.45)
}

2. Positive remains Positive:

# Result: 10
output "abs_positive" {
  value = abs(10)
}

3. Using with Variables (Real-world scenario):

Imagine you are calculating a budget offset and want to ensure the result is always a positive "distance" from zero:

variable "offset" {
  default = -50
}

locals {
  absolute_offset = abs(var.offset)
}

⚠️ Common Errors

Input Result Why?
abs("hello") Error Input must be a number, not a string.
abs(null) Error Cannot calculate absolute value of a null object.

  • [[ceil]] — Round up to the nearest integer.

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

  • Official HashiCorp Docs