How to make lambda return 429 on invocation limit

0

I have implemented a lambda function that synchronously handles HTTP requests through AWS loadbalancer. Since lambdas have invocation limits the lambda might be overloaded/throttled when a lot of requests (>1000) are received at the same time.

Since these requests fail because of throttling, I assumed that the loadbalancer-lambda integration would return a 429 code, allowing the client to retry the request at a better time. But this is not the case, whenever the lambda fails because of heavy load the return code is a simple 500 and not a 429. Making the client either retry at a too fast rate or simply believing the service to be broken.

This makes it very difficult to guarantee a stable service, how can I make sure that the lambda returns the more correct 429 code whenever it is throttled?

Thank you!

Philip
asked 11 days ago103 views
2 Answers
0

Hi

Unfortunately, you can't directly force a Lambda function to return a 429 error upon throttling. Here's why, and potential solutions: Lambda Throttling Behavior: When a Lambda function is throttled, it usually triggers an internal error within the Lambda service, often resulting in a 5xx type error response. Lambda itself is not designed to emit a 429.

Alternative solutions/Workaround:

profile picture
GK
answered 11 days ago
  • This isn't about API Gateway, this is about ALB -> Lambda.

  • @Max I just corrrected the answer thanks for the info

0

Hi,

Is there an important reason why you use ALB instead of standard pattern API GTW + Lambda ?

If the case of this standard pattern with GTW, you will get a 429 on throttling. See https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-request-throttling.html for example

Best,

Didier

profile pictureAWS
EXPERT
answered 11 days ago
  • Hi,

    The loadbalancer existed before the lambda and handles requests in a certain path. I want the lambda to handle a subpath of the same path the loadbalancer handles, so to avoid having the loadbalancer handling the request that I want to go to the lambda, I put the lambda as an action for a rule in the loadbalancer.

    Best regards, Philip

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions