Question

Difficulty: MediumAPI Development and Integration with Amazon API Gateway

An e-commerce application uses a REST API in Amazon API Gateway to retrieve product inventory. The developer decides to transition the integration type of the GET method from a Lambda custom integration to a Lambda proxy integration. Following this change, clients receive an HTTP 502 Bad Gateway error. The Amazon CloudWatch logs for the integrated AWS Lambda function confirm that the function executes successfully and returns the raw inventory data. Which modification should the developer make to resolve this error?

  1. A
    Configure a Velocity Template Language (VTL) mapping template in the API Gateway Integration Response to format the response payload.
  2. Format the Lambda function's output as a JSON object containing a `statusCode` integer, a `headers` object, and a stringified JSON `body`.Answer
  3. C
    Configure a custom Lambda authorizer in API Gateway to intercept the execution result and map it to the correct Method Response.
  4. D
    Define the required response structure in the Method Response settings of the API Gateway console to automatically parse the raw Lambda return value.

Answer

Format the Lambda function's output as a JSON object containing a `statusCode` integer, a `headers` object, and a stringified JSON `body`.
In a Lambda proxy integration, API Gateway does not map the response automatically or apply integration response templates. The Lambda function itself must return a JSON object with specific keys: `statusCode` (integer), `headers` (object), and `body` (string). If the Lambda function returns raw data or any other structure, API Gateway fails to parse it and returns a 502 Bad Gateway error to the client. Formatting the function's output with these keys resolves the error.

Step-by-Step Solution

1
Identify that the GET method uses a Lambda proxy integration.
Realize that under Lambda proxy integration, API Gateway passes the request and response through without integration mapping templates.
This determines that API Gateway expects the backend Lambda function to strictly conform to the required proxy response payload format.
2
Analyze the error (HTTP 502 Bad Gateway) and CloudWatch log success status.
Determine that the Lambda function itself runs fine but returns raw inventory data instead of the required envelope structure.
API Gateway requires the proxy return format (with keys like statusCode and body) to parse the response and map it to an HTTP client response.
3
Update the Lambda function's return statement to output the proper JSON response structure.
Modify the return value to be a JSON object with a statusCode integer (e.g., 200) and the inventory data as a stringified JSON body.
This matches the expected schema, allowing API Gateway to correctly parse the payload and return the inventory to the client.

Key Concept

Lambda Proxy Integration Response Format
Estimated Time:1m 30s
Rate this question