Terraform – FotD – base64gzip()

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

What is it?

Function name: base64gzip(string)

Returns: The base64gzip function first takes a string and applies gzip compression, then encodes the compressed data using base64.

Example:

variable "base64gzip" {
  default = "1234"
}

# Returns H4sIAAAAAAAA/zI0MjYBAAAA//8BAAD//6Pg45sEAAAA
output "base64gzip " {
  value = "${base64gzip (var.base64gzip )}"
}

Example file:

##############################################
# Function: base64gzip
##############################################
provider "aws" {
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region     = "us-east-1"
}

##############################################
# Variables
##############################################
variable "base64gzip" {
  default = "1234"
}

variable "sourcefile" {
  default = "input.txt"
}

variable "aws_access_key" {}

variable "aws_secret_key" {}

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

data "local_file" "source" {
  filename = "${var.sourcefile}"
}

resource "local_file" "base64gzip" {
  content  = "${base64gzip(data.local_file.source.content)}"
  filename = "output.txt"
}

resource "aws_s3_bucket" "bucket" {
  bucket_prefix = "base64gzip"
  acl           = "public-read"
}

resource "aws_s3_bucket_object" "object" {
  bucket           = "${aws_s3_bucket.bucket.id}"
  key              = "output.txt"
  content          = "${base64gzip(data.local_file.source.content)}"
  content_encoding = "identity"
  acl              = "public-read"
}

##############################################
# Outputs
##############################################
output "file" {
  value = "https://${aws_s3_bucket.bucket.bucket_domain_name}/${aws_s3_bucket_object.object.id}"
}

output "base64gzip " {
  value = "${base64gzip (var.base64gzip )}"
}

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

#Since we are using the aws and local provider we need to initialize
terraform init -var "aws_access_key=YOURACCESSKEY" -var "aws_secret_key=YOURSECRETKEY"

#Run a plan to get prepped
terraform plan -var "aws_access_key=YOURACCESSKEY" -var "aws_secret_key=YOURSECRETKEY" -out terraform.tfplan

#Run apply to get the output
terraform apply "terraform.tfplan"

#Apply it to any file you'd like!
terraform plan -var "sourcefile=\Path\to\a\source\text\file.txt" -var "aws_access_key=YOURACCESSKEY" -var "aws_secret_key=YOURSECRETKEY" -out terraform.tfplan

terraform apply "terraform.tfplan"

Why use it?

If you have a string that needs to be base64 encoded for a web service or some other resource and it also need to be compressed, this is how you can do that. I would think primarily of sending GET requests where you are trying to embed some information in the header. If you are using the HTTP Provider to send information to a site, then you might use this function.

Lessons learned

I was combing through resources in Terraform that might use base64 gzipped files and I found that the S3 bucket object type let’s you specify the encoding of an object being added. So I created this example that will upload the string as a file. The string is encoded as base64, but once in the S3 bucket, it is decoded and stored as compressed in gzip. You can download the file using the link in the output. Copy and paste that text into a gzip decompression engine, like this one to get back the original text. The example also writes out an identical file locally, so you can remove the AWS pieces if you don’t have an account.

I thought I was done with the base64 functions, but it looks like a few new ones were added. Coming up next is base64sha256() and base64sha512(). They are basically the same function, so I figured I’d do two in a day.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.