Question #981
A developer is integrating Amazon API Gateway with an AWS Lambda function. After deploying the API, the developer notices that all requests result in a 502 Bad Gateway error. The Lambda function logs indicate that the function executed successfully. What should the developer do to resolve the error?
Ensure the API Gateway method uses an IAM role with permissions to invoke the Lambda function.
Modify the API Gateway to use a Lambda alias instead of the function's ARN.
Update the Lambda function to return a response in the format expected by API Gateway.
Adjust the timeout settings of the API Gateway integration to match the Lambda function's duration.
Explanation
A 502 Bad Gateway error in API Gateway typically indicates an invalid response from the backend (Lambda). Even if the Lambda function executes successfully, it must return a response in the format API Gateway expects, which includes keys like 'statusCode', 'headers', and 'body'. If the response lacks these fields or is malformed, API Gateway cannot process it, resulting in a 502 error.
Why Other Options Are Incorrect:
- A: Incorrect because IAM permissions relate to invoking the Lambda function. Since the Lambda logs confirm execution, permissions are not the issue.
- B: Lambda aliases manage versions, but using an alias instead of the ARN does not resolve response format issues.
- D: Timeout mismatches cause 504 (Gateway Timeout), not 502. The Lambda logs confirm successful execution, so timeouts are irrelevant.
Key Takeaway: Always validate that Lambda responses adhere to API Gateway's required structure to avoid 502 errors.
Answer
The correct answer is: C