0

Im using a AWS Lambda function receving many unexpected signals from several channels and sending request to another API.

the problem is the another API is limiting the number of requests per minute.(if the numer exceed the limit, IP will be banned)

So I want to limit the number of requests per minute sent by the lambda function.

[What I have tried] :

  1. At first, I tried to use 'Concurrency limit', which turns out that it cannot limit the number of call 'per minute'.

  2. Secondly, I tried to get the current number of requests per minute from the another API before actual requests and if the number of requests exceed the limit, then sleep 1minute and send actual requests.

However, getting the current number of requests per minute requires 1 request quarter also. So, if too many signals call the lambda function in one minute, it can cause IP ban as well.

Please help me to solve this problem, I will really appreciate your help. Thanks!

1 Answer 1

3

Many people ask this question, but there is no simple answer.

It requires rethinking your architecture to determine how to receive, queue and make API calls. For example, if the limit has been exceeded, what should happen to new requests coming in -- should they be rejected, should they simply wait (not a good idea with AWS Lambda functions since they accumulate cost with no benefit), or should the messages go into a queue for later handling?

Can the processes calling your function gracefully handle a "Too many requests" error? Can they asynchronously make a request and later poll for a response? Figuring out this process is actually harder and more important that implementing the 'per minute' limit.

Reducing the concurrency limit is a good start. If possible, reduce it to 1 concurrent Lambda function so that you don't need to deal with parallel functions. Then, the function could simply maintain a global variable containing the times of the previous calls. When invoked, the function can check the list of times to count how many were made in the last period. If there are too many, then either wait or reject the request. This is made much more complex if the concurrency is higher than 1 since this information will need to be shared.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks!!! you are a genius! I have never thought of the last phrase you wrote. additionally, I searched the keywords you used here in StackOverflow and found additional good solutions such as dynamoDB or parameter store! So I wanted to express thanks and found that the writer is also you haha! you are really a good profession in this industry :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.