021. Spend control: Infracost ceiling + budgets, alarms and kill switch
- Date: 2026-11-17
- Status: Accepted
- Supersedes: RAG016. Infrastructure cost visibility and spend enforcement: Infracost + Budgets Actions
- Builds on: RAG001. Environment topology: QA and Prod separate VPCs, RAG009. API authentication: bearer-token Lambda authorizer + Aurora-backed key store, RAG020. Observability: Powertools Logger + EMF + per-env alarms
Decision
Section titled “Decision”In the context ofa single-account, Terraform-managed AWS stack (RAG001) where Bedrock inference is the main variable cost, has no native hard cap, and is reached through the API via operator-issued or CI-minted/1-day API keys, each with a request quotafacingthree needs: fixed-price cost checked before merge, proactive spend and abuse warnings, and a method to stop spend fast; and the constraint that “AWS Budgets” refreshes only a few times a day,we decided for3 layers of control and one remediation:- Layer 1: A pre-merge ceiling: Infracost prices
terraform planduring Terraform PRs, and a blocking check on the monthly total (see the ongoing deployment guide); cost diffs visible as PR comments, - Layer 2: Spend detection: two account-level AWS Budgets, a lower daily budget on a Bedrock services filter and a higher unfiltered monthly budget, plus per-environment CloudWatch alarms from RAG020,
- Layer 3 deferred: an AWS Budgets Action that denies Bedrock on breach is deferred until either:
- API keys are issued without operator action, other than per-run CI keys whose expiry and quota are fixed in reviewed code, or
- the remaining quota of all unexpired operator-issued and CI keys, multiplied by the worst-case cost per request, exceeds the higher unfiltered monthly account budget,
- Kill switch remediation: set API Lambda reserved concurrency to 0 to pause service; remove it to restore service,
- Layer 1: A pre-merge ceiling: Infracost prices
and neglected- a Budgets Action, as RAG016 decided: the lag in “Budgets Actions” demotes it from a “guard”, to a recovery step,
- per-environment budgets filtered on a cost allocation tag: the tag must be activated before it filters anything, and on-demand Bedrock usage is not known to carry it,
- AWS Organizations service control policies: a strong enforcement path, but multi-account structure is disproportionate for a demo,
- a blocking cost-delta gate, in Rego or as an Infracost threshold: it checks price delta not total,
- a kill switch on the Bedrock IAM permissions: it works, but editing a policy is slower and more error-prone than the concurrency setting,
to achieve- a Terraform change that adds a fixed-price resource above the ceiling fails during PR (before merge and before apply),
- notification time: Bedrock overspend within hours, request flood on an unauthenticated route (
/healthz) within minutes, - API spend can be stopped or restored in seconds,
accepting- Infracost checks fixed-price items only; Aurora Serverless v2 and Bedrock do not appear, so Layer 1 gives no signal on this project’s highest-cost items,
- daily budgets check actual spend only, and an alert can arrive up to about half a day after the breach,
- a
POST /queryflood with valid keys surfaces through the Bedrock daily budget, hours later, - Layer 3’s second trigger is computed by hand at key issuance, and fixed costs and Bedrock callers that use no key (eval judges, local ingestion) draw on the budget outside that computation,
- QA and Prod spend are combined when viewing account-level budgets due to this project’s account strategy,
- the kill switch is manual, and Bedrock calls that bypass the API (CI evaluation, local ingestion) are not subject to it,
- fixed costs keep accruing while the kill switch is on,
- with the API Lambda at 0, the authorizer still runs on each request, spending key quota and waking Aurora; the authorizer’s reserved concurrency of 5 and the route throttle bound this, and the runbook includes a step to stop the authorizer too.
Consequences
Section titled “Consequences”Layer 3’s trigger is a property of key issuance. Any change that lets a key be issued without the operator, such as self-serve or public keys, or that raises the quota or per-request cost a key can carry, needs the trigger checked first. When it holds, the Budgets Action becomes part of the design before that change ships.
The kill switch competes with Terraform: the next apply of that environment’s root restores the configured concurrency. A push to rag-sample-app main triggers the QA Deploy workflow, which reapplies the QA stack, so while the switch is on in QA, the hold means no merges to app main and no QA Deploy runs; for Prod, no Prod apply.
© 2026 Benjamin Arunski