Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to throttle AWS Lambda or API Gateway by IP?

I'm currently trying to design a Serverless Application using S3 static website, API Gateway and AWS Lambda. I want to make this website public, so there is no API key per user.

I want to prevent abuse of these endpoints to something really low, like 1 request every 5 seconds for a given public IP.

Is it possible with AWS Shield or inside any of these services?

like image 657
Albondi Avatar asked Jun 04 '19 21:06

Albondi


People also ask

How do I add throttling to API gateway?

You can set additional throttling targets at the method level in Usage Plans as shown in Create a usage plan. In the API Gateway console, these are set by specifying Resource= <resource> , Method= <method> in the Configure Method Throttling setting.

How do I throttle API requests?

One way to implement API throttling in distributed systems is to use sticky sessions. In this method, all requests from a user are always serviced by a particular server. However, this solution is not well-balanced or fault tolerant. The second solution to API throttling in distributed systems are locks.

Does AWS API gateway have IP address?

Your API gateway is now accessible via static IP addresses provided by AWS Global Accelerator.


2 Answers

Update March 23 2021:

You can now throttle as low as 100 requests per 5 minutes!

enter image description here


Old answer:

Unfortunately if you have a requirement of I want to prevent abuse of these endpoints to something really low, like 1 request every 5 seconds for a given public IP. then AWS WAF will not be suitable.

The minimum threshold you can set for a WAF rate based rule is 2000 requests in a 5 minute period.

enter image description here

If you want to implement aggressive rate based rules based on IP, you will need to write your own solution that either:

  • Inspects the CloudFront access logs and retroactively bans bad IPs
  • Use Lambda@Edge to evaluate requests in real time + ban accordingly

What might be more appropriate for your use case is using throttling on an API stage, but not based on IP. What you can do is set a maximum number of requests per second on average, and have the API return a 429, too many requests, when that number is exceeded. You can be really aggressive with this, or more relaxed by using a decimal value:

enter image description here

like image 176
Chris McKinnel Avatar answered Sep 21 '22 04:09

Chris McKinnel


From AWS Announces Rate-Based Rules for AWS WAF:

AWS today announced Rate-based Rules for AWS WAF. This new rule type protects customer websites and APIs from threats such as web-layer DDoS attacks, brute force login attempts and bad bots. Rate Based Rules are automatically triggered when web requests from a client exceed a certain configurable threshold.

With Rated-based Rules customers can also block future requests from a client trying to send large volume of requests to certain parts of their website like the login page. Customer can also integrate this new rule with CloudWatch Alarms and AWS Lambda to take custom action on clients making unusually high calls against their API endpoints. Customers can also use Rate-Based Rules to mitigate unwanted bots by combining the Rate-based rule with a condition to identify specific malicious user agents’ associated with bad bots.

like image 45
John Rotenstein Avatar answered Sep 20 '22 04:09

John Rotenstein