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.
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)}"
}
##############################################
# 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="
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.
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.
October 18, 2024
What's New in the AzureRM Provider Version 4?
August 27, 2024
Debugging the AzureRM Provider with VSCode
August 20, 2024
State Encryption with OpenTofu
August 1, 2024