slide

Terraform – fot d – base64encode()

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 base64encode() function. The example files are on GitHub here.

What is it?

Function name: base64encode(string)

Returns: The base64encode function returns a base64 encoded value of string.

Example:

variable "base64encode" {
  default = "1234"
}

# Returns MTIzNA==
output "base64encode" {
  value = "${base64encode(var.base64encode)}"
}

Example file:

##############################################
# Function: base64encode
##############################################
##############################################
# Variables
##############################################
variable "base64encode" {}

##############################################
# Resources
##############################################
##############################################
# Outputs
##############################################
output "base64encode_output" {
  value = "${base64encode(var.base64encode)}"
}

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

#We have to get something to encode first
$fileContent = Get-Content .\textFile.txt

terraform apply -var "base64encode=$fileContent"

#Should output QUJDREVGR0hJSktMTU5PUFFSU1RVVldYWVowMTIzNDU2Nzg5
terraform apply -var "base64encode=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"

#Empty string test
terraform apply -var "base64encode="

Why use it?

If you have a string that needs to be base64 encoded for a web service or some other resource, this is how you can transform it. I would think primarily of creating URL strings that are base64 encoded to assist with data transfer. If you are using the HTTP Provider to send information to a site, then you might use this function.

Lessons learned

I have to be honest here, I didn’t really know anything about base64 encoding before I started learning about this function. In that regard, it was an excellent learning experience. If you want to know more about base64 encoding, I definitely recommend reading through the Wikipedia article.

Coming up next is base64gzip() which will round out the base64 functions.