How to Access header in AWS Lambda?

前端 未结 2 2040
孤城傲影
孤城傲影 2021-02-09 23:47

I am using AWS Api Gateway. I have created resource and method using API Gateway.

I have created Lambda function for generating signed URL in json format to access s3 bu

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-10 00:04

    You cannot access the header using Lambda. But what you can do is in the Api Gateway create a mapping template that puts a header value in the event object.

    The header should be in the $input.params(x) variable that can be used in the mapping template. See the full documentation of how to exactly integrate this.

    update: in your mapping template (under api gateway -> your endpoint -> integration request), add something like this:

    #set($inputRoot = $input.path('$'))
    {
      "apikey" : "$input.params('X-Api-Key')"
    }
    

    Now you can access the api key in the lambda function under event.apikey (I did not test this, but we use something similar in production). Note that you can do this for all header variables and also variables in the body.

提交回复
热议问题