Skip to content
All writing
Aurora DSQL30 Nov 20269 min read

Stub: how Aurora DSQL, IAM and AWS Backup let the database refuse my AI agents' overspend

A ticket shape beside a line that falls and then stops at a floor, with a check-mark seal below it.

Can a budget hold when the code that checks it is skipped?

-$7.00. On my spend ledger, ten writers that skipped the check took a $3.00 budget to -$7.00 on Aurora DSQL, in 20 runs out of 20. Today the same race stops at $0.00 in every run, and the database refuses the seven writes that do not fit.

Stub is a double-entry spend ledger that gives a fleet of AI agents one budget they can't overspend. It is live at trystub.vercel.app on Amazon Aurora DSQL in us-east-1 and us-east-2. The project post tells how the Well-Architected Review skill of Agent Toolkit found the problem. The before and after takes 90 seconds on YouTube.

This article is about the tools. Aurora DSQL gave me a database that refuses the overspend for every role, in two Regions at once. AWS gave me an identity with no stored key, backups I have restored, a record of every change and a failover I have measured.

The rule every service had to hold

A spend the budget can't cover fails, whoever sends it.

My app checked that in core/ledger.ts, and that check was the only one. The app connected to the database as admin, and it reached AWS through an IAM user with AdministratorAccess and a long-lived key. A script, a hotfix or a new route could skip the check, and nothing would stop the write. The review rated SEC03-BP01, "Grant least privilege access in workload components", Critical for exactly that.

Who decides whether a spend fits, before and after

The app's check is still there. The database no longer depends on it.

Why Aurora DSQL

CHECK constraints: a refusal no code path can skip

Every balance table has CHECK (balance_micro >= 0). Accounts also have CHECK (cap_micro IS NULL OR balance_micro <= cap_micro), so a bug can't mint budget either. A write that breaks either one fails with SQLSTATE 23514, whichever role sends it, admin included. When that 23514 reaches the app's own spend path, the app records it as a denial with the reason db_refused.

I measured it on a temporary DSQL cluster. Ten writers each took $1.00 from a $3.00 budget with a plain UPDATE, as the old app's admin role. Only the schema changed between the two sets of runs.

Running balance before and after the CHECK constraint

Ten writers, one $3.00 budget. 20 runs before the CHECK, 20 after, every run the same.

20 runs each Before With the CHECK
Writes committed per run 10 3
Writes refused per run 0 7, all 23514
Final balance -$7.00 in every run $0.00 in every run
Money accepted that wasn't in the budget $140.00 $0.00

NOT VALID, then ALTER TABLE ASYNC: adding it to a live cluster

DSQL accepts a CHECK on an existing table only as NOT VALID, followed by ALTER TABLE ASYNC … VALIDATE CONSTRAINT. That is a job that scans the table and marks the constraint valid. npm run migrate adds the constraint when pg_constraint lacks it and waits until convalidated is true. On Postgres the same script runs a plain VALIDATE.

On production, a precheck found 0 accounts above their cap. The cap constraint then ran from 17:35:02 to 17:35:39 UTC and read back as valid from both Regional endpoints.

Before With ALTER TABLE ASYNC
A schema change I would have to schedule around 37 seconds on the live cluster, with no downtime
A constraint I would have to trust was applied convalidated = true, read from both Regions

Optimistic concurrency: order, not affordability

DSQL has no row locks. Two transactions that write the same row conflict, and the loser gets SQLSTATE 40001 and retries. In the race above, DSQL put the writers in order with up to 45 retries in a run. My old concurrency test relied on that, and it was the wrong thing to rely on. The retries fixed the order of the writes. Nothing decided whether a write was affordable. Now the two work together: the conflict puts writers in order, and the constraint refuses the ones that no longer fit.

Before With the CHECK beside OCC
40001 retries, then every writer commits 40001 retries, then the writers that don't fit get 23514

Active-active in two Regions

I picked DSQL because it is active-active with strong consistency. A writer in us-east-1 and a writer in us-east-2 can race for the same budget, and both get one answer. With the constraint in the schema, that answer holds in both Regions.

Before With a multi-Region cluster
One writer Region, or replicas that settle later Two writer Regions, one answer per budget

A database role that can do less

The app now connects as stub_app. It can't update or delete ledger entries, create accounts or change the schema. All 11 write paths pass as stub_app.

This alone did not stop the race. With today's grants and the old schema, it still ended at -$7.00, because the app has to write balances. The role limits what the app can touch. The constraint decides the budget.

Before With stub_app
The app connects as admin The app connects as a role with no DDL and no rewrites
A rewrite of a ledger entry is accepted It is refused with 42501

Why AWS

IAM with Vercel OIDC: no stored key

The app assumes the stub-app IAM role through Vercel OIDC. Sessions last one hour, the role trusts only this project's production tokens, and its policy allows only dsql:DbConnect on the two clusters. Both clusters also carry a resource policy: it denies dsql:DbConnect to anyone but that role and an operator, and denies dsql:DbConnectAdmin to anyone but an operator.

Before With the OIDC role
An IAM user with AdministratorAccess A role that can only connect to two clusters
1 long-lived AWS key in the app 0
Destructive DSQL actions the app's identity may take: 6 of 6 0 of 6

AWS Backup: a restore I have run

Two backup plans, one per Region, run daily at 05:00 UTC with 35-day retention. They write to two vaults with Vault Lock in governance mode, and the daily rule copies the us-east-1 point to the us-east-2 vault.

Then I tested a restore. I restored the us-east-1 point into a new cluster, and the job took 2 min 9 s. A fingerprint script read both clusters: 7 accounts, 30 entries and 2 denials on each, 0 hash-chain problems, the same SHA-256 over every entry hash and the same sum of balances, 1,101.76 USD.

Before With AWS Backup
0 backup plans 2, daily, copied across Regions
0 restores ever tested 1, verified row for row, 2 min 9 s

CloudTrail, GuardDuty, CloudWatch, EventBridge and AWS Config: a record of every change

The account now has a multi-Region CloudTrail trail and GuardDuty in both Regions. Four CloudWatch alarms watch DSQL: OccConflicts at 50 or more per 5 minutes and QueryTimeouts at 1 or more per 5 minutes, per cluster. Four EventBridge rules catch GuardDuty findings of severity 4 or higher, and DeleteCluster, UpdateCluster, PutClusterPolicy and DeleteClusterPolicy on DSQL. They publish to an SNS topic encrypted with a customer managed KMS key. AWS Config records every change to a DSQL cluster in both Regions, and a managed rule flags any cluster without deletion protection. I tested it with a throwaway cluster that had protection off: the rule reported it NON_COMPLIANT about 12 minutes after it was created. DSQL's SQL audit log goes to a locked S3 bucket.

CloudTrail also let me check the review itself. Its second run made 78 API calls in 61 seconds, all read-only, across 11 services in two Regions.

Before With detection
No trail, detector, alarm or rule CloudTrail, 2 GuardDuty detectors, 4 alarms, 4 EventBridge rules, each seen firing
Nothing noticed a cluster without deletion protection 2 Config rules; a test cluster flagged NON_COMPLIANT
No record of who ran which SQL A SQL audit log in a locked bucket

AWS Fault Injection Service: failover measured on production

An FIS experiment template cuts connections to the primary Region's cluster, using the DSQL action aws:dsql:cluster-connection-failure. I ran it on production. On the first drill, production served from us-east-2 25.9 seconds after the fault started, and the drill found an XX000 internal error on dropped connections. I fixed the connection handling and ran it again.

Before With FIS
Failover assumed from the architecture Served from us-east-2 17.3 s into the fault, on production
An unseen error on dropped connections Found by the first drill, fixed before the second

AWS CloudFormation: the fixes stay fixed

The role, the cluster policies, the backups and the detection are templates in infra/. 8 of the 9 stacks deploy from main. A test reads the templates, and it fails if the app role is given dsql:DbConnectAdmin.

Before With CloudFormation
Changes made by hand in the console Templates, deployed from main
Nothing stops a wider grant later CI fails the change before it reaches AWS

What the review said afterwards

I ran the skill three times. The first run scored Security 1 of 5 with 3 Critical findings. The second kept Security at 1, because production still ran the old identity. The third, on the deployed app, scored Security 3 of 5 with 0 Critical findings. It opened with "Stub's strongest property is that it does not trust its own application code with money."

What it costs

About $2.50 a month, plus GuardDuty once its 30-day free period ends.

Check it yourself