A mobile application client receives a 502 Bad Gateway error when calling a REST API endpoint. The endpoint is configured with Amazon API Gateway using a Lambda Proxy integration. Upon reviewing the Amazon CloudWatch logs, the developer confirms that the backend Lambda function executed successfully and completed without timing out. Which of the following is the most likely cause of this error?
- AThe REST API resource does not have CORS enabled, which blocked API Gateway from processing the backend response.
- The Lambda function is returning a raw string response instead of a JSON object containing the required statusCode field.Answer
- CThe developer did not configure an integration response mapping template in API Gateway to convert the Lambda output.
- DThe Lambda function's IAM execution role lacks the permissions to invoke the API Gateway endpoint.
Answer
The Lambda function is returning a raw string response instead of a JSON object containing the required statusCode field.
The correct answer is correct because under a Lambda Proxy integration, API Gateway expects the backend Lambda function to return a JSON object containing specific keys, including 'statusCode' and 'body'. If the function returns a raw text string, API Gateway fails to parse the output and returns a 502 Bad Gateway error to the client.
Step-by-Step Solution
Key Concept
API Gateway Lambda Proxy Integration response format requirements