Skip to content

Commit 3a84d93

Browse files
Merge pull request #91 from socraticDevBlog/20250324-keepcostsafe
feat: reduce API Gateway capacity and add email notification monitoring for excessive usage
2 parents 4d8f052 + 644bee3 commit 3a84d93

File tree

2 files changed

+42
-6
lines changed

2 files changed

+42
-6
lines changed

.github/workflows/pytest.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ jobs:
2121
run: |
2222
python -m pip install --upgrade pipenv wheel
2323
- id: cache-pipenv
24-
uses: actions/cache@v1
24+
uses: actions/cache@v3
2525
with:
2626
path: ~/.local/share/virtualenvs
2727
key: ${{ runner.os }}-pipenv-${{ hashFiles('**/Pipfile.lock') }}

terraform/main.tf

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,8 @@ resource "aws_apigatewayv2_stage" "default" {
247247

248248
content {
249249
route_key = route_settings.value.route_key
250-
throttling_burst_limit = 5
251-
throttling_rate_limit = 2
250+
throttling_burst_limit = 1
251+
throttling_rate_limit = 1
252252
}
253253
}
254254

@@ -257,7 +257,7 @@ resource "aws_apigatewayv2_stage" "default" {
257257

258258
content {
259259
route_key = route_settings.value.route_key
260-
throttling_burst_limit = 2
260+
throttling_burst_limit = 1
261261
throttling_rate_limit = 1
262262
}
263263
}
@@ -326,8 +326,44 @@ resource "aws_sns_topic" "budget_notification" {
326326
name = "BudgetNotificationTopic"
327327
}
328328

329-
resource "aws_sns_topic_subscription" "email_subscription" {
330-
topic_arn = aws_sns_topic.budget_notification.arn
329+
locals {
330+
email_alarm_topics = {
331+
budget_notification = aws_sns_topic.budget_notification.arn
332+
api_requests_notification = aws_sns_topic.api_requests_notification.arn
333+
}
334+
}
335+
336+
moved {
337+
from = aws_sns_topic_subscription.email_subscription
338+
to = aws_sns_topic_subscription.email_alerts["budget_notification"]
339+
}
340+
341+
resource "aws_sns_topic_subscription" "email_alerts" {
342+
for_each = local.email_alarm_topics
343+
topic_arn = each.value
331344
protocol = "email"
332345
endpoint = var.notification_email
333346
}
347+
348+
resource "aws_cloudwatch_metric_alarm" "api_requests_alarm" {
349+
alarm_name = "APIGatewayRequestAlarm"
350+
comparison_operator = "GreaterThanOrEqualToThreshold"
351+
evaluation_periods = 1
352+
metric_name = "Count"
353+
namespace = "AWS/ApiGateway"
354+
period = 1200 # 20 minutes (adjust as needed)
355+
statistic = "Sum"
356+
threshold = 1000 # Trigger alarm when requests exceed 1000
357+
358+
dimensions = {
359+
ApiId = aws_apigatewayv2_api.http_lambda.id
360+
}
361+
362+
alarm_description = "Alarm for API Gateway requests exceeding 10,000 in the evaluation period."
363+
364+
alarm_actions = [aws_sns_topic.api_requests_notification.arn]
365+
}
366+
367+
resource "aws_sns_topic" "api_requests_notification" {
368+
name = "APIRequestsNotificationTopic"
369+
}

0 commit comments

Comments
 (0)