Throttling as you described as a form of protection is completely doable :)
and without the need to create a second dummy function as you described.
Lambdas come with a Reserved Concurrency limit that enables you to set a maximum number of concurrent accepted lambdas. If the number of requests exceeds that limit the overflow requests will receive an error 500 response.
To set the concurrent limit you have several options:
The Console
Inside the AWS console navigate to your lambda, in the configurations page scroll down to the Concurrency box, and select Reserved Concurrency (entering your desired number 50)
The Command Line
To modify the Reserved Concurrency via the command line use the following command:
aws lambda put-function-concurrency --function-name YOUR_FUNCTION_NAME_HERE --reserved-concurrent-executions 50
Serverless Framework File
If your deploying your functions with the serverless framework you can modify the Reserved Concurrency for any lambda inside the function section of you file.
service: stackoverflow # NOTE: update this with your service name
provider:
name: aws
runtime: python3.7
stage: ${opt:stage, 'dev'}
region: ${opt:region, 'us-east-1'}
profile: ${opt:profile, 'default'}
environment:
region: ${self:provider.region}
stage: ${self:provider.stage}
stackTags:
Owner : krapes
Project : concurrencyLimits
Service : concurrencyLimits
Team : brokenLeg
stackPolicy: # This policy allows updates to all resources
- Effect: Allow
Principal: "*"
Action: "Update:*"
Resource: "*"
iamRoleStatements:
functions:
dummy:
handler: dummy.main
timeout: 10
## This parameter sets the reserved concurrency for the lambda 'dummy'
reservedConcurrency: 50
# events:
# - http:
# method: GET
# path: /dummy
# resp: json
#plugins:
# - serverless-python-requirements
custom:
pythonRequirements:
dockerizePip: non-linux
Now when testing your lambda, you'll see that with the Reserved Concurrency set the excess requests were returned an error 500 code, and thus protected the system.
Reserved ConcurrencyDetails (average, fastest, slowest):
DNS+dialup: 0.0009 secs, 2.0200 secs, 6.0415 secs
DNS-lookup: 0.0002 secs, 0.0000 secs, 0.0185 secs
req write: 0.0000 secs, 0.0000 secs, 0.0030 secs
resp wait: 3.5561 secs, 2.0199 secs, 6.0414 secs
resp read: 0.0001 secs, 0.0000 secs, 0.0032 secs
Status code distribution:
[200] 5000 responses
Reserved ConcurrencyDetails (average, fastest, slowest):
DNS+dialup: 0.0007 secs, 0.0094 secs, 5.6580 secs
DNS-lookup: 0.0000 secs, 0.0000 secs, 0.0119 secs
req write: 0.0000 secs, 0.0000 secs, 0.0033 secs
resp wait: 1.1845 secs, 0.0093 secs, 5.5826 secs
resp read: 0.0000 secs, 0.0000 secs, 0.0032 secs
Status code distribution:
[200] 1638 responses
[500] 3362 responses
The outputs above were generated using the lambdaLoadTesting tool without reservedConcurrency AND with it set to 25.