I'm trying to read a JS file (gzipped so that it can fit the lambda edge limits) locally using nodeJS and return it in the response but I get the error from title. Why is that? Is gzip body forbidden by aws edge?
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
var noCacheHeaders = {
'cache-control': [{
key: 'Cache-Control',
value: 'no-cache'
}],
'pragma': [{
key: 'Pragma',
value: 'no-cache'
}],
'content-type': [{
key: 'Content-Type',
value: 'text/html'
}]
};
if (request.uri.startsWith('/js/') === true) {
console.log("js path");
const fs = require('fs');
fs.readFile('js.gz', function(err, data) {
if (err) {
console.log(err);
// prevent caching on errors
const response = {
status: '500',
statusDescription: 'OK',
headers: noCacheHeaders,
body: "",
};
callback(null, response);
} else {
const response = {
status: '200',
statusDescription: 'OK',
headers: noCacheHeaders,//cachedHeaders,
body: data.toString(),
};
callback(null, response);
}
});
return;
}
callback(null, request);
return;
};
responseorrequestobject passed tocallback()and received from Lambda when CloudFront invokes this function. It's always a JSON API behind the scenes, and CloudFront doesn't like something about the format of that document.