AWS Builder Center
Code on Bedrock: Governed Credential Vending & Cost Metering for Bedrock Coding Assistants

Code on Bedrock: Governed Credential Vending & Cost Metering for Bedrock Coding Assistants

A serverless control plane that vends short-lived AWS credentials to developers using Bedrock-backed coding assistants, meters spend, and enforces per-tier budgets with fast daily warnings and monthly kill-switches.

Get the tool  here !

What this is

If you've rolled out a third-party agentic coding harness ( goose ,  opencode , or really any tool that speaks SigV4 to Amazon Bedrock), you've probably already asked yourself two questions: who is allowed to call which models, and how much is this about to cost me.
A number of customers running third-party agentic coding harnesses against Amazon Bedrock have asked for the same thing: guidance on how to meter and control spend when a whole team of developers is pointed at Bedrock through tools like goose or opencode. Agentic workloads can burn through a budget a lot faster than a human clicking around a console: a single loop with a stale conversation context can rack up real money before anyone notices. Now imagine an entire team of developers pointed at Bedrock through their coding assistant of choice, with long-lived IAM keys sitting in their  ~/.aws/credentials  and no per-person spending limit in sight.
Code on Bedrock is a deployable CDK reference implementation that closes that gap. It's a serverless control plane that:
  • Vends 15-minute STS credentials via the AWS  credential_process  protocol, with no long-lived keys on developer machines
  • Meters every single invocation from Bedrock model-invocation logs
  • Enforces per-user and per-tier USD budgets, with fast reaction to breaches
It ships with a Go CLI, a Next.js admin + user web UI, and multi-region source metering, all deployable with  cdk deploy --all  (+ some extra work, see/use  scripts/deploy.sh )

Why I built this

Once you let developers loose on Bedrock through their own coding assistant, you're missing two things that a proprietary SaaS coding assistant would normally hand you out of the box via seats and tiers: who is spending what, and who is allowed to touch which model. A shared API key or a single long-lived IAM user gives you neither: everyone looks the same on the wire.
You could give each developer their own IAM user or role instead, which does get you attribution, eventually. Cost and usage data in AWS lags by hours before it shows up anywhere you'd look for it, so by the time you spot the overspend in Cost Explorer, the month is already blown. That's a reporting mechanism, not a control: it tells you what happened, not what's about to happen or how to stop it mid-month.
On top of the lag, per-developer IAM users also mean long-lived credentials sitting on laptops indefinitely, with no automatic expiry and no cheap way to revoke just one person mid-month without breaking their setup. What you actually want is something closer to how AWS SSO/STS already works for console access: short-lived credentials, scoped to what a person is allowed to do, expiring fast enough that a leaked credential isn't a long-term liability, plus a way to react to spend in near real time instead of waiting on cost data to catch up.
So the design goals were:
  1. No standing credentials. Every credential a developer's machine holds should expire in minutes, not months.
  1. Model access as policy, not convention. If a tier shouldn't be able to call a frontier model, that should be enforced at the IAM layer, not by asking nicely.
  1. Fast feedback on spend. A daily warning when someone is trending toward their budget, and a monthly kill-switch that actually blocks further calls, not a Cost Explorer graph you check once a week.
  1. Don't reinvent FinOps. This is scoped tightly to Bedrock coding-assistant usage, not a general cost-allocation platform.

How it works

At a high level, there are three moving pieces: credential vending, usage metering, and budget enforcement.

Credential vending

Each developer gets a small Go CLI ( codeonbedrock ) that they install once. After a one-time browser login against a Cognito Hosted UI, the CLI writes a  [profile codeonbedrock]  block into  ~/.aws/config  that uses the  credential_process  protocol. From that point on, any SigV4-aware tool (goose, opencode, or a custom script) just needs  AWS_PROFILE=codeonbedrock  set, and the AWS SDK handles the rest, transparently calling out to the CLI for fresh credentials whenever they're about to expire.
Under the hood, the CLI calls  POST /credentials  on the API, which:
  • Validates the developer's Cognito identity
  • Looks up their tier and any per-user overrides (budget limits, allowed models)
  • Calls  sts:AssumeRole  against a per-user IAM role, attaching an inline session policy that restricts the session to that user's allowed models and source Regions
  • Returns credentials that expire in 15 minutes
That 15-minute window matters for limiting exposure: if a credential ever leaked, it's only useful for 15 minutes. But it's not where the budget enforcement speed comes from. That's a separate mechanism (more on it below): when a user crosses their monthly budget, the metering pipeline attaches a deny policy directly to their IAM role. IAM policy changes are eventually consistent rather than instantaneous, but in practice that propagation lands in roughly 30 seconds, so the deny takes effect on close to the very next Bedrock call, not whenever their cached credentials happen to expire.
Model access is also enforced independently on the per-user IAM role's identity policy ( BedrockInvokeAllowed ), so it's defense-in-depth: the same rules live in two places rather than relying solely on the ephemeral session policy.

Usage metering

Every Bedrock call a developer makes shows up in Bedrock model-invocation logs. A metering Lambda watches that log group, extracts token counts per model per user, and converts them into a dollar figure using per-model rates stored in DynamoDB. Those rates aren't hand-maintained — a scheduled sync job pulls current pricing from the AWS Pricing API twice a day, so the cost math doesn't quietly drift out of date.
One subtlety that's easy to get wrong here: Bedrock records model-invocation logs and metrics in the source Region (the Region the client actually calls), not necessarily the Region a cross-Region inference profile routes the request to for inference. If you only meter the Region your inference profile targets, you'll miss usage. Code on Bedrock meters by source Region instead, so a developer calling an  eu.*  geographic inference profile from  eu-central-1  is fully metered there, even if Bedrock quietly routes the actual inference to  eu-west-1  or  eu-west-3  behind the scenes. Each additional source Region you want to allow developers to call from gets its own regional metering stack, deployed alongside the home Region's.
There's also an hourly crosscheck Lambda that compares CloudWatch metrics against what's landed in DynamoDB, to catch divergence between the two rather than silently trusting one source.

Budget enforcement

Budgets are set per tier (with optional per-user overrides) as daily and monthly USD limits. Two things happen when a user approaches or crosses them:
  • Daily warning: an email (via SES) to the user and an SNS notification to admins when someone is trending over their daily budget. This is deliberately fast; you want to know today, not at month-end reconciliation.
  • Monthly kill-switch: once a user crosses their monthly limit, the metering Lambda invokes a killswitch function that attaches a deny policy directly to that user's per-user IAM role via  iam:AttachRolePolicy . There's no waiting on credential expiry: the deny takes effect on the role itself. IAM policy propagation is eventually consistent rather than guaranteed-instant, but in practice it lands in roughly 30 seconds, so close to the next Bedrock call that user makes is rejected. No more Bedrock calls succeed until an admin unblocks them or the monthly reset job runs.
Admins also have a manual block/unblock lever for anything that needs immediate intervention outside the automated flow.

Admin and user experience

The Next.js web UI gives developers a self-service view of their own day/month spend, limits, and a "get me temporary credentials right now" button for one-off testing outside the CLI. Admins get tier management, per-user overrides, a dashboard of aggregate spend and blocked users, and CRUD over the model pricing table.

Walkthrough

Deployment gets you the infrastructure; here's what actually using it looks like end to end, from an admin standing up their first tier to a developer seeing their own usage show up.
Everyone - admin or developer - starts at the same web UI, authenticating against the Cognito Hosted UI:


1. Admin creates a tier

An admin's first job is to define a tier: a daily and monthly USD budget, plus an optional allowlist of models. Leave the allowlist empty and the tier can call any model; lock it down and members of that tier only get what you list.

Here I've created a deliberately tight  micro  tier ($0.10/day, $0.20/month, restricted to a single small model), which is a good way to sanity-check the budget enforcement without burning real spend while testing.

2. Admin creates a user and assigns them to that tier

With a tier in place, the admin creates a user by email and assigns their tier:

Behind this one form submission, the API provisions a per-user IAM role, seeds their limits in DynamoDB, and (if SES is still in sandbox mode) kicks off recipient email verification so budget-warning mail can actually reach them.

3. Admin hands the developer a one-line CLI setup command

Every deployment exposes a ready-to-share  codeonbedrock init  command with the API URL, Cognito domain, and client ID already filled in; this is available in the configuration tab in both admin and user accounts:

The developer runs that command once, then  codeonbedrock login  to complete a one-time browser login. From there, setting  AWS_PROFILE=codeonbedrock  in any shell running goose, opencode, or another SigV4 Bedrock client is all it takes — the CLI handles fetching and refreshing 15-minute STS credentials transparently via  credential_process .

4. Developer sees their own usage

Once the developer starts making calls, they can check their own day/month spend, token counts, and historical usage at any time from the My Usage tab — a stripped-down view of the same web UI, scoped to just their own data (no admin tabs in sight):

This is after some real usage: $0.00 today, but $0.11 and about 226K tokens for the month - proof the metering Lambda is actually parsing invocation logs and turning them into a dollar figure per user, not just recording that a call happened. That's the same pipeline that triggers the daily warning and monthly kill-switch described earlier.

Getting started

The project deploys as a set of CDK stacks - DynamoDB tables, Cognito, an HTTP API backed by Lambda, the metering pipeline, and the static web UI behind CloudFront. Prerequisites:
  • An AWS account with Bedrock model access enabled in each source Region you plan to meter
  • Ability to bootstrap and deploy CDK (admin-level credentials for the initial bootstrap)
  • Node.js 20+, and Go 1.21+ if you want to build the CLI yourself
  • A verified SES sender identity, for budget-warning email delivery
From there it's roughly:
npm ci
npx cdk bootstrap aws://<account-id>/eu-central-1
AWS_REGION=eu-central-1 ./scripts/deploy.sh

 deploy.sh  builds the web UI, compiles the CDK app, runs the test suite, deploys every stack, and syncs the frontend and initial model rates. The one thing it won't do for you is the one-time  cdk bootstrap  per account/Region.
After that, there's a short list of post-deploy steps: verify your SES sender, bootstrap your first admin user in Cognito, and create your initial tiers with their budgets and allowed models, which is exactly where the walkthrough above picks up.

What this is not

Worth being upfront about the scope:
  • This is a reference implementation, not an official AWS service or supported product.
  • It only governs usage that goes through the vended STS role; long-lived IAM user keys still bypass it entirely, so this only works if you actually retire those keys for your developers.
  • It's not a general-purpose FinOps or cost-allocation platform. It's scoped specifically to Bedrock usage from coding assistants.

Try it out

If you're running third-party agentic coding tools against Bedrock and have felt the same "wait, who has access to what, and how much are we spending" itch, take a look at the code and let me know what you'd change. Sample at  https://github.com/aws-samples/sample-code-on-bedrock 
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article