Question: How to pass headers to lambda function?

I’m trying to duplicate an existing lambda function into a serverless app. I already got most of it right, but there is a configuration I can’t make.

I want to pass the X-Forwaded-For to the lambda function. I also have a request body template configured.

This is the function part on the serverless.yml:

functions:
  enpoint:
    memorySize: 128
    handler: handler.handler
    events:
      - http:
          method: POST
          path: reviews
          integration: lambda
          request:
            parameters:
              headers:
                'X-Forwarded-For': true
            passThrough: WHEN_NO_TEMPLATES
            template:
              application/json: '${file(json_mapping_template.txt)}'

          cors:
            origins:
              - '*'
            headers:
              - Content-Type
              - X-Amz-Date
              - Authorization
              - X-Api-Key
              - X-Amz-Security-Token

There was an issue with a similar problem: #466

I looked into the .serverless/cloudformation-template-update-stack.json file and saw no reference to these settings.

On the AWS console, this setting is on the API Gateway, after selecting the POST method, Method Execution panel, HTTP Request Headers.

What am I doing wrong? If the question needs more info, let me know.

It should be passed through by default, can you add a console.log statement to the function to print the event data. You can also look into the proxy that would pass this through automatically: https://serverless.com/framework/docs/providers/aws/events/apigateway/

Hey, thanks!

It was really being passed by default. It was probably my misunderstading about how api gateway works.

1 Like