While executing the AWS lambda through the API gateway proxy URL, you may encounter an issue saying Malformed Lambda proxy response – Method completed with status: 502 here I am going to address the resolution for this.

Malformed Lambda proxy response:

When you see the Malformed Lambda proxy response, it means the response from the Lambda function doesn’t match the format of API Gateway is expecting.

issue:

Terminal
Wed Aug 04 16:57:22 UTC 2021 : Execution failed due to configuration error: Malformed Lambda proxy response
Wed Aug 04 16:57:22 UTC 2021 : Method completed with status: 502

Resolution:

Especially with API gateway proxy integrations, the API gateway requires a specific output format to be returned from the backend lambda function. Primarily the response should be in JSON format with the following fields init.

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

Example:

return {
        "statusCode": 200,
        "body": json.dumps({"statusCode": 200,"data": data}),
        "isBase64Encoded": False
    }

Done!

References:

Happy Learning: