Resolve “malformed Lamda proxy response” or 502 errors …

Note: If your function is written in another programming language, refer to that language’s programming reference. Confirm that the function returns a response in the correct format. In this response, there are four fields: statusCode, headers, body, and isBase64Encoded. The statusCode is an integer interpreted by API Gateway that’s returned to the caller of the API method.

https://aws.amazon.com/premiumsupport/knowledge-center/malformed-502-api-gateway/