Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Access header in AWS Lambda?

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 bucket via CloudFront.

When I call lambda function using GET method. I pass "channekID" as a querystring. I want to send X-API-Key custom header as well for authorization.

I have tried lot things but did not find any solution. How to send custom header in Lambda function?? and after accessing header value in Lambda How to authorize using x-api-key ?

like image 826
Vipin Singh Avatar asked Dec 15 '15 10:12

Vipin Singh


1 Answers

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.

like image 64
Luc Hendriks Avatar answered Nov 15 '22 03:11

Luc Hendriks