5

I'm creating a simple Lambda function to serve a POST in AWS Gateway:

import json
import boto3
from datetime import datetime


def lambda_handler(event,context):
    wf = event['WF']
    
    if wf == 'start1':
            body = 'Suceeded'
            return {
                "isBase64Encoded": False,
                'statusCode': 200,
                'body': json.dumps(body),
                'headers': {
                'Content-Type': 'application/json'
                    }   
                }
    else:
            body = 'Failed'
            return {
                "isBase64Encoded": False,
                'statusCode': 400,
                'body': json.dumps(body),
                'headers': {
                'Content-Type': 'application/json'
                    }   
             }

If I send a POST with this body is successful:

{
  "WF": "start1",
  "Context": "OK"
}

Returns:

Test Event Name
test

Response
{
  "isBase64Encoded": false,
  "statusCode": 200,
  "body": "\"Suceeded\"",
  "headers": {
    "Content-Type": "application/json"
  }
}

Function Logs
START RequestId: 91663a9d-d8b6-4aad-b8b6-eb2f15ff0354 Version: $LATEST
END RequestId: 91663a9d-d8b6-4aad-b8b6-eb2f15ff0354
REPORT RequestId: 91663a9d-d8b6-4aad-b8b6-eb2f15ff0354  Duration: 1.74 ms   Billed Duration: 2 ms   Memory Size: 128 MB Max Memory Used: 66 MB  Init Duration: 300.73 ms

Request ID
91663a9d-d8b6-4aad-b8b6-eb2f15ff0354

But if I run a test in the API Gateway service in AWS I get:

Fri Jun 04 13:45:22 UTC 2021 : Sending request to https://XXXXXXX
Fri Jun 04 13:45:22 UTC 2021 : Received response. Status: 200, Integration latency: 15 ms
Fri Jun 04 13:45:22 UTC 2021 : Endpoint response headers: {Date=Fri, 04 Jun 2021 13:45:22 GMT, Content-Type=application/json, Content-Length=159, Connection=keep-alive, x-amzn-RequestId=d479fdac-6737-42fa-96a3-9e991056b48d, X-Amz-Function-Error=Unhandled, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-60ba2e72-ff62f051febbd21d6cc9d114;sampled=0}
Fri Jun 04 13:45:22 UTC 2021 : Endpoint response body before transformations: {"errorMessage": "'WF'", "errorType": "KeyError", "stackTrace": ["  File \"/var/task/lambda_function.py\", line 7, in lambda_handler\n    wf = event['WF']\n"]}
Fri Jun 04 13:45:22 UTC 2021 : Lambda execution failed with status 200 due to customer function error: 'WF'. Lambda request id: d479fdac-6737-42fa-96a3-9e991056b48d
Fri Jun 04 13:45:22 UTC 2021 : Method completed with status: 502

Why line 7 is wrong? Is there another way to parse the body of the POST?

Xbert
  • 91
  • 1
  • 1
  • 5
  • Is it possible your test from API Gateway is not populating the `event` object? That would cause your code to try and read the `WF` attribute of a null object. – Tom Nijs Jun 04 '21 at 15:42
  • Yes! @Tom Nijs, the API was in proxy mode! So it wasn't passing correctly the body of the POST. – Xbert Jun 05 '21 at 11:15
  • I'll post that as an answer for future visitors. – Tom Nijs Jun 05 '21 at 11:23

3 Answers3

3

The solution was found by disabling Lambda Proxy Integration in the POST - Integration Request.

Xbert
  • 91
  • 1
  • 1
  • 5
1

It looks like your API Gateway invocation is not populating the event parameter of your Lambda function. When you create a test event your lambda function's page, you get to create your own test event. API Gateway will not use this test event, it will instead send an empty event (probably null).

I.e.


import json
import boto3
from datetime import datetime


def lambda_handler(event,context): # Event is null here
    wf = event['WF'] # Attempting to read the attribute 'WF' of null results in an error

Tom Nijs
  • 3,835
  • 3
  • 22
  • 40
0

In my instance, I had received this error when invoking my Lambda function via API gateway, it was also completely fine when invoking directly the lambda function expecting a simple JSON body.

my expected payload: {"Email": "foo3@foo.com"}

It turns out Lambda Proxy Integration in the POST in the configuration API gateway changes the payload structure before passing into Lambda, and I was getting the error missing Key attribute Email. I noticed that line Endpoint request body after transformations: in cloudwatch. I learned that it was changing the payload.

How I debug this:

mel3kings
  • 8,857
  • 3
  • 60
  • 68