How to do base64 encoding/decoding when using CloudFront function?

Total
7
Shares

Amazon CloudFront Function is a new feature introduced by AWS.
CloudFront Function can be written using JavaScript only.
https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/

Our website generates a timestamp and encode it using btoa() (Base64 encoding).
Then, website sends HTTP GET request which includes the encoded timestamp to CloudFront function.

var sending_time        = new Date().getTime();
var enc_sending_time    = btoa(sending_time);

function generate_http_request(enc_sending_time)
{
...
}

Once CloudFront function receives the HTTP request,
it should decode the timestamp using atob().

However, CloudFront Function does not support atob().
https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html

How can we do base64 encoding to an integer
and then do base64 decoding on CloudFront function side
without using btoa() and atob()? (JavaScript only)


Solution

CloudFront does not support btoa() and atob() functions,
so we cannot do Base 64 encoding/decoding using these functions.

So as an alternative, we can use the following:
https://gist.github.com/oeon/0ada0457194ebf70ec2428900ba76255

Works like a charm!

Leave a Reply

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