Reputation: 39563
AWS Lambda offers 1M free requests per month as part of their "Always Free" tier. That sounds like plenty to me, but it's possible in principle that someone could try to hammer my AWS service (e.g. a denial-of-service attack, or I just get much popular than I thought I'd be).
The service I'm building isn't mission critical, so I'd like to automatically deactivate my API gateway, taking down my API, if I'm about to go over the free tier limit.
Is that possible? If so, how?
Upvotes: 0
Views: 1193
Reputation: 13
You can use cost explorer APIs and to be specific this API - https://docs.aws.amazon.com/aws-cost-management/latest/APIReference/API_GetCostAndUsage.html
call the API at last in your lambda code, check and if the usage cross the threshold call this API (https://docs.aws.amazon.com/apigateway/api-reference/link-relation/stage-update/) to delete the stage of invoke URL.
This way you don't delete any resources but remove the public access.
other ways to deactivate API after checking usage metrics- 1 - Update API resource policy to block public access 2 - Delete the complete API 3 - Delete integrations
CLI example - aws ce get-cost-and-usage --time-period Start=2020-03-01,End=2020-03-31 --granularity MONTHLY --metrics "BlendedCost" "UnblendedCost" "UsageQuantity" --group-by Type=DIMENSION,Key=SERVICE Type=TAG,Key=Environment --filter file://t.json
t.json
{
"Dimensions": {
"Key": "SERVICE",
"Values": [
"AWS Lambda"
]
}}
Upvotes: 0
Reputation: 269284
The AWS Free Tier includes:
This gives a free monthly usage of: $0.20 + $6.66 = $6.86
Many companies find that their production usage of Lambda fits within this amount of usage.
Therefore, unless you are running heavy workloads, I wouldn't recommend you spend too much time worrying about going over the Free Tier amounts for AWS Lambda. If you do, just skip your next visit to Starbucks.
Upvotes: 2
Reputation: 78583
You could build a simple solution leveraging CloudWatch Metrics and Alarms.
Lambda function invocations are recorded as metrics. You could use CloudWatch Alarms to raise an alarm when the number of invocations exceeded X (maybe over some time interval). Have the alarm notification sent to an SNS topic, to which another Lambda function is subscribed. That 2nd Lambda function can infer the name or ARN of the Lambda function triggering the alarm and either disconnect the Lambda from its trigger or otherwise deactivate (possibly delete) the Lambda function.
Upvotes: 1
Reputation: 10254
You can set concurrency limits for your Lambda functions so it will be hard to go over limits. See https://aws.amazon.com/about-aws/whats-new/2017/11/set-concurrency-limits-on-individual-aws-lambda-functions/?nc1=h_ls
Upvotes: 0