I have a Cloudfront distribution with a custom origin.
I want to use a Lambda@Edge Origin Request to modify and add some extra headers to be forwarded to my origin server.
Below is my Lambda function. The custom_header is visible in Cloudwatch logs for my Lambda, but doesn't show up in my custom server request headers :(.
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
headers['custom_header'] = [{ key: 'custom_header', value: 'custom_header' }];
return callback(null, request);
}
I expect custom_header to be visible in my Node.js route under req.headers.
Custom header can be passed through following structure.
request.origin.custom.customHeaders
Ref: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#lambda-event-structure-request
So, the code should look like .
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const headers = request.headers;
request.origin.custom.customHeaders['custom_header'] = [{ key: 'custom_header', value: 'custom_header' }];
return callback(null, request);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With