slide

Terraform fot d log()

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 log() function. The example file is on GitHub here.

What is it?

Function name: log(value, base)

Returns: Takes a numeric value and a base numeric value and returns the logarithm of the value in the base.

Example:

variable "value" {
  default = "100"
}

# Returns 2
output "list_output" {
  value = "${log(var.value, 10)}"
}

Example file:

##############################################
# Function: log
##############################################
##############################################
# Variables
##############################################
variable "log" {
  default = 10
}

variable "base" {
  default = 10
}

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

##############################################
# Outputs
##############################################
output "log_output" {
  value = "${log(var.log,var.base)}"
}

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

terraform apply -var 'log=5' -var 'base=10'
terraform apply -var 'log=-5' -var 'base=2'
terraform apply -var 'log=5' -var 'base=-10'
terraform apply -var 'log=-5' -var 'base=-10'
terraform apply -var 'log=0' -var 'base=10'
terraform apply -var 'log=5' -var 'base=0'
terraform apply

Why use it?

You know, I had to think pretty hard to remember what a logarithm actually is. If you’re like me, here’s a quick refresher. The logarithm is the exponent that would have to be applied to the base number to get the desired value. So if you want to know the log of 100 with base 10, you would need to know what exponent to apply to 10 to get 100. That one is easy of course, you would just square 10, thus the log is 2.

Now, why would you use this in a Terraform configuration? I have no idea. I suppose if you are already using the math package in Golang for more common functions like floor or ceil, then this is super easy to add in.

Lessons Learned

I didn’t really learn anything new on this one. The function does exactly what you would expect it to do.

Coming up next is the lookup() function.