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 pow() function. The example file is on GitHub here.
Function name: pow(base,exponent)
Returns: Takes a base value and raises it by the exponent
Example:
# Returns 8
output "pow_output" {
value = "${pow("2","3")}"
}
##############################################
# Function: pow
##############################################
##############################################
# Variables
##############################################
variable "base" {}
variable "exp" {}
##############################################
# Resources
##############################################
##############################################
# Outputs
##############################################
output "pow_output" {
value = "${pow(var.base, var.expZ)}"
}
Run the following from the pow folder to get example output for a number of different cases:
#Basic functionality
terraform apply -var 'base=2' -var 'exp=3'
terraform apply -var 'base=-1' -var 'exp=3'
terraform apply -var 'base=3.1' -var 'exp=-2'
#Gives NaN, weird, guess it doesn't like decimal exponent
terraform apply -var 'base=-3.1' -var 'exp=2.3'
#Checking maximums, 308 seems to be the max
terraform apply -var 'base=10' -var 'exp=309'
terraform apply -var 'base=10' -var 'exp=308'
This is a bit like the log function, both from a mathematical and usefulness perspective. Mathematically, this is the inverse operation of a log function. How useful this function is to deploying automated infrastructure is a bit more debatable. It is pretty easy to implement though, since the pow function is a part of the math package in Golang.
The function has an upper limit on value, so anything more than 10^308 is rendered as infinity. Since there are only about 10^82 atoms in the universe, I guess that’s more than sufficient. The function also does not like a negative base value with a decimal for the exponent. The resulting value is NaN. I checked and that is how Golang renders it as well, so that’s not an issue of implementation on Terraform’s part.
Coming up next is the replace() function.
Resourcely Guardrails and Blueprints
November 15, 2024
Deploying Azure Landing Zones with Terraform
November 12, 2024
October 18, 2024
What's New in the AzureRM Provider Version 4?
August 27, 2024