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


Amazon CloudFront Function is a new feature introduced by AWS.
CloudFront Function can be written using JavaScript only.

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().

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)


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:

Works like a charm!

Leave a Reply

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