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 ?
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.
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