6

I am playing around with AWS Lambda + API Gateway + Serverless (Python). It is amazing!

So I figured that the event parameter in the function holds a lot of information including the HTTP Request information

Furthermore, I found that

queryStringParameters
body

Are the keys that hold the GET and POST parameters.

"queryStringParameters": {
  "name": "me"
},

and

"body": "------WebKitFormBoundaryXAin8CB3c0fwFfAe\r\nContent-Disposition: form-data; name=\"sex\"\r\n\r\nmale\r\n------WebKitFormBoundaryXAin8CB3c0fwFfAe--\r\n",

How could I get a hash/dictionary from the body key ?

Thanks

Prakash Raman
  • 13,319
  • 27
  • 82
  • 132
  • I think this question has been answered here before: https://stackoverflow.com/questions/34386869/how-do-i-pass-arguments-to-aws-lambda-functions-using-get-requests – Michael Anckaert Sep 07 '17 at 13:53
  • Thanks, yes, it is helpful. But I was hoping there would be an easier way in serverless.yml – Prakash Raman Sep 07 '17 at 16:07

2 Answers2

5

if you want complete freedom / full transparency in Lambda then you might want to look at Lambda proxy integration

import json

def endpoint(event, context):
# With the Lambda proxy integration, API Gateway maps the entire client request to the
# input event parameter of the backend Lambda function as follows:
# {
#     "resource": "Resource path",
#     "path": "Path parameter",
#     "httpMethod": "Incoming request's method name"
#     "headers": {Incoming request headers}
#     "queryStringParameters": {query string parameters }
#     "pathParameters":  {path parameters}
#     "stageVariables": {Applicable stage variables}
#     "requestContext": {Request context, including authorizer-returned key-value pairs}
#     "body": "A JSON string of the request payload."
#     "isBase64Encoded": "A boolean flag to indicate if the applicable request payload is Base64-encode"
# }
    body = {}
    body["event"] = event

    # With the Lambda proxy integration, API Gateway requires the backend Lambda function
    # to return output according to the following JSON format:
    # {
    #     "isBase64Encoded": true|false,
    #     "statusCode": httpStatusCode,
    #     "headers": { "headerName": "headerValue", ... },
    #     "body": "..."
    # }
    response = {
        "statusCode": 200,
        "isBase64Encoded": False,
        "headers": {"x-test-header" : "foobar"},
        "body": json.dumps(body),
    }
    return response

and in template

"paths": {
    "/{proxy+}": {
      "x-amazon-apigateway-any-method": {
        "parameters": [{
          "name": "proxy",
          "in": "path",
          "required": true,
          "type": "string"
        }],
        "produces": ["application/json"],
        "responses": {},
        "x-amazon-apigateway-integration": {
          "responses": {
            "default": {
              "statusCode": "200"
            }
          },
          "uri": "arn:aws:apigateway:us-west-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-west-2:xxxx:function:yyy/invocations",
          "passthroughBehavior": "when_no_match",
          "httpMethod": "POST",
          "cacheNamespace": "57w2aw",
          "cacheKeyParameters": [
            "method.request.path.proxy"
          ],
          "contentHandling": "CONVERT_TO_TEXT",
          "type": "aws_proxy"
        }
      }
    }
}
Nicholas
  • 452
  • 2
  • 10
-1

Please use "$input.params('key')".

Please create a body mapping template in API gateway integration response and try "$input.params('YourQueryStringKey')"

Example,

{
"name" : "$input.params('name')"
}

Then in Lambda function you can get value like below,

var name= event.name;
Vijayanath Viswanathan
  • 8,027
  • 3
  • 25
  • 43