Spend and abuse alerts
Operator decision D-4 and the spend-control ADR, RAG021, set the spend controls ($5/day on Bedrock, $40/month for the account, alarms, a kill switch); #385 builds them. Every alert goes to one SNS topic, rag-sample-alerts, which emails the operator. Run the commands below from PowerShell at the root of a rag-sample-app checkout, with admin credentials. <account-id> is the AWS account ID, and <env> is qa or prod. The OC-n labels are operator checks from the review action plan.
What sends alerts
Section titled “What sends alerts”| Source | Where it is defined | Fires when |
|---|---|---|
Budget rag-sample-bedrock-daily | infra/bootstrap/alerts.tf | Bedrock spend for one day passes 80% and 100% of $5. Daily budgets support only actual-spend alerts, and Budgets refreshes spend up to three times a day, so the alert can arrive up to about 12 hours after the breach |
Budget rag-sample-monthly | infra/bootstrap/alerts.tf | All account spend passes 80% of $40 (actual), or is forecast to pass 100% |
Alarm rag-sample-<env>-api-invocations | infra/modules/alarms | More than 600 API Lambda invocations in 5 minutes |
Alarm rag-sample-<env>-api-errors | infra/modules/alarms | 3 or more API Lambda errors in 5 minutes |
Alarm rag-sample-<env>-authorizer-duration | infra/modules/alarms | An authorizer invocation takes more than 8 s (its timeout is 10 s) |
Alarm rag-sample-<env>-db-awake-12h | infra/modules/alarms | Aurora stays above 0 ACU for 12 hours |
Alarm rag-sample-<env>-api-key-exhausted | infra/modules/alarms | A request leaves an API key at zero remaining requests, or is denied on an exhausted key. See API key issuance |
Each alarm also emails when it returns to OK.
One-time setup
Section titled “One-time setup”-
Find the Cost Explorer service names for Bedrock (OC-11). The budget filters on these names, and model IDs do not match them:
Terminal window aws ce get-cost-and-usage --time-period Start=<first-day-of-last-month>,End=<first-day-of-this-month> --granularity MONTHLY --metrics UnblendedCost --group-by Type=DIMENSION,Key=SERVICE --query "ResultsByTime[0].Groups[?contains(Keys[0], 'Bedrock')].Keys[0]" -
In
infra/bootstrap/terraform.tfvars, setbedrock_budget_servicesto that list andalert_emailto the address that receives alerts. -
List the existing budgets (OC-12) and delete the hand-made ones,
rag-sample-qaandrag-sample-prod. They filter on a cost allocation tag that is not active, so they never fire:Terminal window aws budgets describe-budgets --account-id <account-id> --query "Budgets[].BudgetName"aws budgets delete-budget --account-id <account-id> --budget-name <name> -
make tf-bootstrap-plan, thenmake tf-bootstrap-apply. The plan adds the topic, its policy, the subscription and the two budgets. -
Click the confirmation link AWS emails to
alert_email. No alert is delivered until you do. -
Send a test notification and confirm it arrives:
Terminal window $topic = terraform -chdir=infra/bootstrap output -raw alert_topic_arnaws sns publish --topic-arn $topic --subject "rag-sample alert test" --message "Test notification from the spend runbook." -
After a day with Bedrock usage has closed, confirm the daily budget’s filter matches spend (OC-12). Read that day’s actual amount and compare it with the same day’s Bedrock lines in Cost Explorer; zero against non-zero Cost Explorer spend means the service names in step 2 are wrong:
Terminal window aws budgets describe-budget-performance-history --account-id <account-id> --budget-name rag-sample-bedrock-daily --time-period Start=<usage-day>,End=<next-day> --query "BudgetPerformanceHistory.BudgetedAndActualAmountsList[].ActualAmount"
Each environment root creates its alarms: QA through QA Deploy on merge to rag-sample-app main, Prod through make tf-prod-apply.
When an alert fires
Section titled “When an alert fires”- Invocations. The
/querythrottle (1 req/s, burst 5) caps that route near 300 invocations in 5 minutes, so passing 600 means traffic onGET /healthz(throttled at 3 req/s, burst 6); any other path gets the gateway’s 404 and never invokes the Lambda. Read the API access log,/aws/apigateway/rag-sample-<env>-api, grouped byrouteKeyandsourceIp, to find the caller. That traffic spends no key, so revoking keys does not stop it; the kill switch does. - Bedrock budget.
make key-list ENV=<env>shows each key’s remaining requests; a key that dropped fast is the likely source. Revoke it withmake key-revoke ENV=<env> ID=<key-id>. The budget also counts Bedrock calls that bypass the API, so if no key dropped, check recent QA eval runs and local ingestion first. If API spend keeps rising, use the kill switch. - API errors. Read the API log group,
/aws/lambda/rag-sample-<env>-api, for the failing requests. Bedrock throttling and Data API errors are the usual causes. - Authorizer duration. Usually the Data API stalled on an Aurora resume. If it persists with the cluster awake, check the authorizer log group,
/aws/lambda/rag-sample-<env>-authorizer. - Database awake 12 h. Something is querying the cluster often enough that it never pauses. The API access log,
/aws/apigateway/rag-sample-<env>-api, shows the callers; an SPA tab left open polling/healthzis one cause. - Monthly budget. Open Cost Explorer grouped by service to find the line that grew.
Kill switch
Section titled “Kill switch”Setting the API Lambda’s reserved concurrency to 0 stops every API invocation, and so every Bedrock call made through the API. Bedrock calls from the QA eval CI role and local ingestion bypass it. API callers get an error until it is restored. The authorizer still runs first, so each request with a valid key still reaches Aurora and spends one request from that key; to stop that too, revoke the keys in use or set the authorizer’s reserved concurrency to 0 as well.
# Stopaws lambda put-function-concurrency --function-name rag-sample-<env>-api --reserved-concurrent-executions 0
# Restoreaws lambda delete-function-concurrency --function-name rag-sample-<env>-api
# Also stop the authorizer, so no request reaches Auroraaws lambda put-function-concurrency --function-name rag-sample-<env>-authorizer --reserved-concurrent-executions 0
# Restore the authorizer to its Terraform cap of 5 (#380)aws lambda put-function-concurrency --function-name rag-sample-<env>-authorizer --reserved-concurrent-executions 5The next apply of that environment’s root resets reserved concurrency to the Terraform value (unreserved for the API Lambda; 5 for the authorizer, #380), which undoes the kill switch. For QA, hold merges to rag-sample-app main and manual QA Deploy runs while it is on. For Prod, do not run make tf-prod-apply.
Rehearse it once on QA (OC-4). First run aws lambda get-account-settings --query AccountLimit and note ConcurrentExecutions and UnreservedConcurrentExecutions. Setting the API Lambda to 0 consumes no unreserved concurrency. Lambda keeps at least 100 unreserved, so restoring the authorizer to 5 and the Terraform apply each need UnreservedConcurrentExecutions to stay at 100 or more after the reservation; on a low-limit account those are the steps that can fail. Then stop, confirm /query fails and the API Lambda’s Invocations metric stays flat, then restore and confirm make qa-smoke passes. The /query check spends one request from the key it uses.
© 2026 Benjamin Arunski