Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add add custom header in Cloudfront Lambda@Edge Origin Request?

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.

like image 497
Mihai Serban Avatar asked May 17 '26 13:05

Mihai Serban


1 Answers

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);
}
like image 64
Sumit Kathuria Avatar answered May 20 '26 16:05

Sumit Kathuria