diff --git a/README.md b/README.md index f156e8a..638d0b0 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ ### About the Project > The problem statement focuses on designing and implementing an effective rate limiting mechanism for API management to ensure fair and efficient usage of resources. The objective is to develop a system that prevents API abuse, optimizes resource allocation, and maintains high performance by applying various rate limiting strategies—Fixed Window, Sliding Window, Token Bucket, and Leaky Bucket. These strategies need to be adaptable based on parameters like request frequency, client identities, and available capacity. -#redis-cli -h rate-limiter-cache-0001-001.easvay.0001.use1.cache.amazonaws.com -p 6379 ### Tech Stack diff --git a/server/event/ControllerLambda.json b/server/event/ControllerLambda.json new file mode 100644 index 0000000..77a6611 --- /dev/null +++ b/server/event/ControllerLambda.json @@ -0,0 +1,10 @@ +{ + "function": "Lambda1-RL", + "payload": { + "resource": "MKResource", + "url": "http://example.com", + "bucketsize": 5, + "refillrate": 1, + "ttl": 10 + } +} diff --git a/server/event/Lambda1.json b/server/event/Lambda1.json index 127a6b7..a190bf7 100644 --- a/server/event/Lambda1.json +++ b/server/event/Lambda1.json @@ -1,4 +1,7 @@ { "resource": "example_resource", - "time_window": 60 -} + "url": "http://example.com", + "bucketsize": 5, + "refillrate": 1, + "ttl": 3600 + } \ No newline at end of file diff --git a/server/event/Lambda2.json b/server/event/Lambda2.json new file mode 100644 index 0000000..aafb99d --- /dev/null +++ b/server/event/Lambda2.json @@ -0,0 +1,6 @@ +{ + "resource": "example_resource", + "url": "http://example.com", + "limit": 10, + "window_size_seconds": 60 +} \ No newline at end of file diff --git a/server/event/Lambda3.json b/server/event/Lambda3.json new file mode 100644 index 0000000..94470a4 --- /dev/null +++ b/server/event/Lambda3.json @@ -0,0 +1,6 @@ +{ + "resource": "example_resource", + "url": "http://example.com", + "limit": 10, + "window_size_seconds": 60 + } \ No newline at end of file diff --git a/server/scripts/ControllerLambda.py b/server/scripts/ControllerLambda.py index f89deba..dc9d573 100644 --- a/server/scripts/ControllerLambda.py +++ b/server/scripts/ControllerLambda.py @@ -2,10 +2,29 @@ import json lambda_client = boto3.client('lambda') + +def validate_payload(function_name, payload): + expected_parameters = { + 'Lambda1-RL': ['resource', 'url', 'bucketsize', 'refillrate', 'ttl'], + 'Lambda2-RL': ['resource', 'url', 'limit', 'window_size_seconds'], + 'Lambda3-RL': ['resource', 'url', 'limit', 'window_size_seconds'], + 'Test_Fun': ['test_param1', 'test_param2'] + } + + if function_name in expected_parameters: + return all(param in payload for param in expected_parameters[function_name]) + else: + return True + def lambda_handler(event, context): function_name = event.get('function') payload = event.get('payload', {}) + if not validate_payload(function_name, payload): + return { + 'statusCode': 400, + 'body': 'Invalid payload format for function: ' + function_name + } function_arns = { 'Lambda1-RL': 'arn:aws:lambda:us-east-1:247477386084:function:Lambda1-RL', diff --git a/server/scripts/Lambda1.py b/server/scripts/Lambda1.py index 95cd49a..08a4a99 100644 --- a/server/scripts/Lambda1.py +++ b/server/scripts/Lambda1.py @@ -3,7 +3,8 @@ import os URL = os.environ.get('URL') -redis_client = redis.Redis(host=URL, port=6379, db=0) +# redis_client = redis.Redis(host=URL, port=6379, db=0) +redis_client = redis.Redis.from_url(URL) def is_allowed(resource, bucket_size, refill_rate, ttl_seconds): key = f"token_bucket:{resource}" diff --git a/server/scripts/Lambda2.py b/server/scripts/Lambda2.py index 258ac40..b7f6af5 100644 --- a/server/scripts/Lambda2.py +++ b/server/scripts/Lambda2.py @@ -3,8 +3,8 @@ import os URL = os.environ.get('URL') -redis_client = redis.Redis(host=URL, port=6379, db=0) - +# redis_client = redis.Redis(host=URL, port=6379, db=0) +redis_client = redis.Redis.from_url(URL) def is_allowed(resource, limit, window_size_seconds): key = f"fixed_window:{resource}" current_count = redis_client.get(key) diff --git a/server/scripts/Lambda3.py b/server/scripts/Lambda3.py index 0447e5e..2b2e801 100644 --- a/server/scripts/Lambda3.py +++ b/server/scripts/Lambda3.py @@ -4,7 +4,8 @@ URL = os.environ.get('URL') # Connect to Redis -redis_client = redis.Redis(host=URL, port=6379, db=0) +# redis_client = redis.Redis(host=URL, port=6379, db=0) +redis_client = redis.Redis.from_url(URL) def is_allowed(resource, limit, window_size_seconds): key = f"sliding_window_log:{resource}" diff --git a/server/test/Rate-Limiter-AWS.postman_collection.json b/server/test/Rate-Limiter-AWS.postman_collection.json new file mode 100644 index 0000000..d91576b --- /dev/null +++ b/server/test/Rate-Limiter-AWS.postman_collection.json @@ -0,0 +1,53 @@ +{ + "info": { + "_postman_id": "97465e81-962e-45d7-a402-356fe36a6c78", + "name": "Rate-Limiter-AWS", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "26811368", + "_collection_link": "https://www.postman.com/orbital-module-engineer-11493836/workspace/postman-api-fundamentals-student-expert/collection/26811368-97465e81-962e-45d7-a402-356fe36a6c78?action=share&source=collection_link&creator=26811368" + }, + "item": [ + { + "name": "Test1Controller", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"function\": \"Lambda1-RL\",\r\n \"payload\": {\r\n \"resource\": \"MKResource\",\r\n \"url\": \"http://example.com\",\r\n \"bucketsize\": 5,\r\n \"refillrate\": 1,\r\n \"ttl\": 10\r\n }\r\n}\r\n", + "options": { + "raw": { + "language": "json" + } + } + } + }, + "response": [] + }, + { + "name": "Test2Controller", + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "raw", + "raw": "{\r\n \"function\": \"Lambda2-RL\",\r\n \"payload\": {\r\n \"resource\": \"MK2_resource\",\r\n \"url\": \"http://example.com\",\r\n \"limit\": 5,\r\n \"window_size_seconds\": 30\r\n }\r\n}\r\n", + "options": { + "raw": { + "language": "json" + } + } + } + }, + "response": [] + }, + { + "name": "Test3Controller", + "request": { + "method": "POST", + "header": [] + }, + "response": [] + } + ] +} \ No newline at end of file