Reputation: 34089
I have AWS Gateway REST API that takes 2 querystring parameters
https://xxxxxx.xxxx.us-east-1.amazonaws.com/dev/pets?type=dog&page=1
The caller of the API also include x-api-key
in the header. I want API gateway to pass querystring parameters and x-api-key
to lambda function. So in AWS API Gateway Console
i have configured the Integration Request
as below
The lambda function looks like this
namespace AWSLambda1
{
public class Function
{
public string FunctionHandler(LambdaRequest request, ILambdaContext context)
{
return string.Format("{0},{1},{2}", request.Type, request.Page, request.ApiKey);
}
}
}
public class LambdaRequest
{
public string Type { get; set; }
public string Page { get; set; }
public string ApiKey { get; set; }
}
Issues
1> When lambda function receives the request, the Type
and Page
properties are coming as NULL.
2>As per documentation API Gateway can map the http header using the naming convention method.request.header.{param_name}
, however when i try to set map from as method.request.header.x-api-key
it throws error
Invalid mapping expression specified: Validation Result: warnings : [], errors : [Invalid mapping expression parameter specified: method.request.header.x-api-key]
I am not sure how do i map these query string and header to C# lambda object
(Please note that i have already gone through SO post that suggest to JObject as parameter for lambda function. But it only works for me if i enable Use Lambda Proxy integration
in Integration Request
. In such case API gateway pass all the information to lambda. This might work for me but i am trying to avoid passing unwanted information to lambda function)
Upvotes: 1
Views: 2568
Reputation: 6235
Adding Full answer here.
Header Issue
First thing, you need to make sure header entry is added in Method Request
and then you can go map that in Integration Request
with mapping method.request.header.x-api-key
. The error is happening because you did not add in Method Request
section but trying to configure it in Integration Request
only.
Lambda Payload Issue
It looks like you are not using Lambda Proxy Integration
. If you use Lambda Proxy Integration then you will get full event JSON object event data to Lambda. Similar to answer given in post you have shared. This JSON object will contains headers, queryparameters, path variables, url, request body etc.,. If you want to see some sample on how it looks, just go and create API Gateway Test Event on Lambda.
Now, if you do not want to use Lambda Proxy Integration but want to limit what is being sent to Lambda then you will have to create Integration Mapping Template to send only required info to Lambda such as headers, payload, query params etc., from API Gateway.
Sample Integration Template.
{
"body" : $input.json('$'),
"headers": {
#foreach($header in $input.params().header.keySet())
"$header": "$util.escapeJavaScript($input.params().header.get($header))" #if($foreach.hasNext),#end
#end
},
"method": "$context.httpMethod",
"params": {
#foreach($param in $input.params().path.keySet())
"$param": "$util.escapeJavaScript($input.params().path.get($param))" #if($foreach.hasNext),#end
#end
},
"query": {
#foreach($queryParam in $input.params().querystring.keySet())
"$queryParam": "$util.escapeJavaScript($input.params().querystring.get($queryParam))" #if($foreach.hasNext),#end
#end
}
}
Reference -
Upvotes: 4