If you have an API in AWS API Gateway integrated with Lambda as proxy, you may get the following error when you execute an API resource’s method:

Response body

{
  "message": "Internal server error"
}

And in the logs, you will find this error:

Execution failed due to configuration error: Malformed Lambda proxy response

Reason

The reason for the error is that API gateway is not getting a response in the format that it expects.

Solution

Return the following response from your Lambda function(in Python):

import json

def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "Successful response",
        }),
        "headers":{ 'Access-Control-Allow-Origin' : '*' }
    }

The main thing to note here is that the API Gateway expects a status code, a response body and headers in the response from the Lambda function. Now deploy these new changes to API Gateway & Lambda and test again. You will get a successful response.

In the above example, json is just a library that will convert your response body to json. If you are using a different programming language, you can use a suitable json library that you can find.

Now your API Gateway API which is serving as a Lambda proxy, will start returning success responses.