slide

Terraform fot d ceil()

Ned Bellavance
2 min read

Cover

This is part of an ongoing series of posts documenting the built-in interpolation functions in Terraform. For more information, check out the beginning post. In this post I am going to cover the ceil() function. The example file is on GitHub here.

What is it?

Function name: ceil(float)

Returns: Rounds up to the closest integer, unless the float is already an integer value. Will attempt to perform implicit conversion of other data types to float prior to conversion.

Example:

variable "float" {
  default = 41.66666666666
}

# Returns 42
output "float" {
  value = "${ceil(var.float)}"
}

Example file:

##############################################
# Function: ceil
##############################################
##############################################
# Variables
##############################################
variable "ceil" {
  default = false
}

##############################################
# Resources
##############################################

##############################################
# Outputs
##############################################
output "ceil_output" {
  value = "${ceil(var.ceil)}"
}

Run the following from the ceil folder to get example output for a number of different cases:

terraform apply -var 'ceil=5.9'
terraform apply -var 'ceil=-5.9'
terraform apply -var 'ceil=3.1'
terraform apply -var 'ceil=-3.1'
terraform apply -var 'ceil="-1"'
terraform apply -var 'ceil="String"'
terraform apply

Why use it?

Ceil or ceiling is a fairly common mathematical device. If you are going to be manipulating float values and need an integer, ceil might suit your needs.

Lessons learned

I learned in the process that there is an implicit conversion of boolean true to float as 1 and false to float as 0. This conversion only happens when using a default value or passing the value in a tfvars file. True or false passed in the -var agrument will be treated as a string without conversion. Strings will be converted to float if it’s a number, but not if it’s regular text. Negative numbers are rounded up towards 0, so -5.9 is rounded up to -5. Fun stuff!

Coming up next is chomp() which is one of my favorite Mario characters.