Search code examples
amazon-web-servicesamazon-eksamazon-ecraws-application-load-balancer

Limit number of users accessing the URL


Is there any way to limit the number of users accessing the site url using AWS. WAF checks for IP address and blocks it if it exceeds rate limit.

I need it to be from any IP and if altogether crossing the rate limit, it should redirect to custom page saying the limit has been exceeded.

Rate throttling from api gateway seems to be better way, however, the home page doesn't call any api. So, it has to be managed with the url only.


Solution

  • Natively not as far as I know. My understanding is that you want to have a global limit on how often a specific URL can be accessed.

    You could use a Lambda@Edge function in CloudFront with a DynamoDB table in the backend.

    The Lambda function gets invoked on every request.

    If it's the URL that should be limited, it makes a conditional UpdateItem call to DynamoDB and tries to increase the counter by one under the condition, that the counter is not already greater than X, where X is the maximum limit for your URL.

    Should that UpdateItem succeed, you can pass on the request to the origin. If the call fails, you know the limit has been exceeded and you can make a redirect.

    This way you use DynamoDB as the central counter, it should be fast enough and scale well enough. Once the limit is exceeded, you can cache that in the Lambda@Edge function to avoid making too many requests.