Skip to content Skip to sidebar Skip to footer

Aws Lambda Function Is Misinterpreting Event Dictionary In Python?

I am trying to deploy a google calendar api to AWS Lambda. Since I was facing a problem in extracting the value from the event dictionary (created by lambda from the JSON payload o

Solution 1:

When you invoke the lambda locally or through the Lambda console, you are invoking that lambda directly and so your lambda receives exactly what you're sending.

When you invoke it through API Gateway, API Gateway creates the event object for you based on your HTTP request. It adds the HTTP headers, path, query strings, payload, etc.

Here's a summary of what you're getting as an event from an API Gateway invocation:

{
    "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"
}

Reference: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-set-up-simple-proxy.html#api-gateway-simple-proxy-for-lambda-input-format

As you can see, the body will be sent to you as a string which you can parse using json.loads().

Solution 2:

  • The request is fully packaged and sent as a single string in the 'body' key of the event dict.
  • This behaviour is different from the test console or invoking from CLI which has only the payload in the event dict meaning event.get('type') works directly.

Example code showing how to access the value of "type" key in payload:

import json

deflambda_handler(event, context):
    body_str = event.get("body", "{}")
    body_str = body_str if body_str else"{}"
    body_obj = json.loads(body_str)
    a = body_obj.get("type", "")

Solution 3:

I have a better suggestion to test. Just return the event that you are receiving in the lambda_handler.

Solution 4:

As said AWS tests Lamda internally, which leads to a solution: you need to use the whole post as AWS get it, you cannot just send some data json as you do with curl.

So, if in doubt, you can fetch the event as Lambda receive it using

import json

def lambda_handler(event, context):

    return { "statusCode": 200, "body": json.dumps(event, indent=2) }

Once saved, this lambda will return the json you'll need to paste into the testing event on AWS Lmbda page.

Post a Comment for "Aws Lambda Function Is Misinterpreting Event Dictionary In Python?"