Question

Difficulty: MediumServerless Development with AWS Lambda

A company is refactoring its web API backend. A developer configures an Amazon API Gateway REST API to use a Lambda proxy integration with an existing AWS Lambda function. During testing, clients receive 502 Bad Gateway errors. The CloudWatch logs show that the Lambda function completed its execution successfully without errors and returned the computed payload. Which action should the developer take to resolve the integration error?

  1. A
    Configure an integration response mapping template in API Gateway to map the Lambda function's raw output to the client's expected JSON format.
  2. B
    Update the Lambda function's resource-based policy to grant the API Gateway execution role permission to assume the Lambda function's IAM execution role.
  3. Change the Lambda handler to return an object containing `statusCode` as an integer, `headers` as a map, and the serialized payload as a string under the `body` key.Answer
  4. D
    Configure a CORS policy in API Gateway to automatically inject the required response headers and strip the raw response wrapper.

Answer

The correct action is to change the Lambda handler to return an object containing `statusCode` as an integer, `headers` as a map, and the serialized payload as a string under the `body` key.
The correct action is to change the Lambda handler to return an object containing `statusCode` as an integer, `headers` as a map, and the serialized payload as a string under the `body` key. In an API Gateway Lambda proxy integration, the backend Lambda function is entirely responsible for defining the HTTP response structure, which requires these specific fields.

Step-by-Step Solution

1
Analyze the error context: a successful Lambda execution with a successful return payload, but a 502 Bad Gateway error at the API Gateway level.
The 502 Bad Gateway error indicates that API Gateway was unable to process the response from the backend Lambda function.
When using Lambda proxy integration, API Gateway enforces a strict response format contract on the backend integration.
2
Identify the response format requirements for Lambda proxy integration.
The Lambda function must return a JSON object with specific keys: `statusCode` (number), `headers` (object), and `body` (string).
Without these keys, API Gateway cannot map the backend response to a proper HTTP response for the client.
3
Determine the required changes to the Lambda function handler code.
The developer must wrap the raw payload by converting it to a string using JSON serialization and assigning it to the `body` key of the returned object.
This matches the expected proxy integration response schema, allowing API Gateway to parse the payload and return it to the client with the designated status code.

Key Concept

API Gateway Lambda Proxy Integration Response Format
Rate this question