Troubleshooting API Gateway 502 Errors Using CloudWatch Logs
What a 502 Means
API Gateway returns 502 Bad Gateway when the integration (usually Lambda) fails or returns a malformed response. CloudWatch captures the underlying cause.
Investigation Steps
- Open CloudWatch Logs for the integration's Lambda function.
- Look for the request ID matching the 502.
- Check for
Task timed out, malformed response shape, or an unhandled exception. - Confirm the Lambda response includes
statusCode,headers, and a stringbody.
Malformed Response Example
{ "statusCode": 200, "body": { "ok": true } }
body must be a string, not an object. Fix:
{ "statusCode": 200, "body": "{\"ok\":true}" }
Use the FormatHub AWS CloudWatch Log Formatter to expand and compare these responses quickly.