I've written some code that should insert a hashed URI as a header I'm using for cache control. I'm fairly new to JS so I think my formatting may be off but I've borrowed a lot from the Lambda@Edge examples.
var crypto = require("crypto");
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
const etagUri = ''
const eTag = crypto.createHash('sha256').update(etagUri).digest('hex');
request.uri = etagUri;
console.log(`Request uri set to "${request.uri}"`);
callback(null, request);
const response = {
status: '200',
statusDescription: 'OK',
headers: {
'ETag': [{
key: 'ETag',
value: eTag,
}],
},
};
return response;
};
I run into an error when I test in the Lambda code editor. I see a lot of debugging with console log but since this runs in the back end I can't get it to print that stuff for me in AWS.
{
"errorType": "TypeError",
"errorMessage": "Cannot read property '0' of undefined",
"trace": [
"TypeError: Cannot read property '0' of undefined",
" at Runtime.exports.handler (/var/task/index.js:3:34)",
" at Runtime.handleOnceNonStreaming (/var/runtime/Runtime.js:73:25)"
]
}
I know it's not behaving nicely with that request const. Also when I try to deploy this through CloudFront I get a 502 The Lambda function result failed validation: The specified URI is in an invalid format. error which I assume means it can use that to generate the URI but is failing after. I'd love any additional debugging tips for working in Lambda as well!