A developer is migrating a backend AWS Lambda function from a Lambda custom (non-proxy) integration to a Lambda proxy integration on an Amazon API Gateway REST API. Under the custom integration, the Lambda function received a pre-mapped JSON payload containing query parameters and headers, and it returned a simple JSON object:
`{ "status": "success", "data": { "userId": 101 } }`
After configuring the API Gateway to use Lambda Proxy Integration, clients receive a 502 Bad Gateway error on all API requests. Additionally, the Lambda function execution logs show errors indicating that the incoming event format is unexpected.
Which of the following modifications must the developer make to resolve these errors?
- Modify the Lambda function to parse the incoming request body from the event.body property, and update the function's return statement to return a JSON object with statusCode and a stringified JSON body.Answer
- BDefine an Integration Request mapping template in API Gateway to map the client request to the Lambda input event, and define an Integration Response to map the Lambda return payload to a Method Response.
- CConfigure a Lambda authorizer for the API Gateway method to authenticate the client request and inject the required request context before invoking the backend Lambda function.
- DAdd Access-Control-Allow-Origin headers to the API Gateway Method Response configuration and configure API Gateway to automatically convert the Lambda output string into a 502 status code.
Answer
Modify the Lambda function to parse the incoming request body from the event.body property, and update the function's return statement to return a JSON object with statusCode and a stringified JSON body.
The correct option is to modify the Lambda function to parse the incoming request body from the event.body property and return a JSON object with statusCode and a stringified JSON body. This is because Lambda proxy integration passes the entire HTTP request wrapper where the request body is stringified, and requires the response to conform strictly to a response format containing statusCode and body fields.
Step-by-Step Solution
Key Concept
The primary difference in payload structure and response contract between API Gateway Lambda Proxy and Lambda Custom (non-proxy) integrations.
Estimated Time:2m 0s