A developer has configured a Lambda proxy integration in Amazon API Gateway. When testing the API, they receive a 502 (Bad Gateway) error. The Lambda function execution logs in Amazon CloudWatch show that the function ran successfully and returned the intended result. Which of the following is the most likely cause of this error?
- The Lambda function is returning a raw string or a custom JSON object instead of a JSON response containing the required "statusCode" and "body" keys.Answer
- BThe integration request requires mapping templates in API Gateway to transform the incoming request payload before invoking the Lambda function.
- CThe developer configured a custom Lambda authorizer instead of a built-in Amazon Cognito User Pool authorizer, causing the execution to abort prior to integration.
- DThe API Gateway integration must be changed to a Lambda custom integration because Lambda proxy integrations do not support dynamic routing or HTTP method execution.
Answer
The Lambda function is returning a raw string or a custom JSON object instead of a JSON response containing the required "statusCode" and "body" keys.
In a Lambda proxy integration, API Gateway expects the backend Lambda function to return a response structured as a specific JSON object. This object must contain the 'statusCode' (as an integer or string) and the 'body' (as a stringified JSON payload). Since the Lambda function executed successfully but API Gateway returned a 502 Bad Gateway error, the most likely cause is that the function did not format its response payload to include these required keys, causing API Gateway to fail parsing the response.
Step-by-Step Solution
Key Concept
Understanding the response format requirements for API Gateway Lambda Proxy integrations.
Estimated Time:1m 0s