Skip to content

fix(lambda): Set proper handler return value for lambda as ALB target#1249

Open
ethann wants to merge 1 commit intoAdyen:masterfrom
ethann:fix/lambda-alb-target-group-502-error
Open

fix(lambda): Set proper handler return value for lambda as ALB target#1249
ethann wants to merge 1 commit intoAdyen:masterfrom
ethann:fix/lambda-alb-target-group-502-error

Conversation

@ethann
Copy link
Copy Markdown
Contributor

@ethann ethann commented Apr 11, 2025

Lambda in AWS can be configured as Target Group for ALB. API Gateway, Lambda Function URL and Application Load Balancer expect lambda to return an object with specific fields, e.g. statusCode which have a special behavior (response object). Not specifying statusCode field in lambda and triggering it by API Gateway or Function URL converts return object into a string and returns it with status code 200. ALB throws 502 Bad Gateway error instead, expecting lambda to return a proper response object structure with at least statusCode field (the rest including body seems to be optional).

To keep it compatible with all three solutions and avoid potential conflict between returned body and response object structure, object returned by the lambda should have a proper structure. To keep it compatible with current behavior, status is set to 200 and the body is stringified and returned as the lambda response object body field.

API Gateway response body:
https://docs.aws.amazon.com/lambda/latest/dg/services-apigateway.html#apigateway-types-transforms

Lambda Function url response body (follows API Gateway structure):
https://docs.aws.amazon.com/lambda/latest/dg/urls-invocation.html#urls-payloads

ALB Target group response body:
https://docs.aws.amazon.com/elasticloadbalancing/latest/application/lambda-functions.html#respond-to-load-balancer

Lambda in AWS can be configured as Target Group for ALB. API Gateway,
Lambda Function URL and Application Load Balancer expect lambda to return
an object with specific fields, e.g. 'statusCode' which have a special behavior
(response object). Not specifying 'statusCode' field in lambda and triggering
it by API Gateway or Function URL converts return object into a string and
returns it with status code 200. ALB throws 502 Bad Gateway error instead,
expecting lambda to return a proper response object structure with at least
'statusCode' field (the rest including 'body' seems to be optional).

To keep it compatible with all three solutions and avoid potential conflict
between returned body and response object structure, object returned by
the lambda should have a proper structure. To keep it compatible with current
behavior, status is set to 200 and the body is stringified and returned
as the lambda response object 'body' field.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant