Search code examples
aws-lambdaapi-gateway

AWS API Gateway - Lambda Proxy (Integration Request) - Internal Server Error


Have a simple Lambda POST integration with DynamoDB. Inserts one record into Dynamo upon execution. Works well when testing in AWS Lambda.

Response output is:

{
  "isBase64Encoded": false,
  "statusCode": 204,
  "headers": {
    "Content-Type": "application/json",
    "Access-Control-Allow-Origin": "*",
    "Access-Control-Allow-Credentials": true
  },
  "body": "{}"
}

This response is programmatically defined as part of the Lambda response handling in accordance with the spec (afaict).

However, when run via a test in API Gateway, I receive a 502 Internal server error. Am using Lambda-Proxy integration as below:

API Gateway Lamba Proxy Integration

Cloudwatch logs indicate:

{ ValidationException: Supplied AttributeValue is empty, must contain exactly one of the supported datatypes
at Request.extractError 

with a limited use stacktrace (webpack has hashed the code pretty well). The above error would suggest DynamoDB is not receiving the payload correctly, or in a format it wants that honours required attributes. However, I have taken the same JSON used for the (successful) tests (tweaking the IDs to be unique between runs) from lambda, and believe my request header (Content-Type: application/json) is sensible.

Any thoughts / help on narrowing down the issue? I can post more info as requested if it helps.


Solution

  • Ok, solved this by logging all output (and inspecting in CloudWatch), in particular the event object. When running in a Lambda Test mode, the ID and other POST attributes were passed in the root of the event object. However, when using lambda-proxy mode, the integration remaps the event object hierarchy, and POST attributes are JSON stringified into the body attribute.

    Just a quirk that makes sense once you understand what it's doing. That said, it's odd that the same payload fails when testing Lambda & API Gateway in turn.