Question

Difficulty: HardAPI Development and Integration with Amazon API Gateway

A developer is migrating a REST API to Amazon API Gateway with an AWS Lambda backend. The developer configures a new resource with a GET method using Lambda Proxy Integration. The Lambda function executes successfully and returns the following JSON response:

{
"status": "success",
"data": {
"items": ["item1", "item2"]
}
}

However, when testing the API endpoint via a client, the client receives an HTTP 502502 Bad Gateway status code. The CloudWatch execution logs for API Gateway show: "Malformed Lambda proxy response" and "Execution failed due to configuration error". What is the root cause of this error, and how should the developer resolve it?

  1. The Lambda function must return a JSON response with specific keys, including `statusCode` and a stringified `body`. The developer should modify the Lambda function to return a structured payload containing these fields.Answer
  2. B
    The developer must configure an Integration Response mapping template in API Gateway to map the custom JSON properties returned by the Lambda function to the HTTP Method Response.
  3. C
    The developer must configure a Lambda Authorizer to validate the format of the payload returned by the Lambda function before it is passed to the Method Response.
  4. D
    The Lambda function returned a JSON object, but Lambda Proxy Integration requires the response body to be base64-encoded. The developer should modify the function to set `isBase64Encoded` to `true` and base64-encode the JSON payload.

Answer

The Lambda function must return a JSON response with specific keys, including `statusCode` and a stringified `body`. The developer should modify the Lambda function to return a structured payload containing these fields.
In Lambda Proxy Integration, API Gateway does not map or modify the response from the backend. The backend Lambda function is directly responsible for generating the complete HTTP response. It must return a JSON object with a specific structure: an integer `statusCode`, and a stringified JSON `body`. The custom JSON object returned by the function does not match this structure, which triggers a 502502 Bad Gateway error in API Gateway.

Step-by-Step Solution

1
Analyze the integration type configured in API Gateway.
The method is configured with Lambda Proxy Integration.
This determines whether API Gateway expects the backend to return an HTTP-compatible response structure directly or if it will use mapping templates to build the response.
2
Inspect the response returned by the Lambda function.
The Lambda function returns a custom JSON object `{"status": "success", "data": ...}`.
To verify if the response conforms to the contract required by Lambda Proxy Integration.
3
Identify the mismatch between the returned response and the proxy contract.
The returned response is missing mandatory fields such as `statusCode` and a stringified `body`.
Under Lambda Proxy Integration, API Gateway requires a specific JSON response format containing `statusCode` (an integer) and `body` (a string). Since the returned object is arbitrary, API Gateway cannot parse it and fails with a 502502 Bad Gateway error.
4
Formulate the resolution strategy.
Modify the Lambda function's code to return the required JSON structure.
Since mapping templates (Integration Responses) are bypassed in proxy integration, the change must be made directly within the backend Lambda function.

Key Concept

Lambda Proxy Integration Response Format
Estimated Time:2m 0s
Rate this question