Introduction

AWS ALB Lambda 502 Bad Gateway usually means the Application Load Balancer reached the Lambda target but rejected the function result or could not complete the invocation successfully.

Symptoms

  • ALB returns 502 while the Lambda appears to run
  • Lambda logs show malformed response or timeout behavior
  • Only requests through the ALB fail while direct tests look healthy

Common Causes

  • Lambda response JSON does not match ALB expected format
  • Function timeout is shorter than real request handling
  • Target group or invocation permissions are incomplete

Step-by-Step Fix

  1. 1.Check the ALB target group and Lambda integration settings.
  2. 2.Validate the exact Lambda response body, statusCode, and headers.
  3. 3.Confirm Lambda permissions allow the ALB to invoke it.
  4. 4.Re-test through the ALB path, not just direct Lambda invocation.

Prevention

  • Keep ALB response contract tests in CI for Lambda handlers
  • Watch ALB 5xx and Lambda error logs together
  • Review timeout and header behavior after every handler change