DEV Community

Cover image for Scoping Down AI Agent Permissions per Request with AWS STS
Sascha Mirow-Stanitzok
Sascha Mirow-Stanitzok

Posted on Originally published at sascha-ms.de

Scoping Down AI Agent Permissions per Request with AWS STS

Picture this: one Lambda function serves dozens of tenants, and an AI model decides at runtime which tool to call with which arguments. The function's role can read every tenant's data, because it has to. So what stops a confused or manipulated model from reaching for another tenant's files? Until recently, mostly a prefix check somewhere in the application code, plus the hope that no tool ever forgets it.

That gap is exactly what AWS GetScopedCredentials closes. The STS API arrived on November 19, and I walk through it in detail, including a complete multi-tenant agent, in my article AWS GetScopedCredentials: Least Privilege for AI Agents. Here is the short version of why it matters and how I think about it.

The idea: shrink the credentials you already have

GetScopedCredentials takes the temporary credentials your code already holds (a Lambda execution role, an SSO session, an instance profile) and returns a second set that can do less and, optionally, expires sooner. You pass a scope-down policy, a lifetime, or both.

Three properties make it attractive for agents:

  • Effective permissions are an intersection. The new credentials can only do what both the original session and your policy allow. A policy that is too generous cannot grant anything new; it is just less strict than it could be.
  • The identity stays the same. No extra role, no new trust relationship. CloudTrail still shows the same principal, and the optional ScopedSessionDescription (up to 256 characters) lets you record why the credentials were issued, for example for which tenant.
  • It nests. Already scoped credentials can be narrowed again, up to three policy layers per session. An orchestrating agent can hand a sub-agent less than it holds itself.

Compared with AssumeRole plus a session policy, you skip the extra role and the one-hour cap of role chaining, and layers add up instead of replacing each other. My rule of thumb: if a tool permanently needs different permissions, it gets its own role. If the permissions depend on the request (the tenant, the task), this API is the better fit.

How I use it in a multi-tenant agent

The example in the full article is an assistant for a fictional bike rental platform where every shop has its own S3 prefix. The handler reads the shop ID from the JWT, validates it strictly, builds a policy for exactly that prefix and fetches scoped credentials. Only the tools receive them; the Bedrock call itself keeps the regular role.

Simplified, using the small broker from the article:

// broker wraps sts:GetScopedCredentials and caches per policy
const creds = await broker.scoped({
  policy: policyForShop(shopId),      // allow shops/<shopId>/* only
  durationSeconds: 900,
  description: "rental-assistant shop:" + shopId,
});
const toolS3 = new S3Client({ credentials: creds });
const tools = createTools(toolS3, shopId); // tools never see the role
Enter fullscreen mode Exit fullscreen mode

If the model now asks for ../../velo-hamburg/docs/prices.md, S3 simply answers with AccessDenied. The prefix check in code still exists for friendly error messages, but IAM provides the guarantee.

Two practical details: keep the broker outside the handler so warm invocations don't call STS every time (at launch the quota is 200 requests per second per account and Region), and grant sts:GetScopedCredentials to the execution role, otherwise the call fails on the implicit deny.

Where it does not save you

This is not a silver bullet, and the limits matter:

  • Only temporary credentials work as input (no IAM user or root keys), and only via regional STS endpoints.
  • Scoped credentials share the parent's issue time, so you cannot revoke them individually. Short lifetimes are your main lever.
  • The whole idea collapses if the agent can reach the parent credentials. A code-execution tool inside the same Lambda can read the environment variables, so isolate such tools.
  • With resource policies in the same account, scoped credentials can behave differently than you are used to, which leads to surprising AccessDenied errors.
  • It limits the damage, but it doesn't stop an agent from doing nonsense within its permissions. Validation, confirmations for critical actions and good logging are still your job.

For internal coding agents, a simple deny list on top of the existing role is an almost free first step. For production multi-tenant agents, an allow list per tenant is worth the effort.

What the full article covers

  • A complete TypeScript credential broker with caching and a lifetime fallback
  • Per-tenant tools, an agent loop with the Bedrock Converse API and the AWS CDK stack
  • Narrowing sub-agents further with an additional policy layer
  • Unit tests without an AWS account and a CLI check of the boundary
  • Common mistakes, limits, costs and an FAQ

You can read everything here: AWS GetScopedCredentials: Least Privilege for AI Agents

📖 This is the condensed version. The full article on sascha-ms.de goes deeper – with all code samples, diagrams and a short knowledge check.


👉 Next step: Working with AWS CDK? Grab my printable AWS CDK cheat sheet with commands, concepts and a stack skeleton.

📬 Stay updated: New articles on serverless, AI and leading tech teams via RSS, browser push or the weekly newsletter (in German).

Originally published on sascha-ms.de. More articles on serverless, AI in everyday development and leading tech teams: sascha-ms.de/en

Top comments (0)