I configured Amazon API Gateway proxy integration to work with an AWS Lambda function, but when I call my API I receive a configuration error or a 502 status code. How can I resolve this?

The source of these errors is often the format of the Lambda function response. To check the format of your response, review the logging output from the Amazon API Gateway console, or review your API Gateway logs in Amazon CloudWatch. For more information, see How do I enable Amazon CloudWatch Logs for APIs I created in the Amazon API Gateway?

If the format of the proxy response is the source of the issue, you will see this message in the API Gateway logs:

Thu Dec 08 01:13:00 UTC 2016 : Execution failed due to configuration error: Malformed Lambda proxy response
Thu Dec 08 01:13:00 UTC 2016 : Method completed with status: 502

To resolve this problem, change the way your Lambda function responds to your API. 

For a Lambda function's response to be handled the by API Gateway, it must return a response in this format:

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

The following is an example function in Node.js:

exports.handler = (event, context, callback) => {

    var responseBody = {
        "key3": "value3",
        "key2": "value2",
        "key1": "value1"
    };

    var response = {
        "statusCode": 200,
        "headers": {
            "my_header": "my_value"
        },
        "body": JSON.stringify(responseBody),
        "isBase64Encoded": false
    };
    callback(null, response);
};

In this example, the function's response is in the format that the API Gateway expects. For additional information, see Output Format of a Lambda Function for Proxy Integration.

Note: In this response, there are four fields: statusCode, headers, body, and isBase64Encoded.

  • The statusCode is an integer interpreted by the API Gateway that is returned to the caller of the API method.
  • The headers are collected and sent back with the API Gateway response.
  • The body field, if you are returning JSON, must be converted to a string or it will cause further problems with the response. You can use JSON.stringify to handle this in Node.js functions; other runtimes will require different solutions, but the concept is the same.
  • The isBase64Encoded field is important if you're working with binary data; if you don't use this field, it should be set to a Boolean value of false.

When writing a Lambda function that works with an API Gateway simple proxy, the response must be JSON in the prescribed format, and contents of the body field must be properly escaped.


Did this page help you? Yes | No

Back to the AWS Support Knowledge Center

Need help? Visit the AWS Support Center

Published: 2017-10-16

Updated: 2018-04-02