Skip to main content

Using Numeric and Type Functions

1. Using abs Function

Function Description Example Result
abs(number) Returns the absolute value of a number abs(-5) 5
abs(number) Works with floats as well abs(-3.14) 3.14

Example: Calculating Absolute Values in Variables

variable "temperature" {
  type = number
  default = -10
}

output "absolute_temperature" {
  value = abs(var.temperature)
}

2. Using ceil and floor Functions

Function Description Example Result
ceil(number) Returns the smallest integer greater than or equal to the given number ceil(3.2) 4
floor(number) Returns the largest integer less than or equal to the given number floor(3.8) 3

Example: Rounding Numbers in Terraform

resource "aws_instance" "example" {
  count = ceil(10 / 3)
}

Example: Using floor for Resource Counting

locals {
  instances_needed = floor(25 / 8)
}

3. Using max and min Functions

Function Description Example Notes
max() Returns the largest value from a list of numbers max(10, 20, 5) Accepts any number of numeric arguments
min() Returns the smallest value from a list of numbers min(10, 20, 5) Accepts any number of numeric arguments

Example: Using max() and min() in a variable

variable "instance_counts" {
  type = list(number)
  default = [5, 10, 3, 8]
}

output "max_count" {
  value = max(var.instance_counts...)
}

output "min_count" {
  value = min(var.instance_counts...)
}

Example: Using max() and min() in a conditional

resource "aws_instance" "web" {
  count = max(1, min(5, var.instance_count))
  instance_type = "t3.micro"
}

4. Using parseint Function

Function Description Example Notes
parseint Parses a string and returns an integer value parseint("123", 10) Base must be between 2 and 36

Example: Parsing Integer from String

output "number" {
  value = parseint("456", 10)
}

Example: Parsing Binary String

output "binary" {
  value = parseint("1010", 2)
}

5. Using pow and signum Functions

Function Description Signature Examples
pow Returns the base raised to the power of the exponent pow(base, exponent) Example: Calculate square pow(2, 3) // Returns 8
signum Returns the sign of a number (-1, 0, or 1) signum(number) Example: Determine sign signum(-5) // Returns -1

6. Using tostring Function

Function Description Examples
tostring() Converts a value to its string representation. Example: Converting a number to string tostring(42)
tostring() Converts a boolean to its string representation. Example: Converting a boolean to string tostring(true)
tostring() Converts a list to its string representation. Example: Converting a list to string tostring([1, 2, 3])
tostring() Converts a map to its string representation. Example: Converting a map to string tostring({key = "value"})

7. Using tonumber Function

Function Description Examples
tonumber() Converts a string or number to a number type. Example: Convert string to number
variable "port_str" {
  type = string
  default = "8080"
}

output "port_number" {
  value = tonumber(var.port_str)
}
tonumber() Supports base conversion (2-36) for strings representing numbers in different bases. Example: Convert binary string to decimal
variable "binary_str" {
  type = string
  default = "1010"
}

output "decimal_value" {
  value = tonumber(var.binary_str, 2)
}

8. Using tobool Function

Function Signature Description Examples
tobool tobool(value) Converts a value to a boolean. If the input is a string, it returns true for "1", "true", "yes", or "on". Returns false otherwise.
tobool("true")  // true
tobool("0")     // false
tobool(1)       // true

Example: Converting String to Boolean

variable "enabled" {
  type = string
  default = "true"
}

output "is_enabled" {
  value = tobool(var.enabled)
}

Example: Using in Conditionals

resource "aws_instance" "web" {
  count = tobool(var.enable_web_server) ? 1 : 0
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

9. Using tolist and toset Functions

Function Description Examples
tolist() Converts a value to a list type. If the input is already a list, it returns the same list.
tolist([1, 2, 3]) // [1, 2, 3]
tolist("hello") // ["h", "e", "l", "l", "o"]
tolist(null) // null
toset() Converts a value to a set type. If the input is already a set, it returns the same set.
toset([1, 2, 3, 2]) // [1, 2, 3]
toset("hello") // ["h", "e", "l", "o"]
toset(null) // null
Note: tolist() and toset() can be used to convert between list and set types, but they do not preserve order in sets.

10. Using tomap Function

The tomap function in Terraform converts a list or set into a map. It's particularly useful when you need to transform structured data for use in configurations that require key-value pairs.

Argument Type Description
list list(any) A list of elements to convert into a map. Each element must be a tuple or object with a key and value attribute.

Example: Converting a List to a Map

locals {
  list_of_tuples = [
    { key = "name", value = "John" },
    { key = "age", value = "30" }
  ]
  map_from_list = tomap(local.list_of_tuples)
}

Example: Using tomap with a Set

locals {
  set_of_objects = [
    { key = "env", value = "prod" },
    { key = "region", value = "us-west-1" }
  ]
  map_from_set = tomap(local.set_of_objects)
}

Was this article helpful?