How to map querystring and header to AWS C# lambda function parameter

amazon-web-services aws-lambda aws-api-gateway

704 观看

1回复

6184 作者的声誉

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

enter image description here

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)

作者: LP13 的来源 发布者: 2018 年 12 月 28 日

回应 1


4

2370 作者的声誉

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.

enter image description here enter image description here

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 -

https://kennbrodhagen.net/2015/12/06/how-to-create-a-request-object-for-your-lambda-event-from-api-gateway/

https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html

作者: Imran 发布者: 2018 年 12 月 29 日
32x32