Rate limiting in AWS Lambdas

0

Say I have few lambdas connected with DynamoDb, and the number of invocations needs to be limited to a hard limit if 500 requests say, after this the lambda shouldn't get invoked and cost us billing, how can this be done, I tried out API gateway quota in this we need to have the limit on each of the API and api keys.

Need some other way to implement this. Thanks

2 Answers
1

I'm not sure on your use-case, so I'm not sure on which solution to recommend. Knowing the reason you'd wanna limit to 500 would help.

You can use a Cloudwatch alarm that alarms at 500 invocations, have the alarm trigger a Lambda function and that function calls UpdateEventSourceMapping and disables the connection between the stream and Lambda. This would prevent the Lambda from being invoked again until you enable it.

profile pictureAWS
EXPERT
answered 9 months ago
  • What if the lambda calling source is AWS API gateway and this gets called by the consumer, can we stop the lambda invocations in this case? Or if we can store the limit of allowed request in ddb access it before each of the call in lambdas and if the limit has got exhausted, we remove the connection between AWS API Gateway and lambdas

    Is this possible?

    Also in the case of API gateway talking with lambda and the command is given to make the reserved concurrency to 0, when does the concurrency becomes 0, will it becomes after all the requests from API gateway are served or how does it works?

0

I hope you'd have already gone through this blog post, which talks about rate limiting in server less applications.

Also, you may consider some fan out mechanism using SQS or eventbridge in your process flow. It'd depend on some other factors such as whether you want your application to be synchronous or asynchronous.

Refer this AWS Documentation Transfer Data Records (Lambda, DynamoDB, Amazon SQS), that would provide some additional context and how to achieve the use case.

Hope you find this information helpful.

Abhishek

profile pictureAWS
EXPERT
answered 9 months ago
  • But this solution will not work, can we stop the lambda from being invoked after a certain limit gets reached?

  • I can give you couple of ideas but none of them comes out of the box, you'll have to put a logic:

    Option1: You can get aws/lambda -> Invocations for given duration through get-metric-data and as soon as that limits gets hit, no processing would happen however lambda would be invoked. This is something you would do within lambda function.

    Option-2: Before this lambda function execution, another lambda function monitors that lambda function invocation and if let's say 50 invocations have already happened in last 30 minutes, then this lambda function would put a switch tag on that lambda function such as "Run":"Off" and your process flow would check this tag value first before invoking lambda function, if invocation count is less than 50 in last 30 minutes, toggle this switch back to on and invoke that function, otherwise don't invoke it.

    You can leverage Cloudwatch alarm feature as well to achieve this use case.

    I'm not sure if either of these options would be feasible for you based on complexity it'd add. I'd also encourage you to explore AWS State Machine(step functions).

    Hope this helps.

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