AWS Lambda error: HTTP 502 ‘Execution failed due to configuration error: Malformed Lambda proxy response’

Calling an AWS Lambda via API Gateway with the Lambda Proxy Integration option, you might see an HTTP 502 response and this message:

Execution failed due to configuration error: Malformed Lambda proxy response"

Wed May 30 05:00:41 UTC 2018 : Execution failed due to configuration error: Malformed Lambda proxy response
Wed May 30 05:00:41 UTC 2018 : Method completed with status: 502

This is a rather cryptic message, but  it’s saying is the response is not in the expected format.

Per this doc, the expected response should be in this format:

{
    "isBase64Encoded": true|false,
    "statusCode": httpStatusCode,
    "headers": { "headerName": "headerValue", ... },
    "body": "..."
}

AWS Lambda node.js template

The node.js based implementation of AWS Lambdas has the following structure:

Using ES6 fat arrow syntax:

exports.handlerName = (event, context, callback) => {
  callback(error, result);
}

where error is either null for a successful execution, or an error, and result is a response to return to your caller.

Using ES5 syntax:

exports.handlerName = function(event, context, callback) {
  callback(error, result);
}

For more info, see here.