DEV Community

Prabhakaran Jayaraman Masani
Prabhakaran Jayaraman Masani

Posted on Originally published at prabhakaranjm.medium.com

One Role, Many Agents: Least-Privilege AI Agents on AWS with STS GetScopedCredentials

How to hand each agent and sub-agent its own narrowed, short-lived credentials without creating a new IAM role for every task

The problem: agents inherit everything

Most agent stacks on AWS share one awkward habit. The orchestrator runs under an IAM role (a Lambda execution role, an ECS task role, an EC2 instance profile or a developer's aws login session), and every tool call, sub-agent and code-execution step quietly reuses those same credentials. The model decides at runtime which API to call, often based on prompts or tool output it does not fully control. If the role can delete a table, so can the agent, whether or not that was ever part of the task.

The usual fixes are heavy. You can create a dedicated role per agent and per task and chain AssumeRole calls, but that means more trust policies, more roles to govern and a role-sprawl problem that grows with every new tool. Session policies on AssumeRole help, but you need a role to assume in the first place, and the session takes on a new role identity.

On 19 November 2026 AWS announced a simpler building block: AWS STS GetScopedCredentials. It takes the temporary credentials you already have and returns a narrower copy: fewer permissions, a shorter lifetime, or both, while keeping the caller's identity. For agent builders, that maps neatly onto the "orchestrator delegates to sub-agent" pattern.

How GetScopedCredentials works

Based on the API reference and the AWS Security Blog launch post:

  • You call it with temporary credentials only (assumed role, federated session, Lambda or EC2 role credentials). IAM user and root credentials are not accepted; for an IAM user you first call GetSessionToken.
  • You pass a scope down policy (one inline IAM policy, up to 2,048 characters), a DurationSeconds value, or both. At least one is required.
  • The resulting permissions are the intersection of the calling session's effective permissions and the scope down policy. It can never add permissions.
  • The duration cannot exceed the caller's remaining session lifetime. If you omit it, the new credentials inherit the caller's expiry.
  • The new credentials keep the same identity: same role ARN, role session name, session tags and source identity.
  • You can call it recursively with already-scoped credentials. Up to three scope down policy layers are allowed per session, and every earlier layer must still allow sts:GetScopedCredentials for the next call to work.
  • An optional ScopedSessionDescription (up to 256 characters) is written to AWS CloudTrail, so you can record why a scoped session exists.
  • It is not available on the STS global endpoint, so use a Regional STS endpoint.

That last identity point is the key difference from AssumeRole. You are not switching roles; you are shrinking the session you are already in.

Architecture: an orchestrator that mints credentials per sub-agent

The pattern looks like this in practice:

  • The orchestrator runs under one role that covers the union of what all its tools might need.
  • Before starting a sub-agent or invoking a tool, a small, trusted credential broker in your code (not the model) chooses a scope down policy for that task and calls GetScopedCredentials.
  • The sub-agent only ever receives the scoped credentials. If it starts its own helper, it can narrow them again but never widen them.
  • CloudTrail records each call with its description, and later API calls made with scoped credentials carry the scoped session's access key ID, which you can trace back to the parent session.

The same pattern works for multi-tenant SaaS: after authenticating a request, the broker scopes the shared role down to that tenant's S3 prefix before handing credentials to the worker.

Hands-on

1. Allow the caller to scope down

The calling role needs permission to call the API. From the AWS launch post:

{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": "sts:GetScopedCredentials",
    "Resource": "*"
  }]
}
Enter fullscreen mode Exit fullscreen mode

You can tighten this further with the sts:DurationSeconds condition key, and with global keys such as aws:PrincipalArn, aws:PrincipalTag or aws:SourceVpce to control who can call it and from where.

2. Write a task-level scope down policy

For a research sub-agent that should only read from one bucket:

{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Action": ["s3:GetObject", "s3:ListBucket"],
    "Resource": [
      "arn:aws:s3:::amzn-s3-demo-reports",
      "arn:aws:s3:::amzn-s3-demo-reports/*"
    ]
  }]
}
Enter fullscreen mode Exit fullscreen mode

If you would rather keep most permissions and just put guardrails in place, AWS documents an Allow plus NotAction pattern. Because a scope down policy can only reduce access, this keeps everything the session already had except the listed actions:

{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "NotAction": [
      "iam:*", "organizations:*", "account:*", "sts:AssumeRole*",
      "s3:DeleteBucket", "ec2:TerminateInstances",
      "rds:DeleteDBInstance", "dynamodb:DeleteTable"
    ],
    "Resource": "*"
  }]
}
Enter fullscreen mode Exit fullscreen mode

Remember dependent actions. If a tool uses Athena over S3 data, the scoped credentials also need s3:GetObject on that data. AWS's open source IAM Policy Autopilot can generate a starting policy from the SDK calls in your code, which you then tighten by hand.

3. Call it from the CLI

With the latest AWS CLI, using a Regional endpoint:

aws sts get-scoped-credentials \
  --policy file://research-agent-policy.json \
  --duration-seconds 900 \
  --scoped-session-description "research-subagent ticket-1234" \
  --region eu-west-2
Enter fullscreen mode Exit fullscreen mode

The response contains new Credentials, a SessionInfo block whose ARN matches your calling session, ScopedPoliciesCount, and two size fields, SessionTokenSize and SessionTokenUtilization.

4. A credential broker in Python (illustrative)

The snippet below is illustrative and untested. It assumes a boto3 version that includes the new operation, with parameter names taken from the API reference (Policy, DurationSeconds, ScopedSessionDescription). Check your SDK version before relying on it.

import json
import boto3

REGION = "eu-west-2"
sts = boto3.client("sts", region_name=REGION,
                   endpoint_url=f"https://sts.{REGION}.amazonaws.com")

def scoped_session(policy: dict, seconds: int, purpose: str) -> boto3.Session:
    """Return a boto3 Session whose credentials are a narrowed copy of ours."""
    resp = sts.get_scoped_credentials(
        Policy=json.dumps(policy, separators=(",", ":")),
        DurationSeconds=seconds,
        ScopedSessionDescription=purpose[:256],
    )
    c = resp["Credentials"]
    return boto3.Session(
        aws_access_key_id=c["AccessKeyId"],
        aws_secret_access_key=c["SecretAccessKey"],
        aws_session_token=c["SessionToken"],
        region_name=REGION,
    )

READ_REPORTS = {
    "Version": "2012-10-17",
    "Statement": [{
        "Effect": "Allow",
        "Action": ["s3:GetObject"],
        "Resource": "arn:aws:s3:::amzn-s3-demo-reports/*",
    }],
}

# The broker, not the model, picks the policy for each tool or sub-agent.
research_session = scoped_session(READ_REPORTS, 900, "research-subagent")
s3 = research_session.client("s3")  # pass this client to the sub-agent's tools
Enter fullscreen mode Exit fullscreen mode

The important design choice is where the policy comes from. Keep a fixed catalogue of task policies in code or configuration, reviewed like any other IAM change. Do not let the model write its own scope down policy; at best that recreates the over-permissioning problem, at worst it hands policy authorship to prompt injection.

Trade-offs, security and cost notes

  • It narrows, it does not isolate. If the parent credentials are reachable from the sub-agent's process (environment variables, the instance metadata service, a shared credentials file), the sub-agent can simply use those instead. AWS recommends running agents in isolated or sandbox environments so they only see the scoped credentials. On AWS that might be a separate container, MicroVM or sandbox per sub-agent.
  • The ceiling is still the parent role. Scoping cannot fix an over-broad base role. Keep the orchestrator role reasonable and use permission boundaries and SCPs as the outer fence.
  • Policy size limits are real. The inline policy is capped at 2,048 characters, and the packed session token has its own limit (PackedPolicyTooLarge). Session tags count too, so watch SessionTokenUtilization.
  • Three layers maximum. Plan your delegation depth. Duration-only calls do not count towards the limit.
  • Audit by description. Make ScopedSessionDescription meaningful (agent name, task or ticket ID) so CloudTrail actually explains who did what.
  • Throughput. At launch AWS states a dedicated quota of 200 requests per second per account per Region. Cache scoped sessions per task rather than per tool call.
  • Service coverage. Scoped credentials work with most AWS services, but AWS publishes a list of unsupported ones; check it for your tool set.
  • Cost. The main cost is engineering effort: designing and reviewing task policies. Check current AWS STS pricing and request quotas for your account rather than assuming.

Compared with Google Cloud: GCP tends to approach the same goal through separate identities (service accounts and newer per-agent identities) plus token downscoping for specific services such as Cloud Storage's Credential Access Boundaries. GetScopedCredentials is notable for being a general, identity-preserving scope down across most AWS services from a single API.

Conclusion

GetScopedCredentials closes a gap that has quietly existed in many AWS agent designs: the "one role, many tools" problem. Instead of multiplying roles, you keep one well-governed role and have a trusted broker mint a narrower, short-lived copy for every sub-agent and tool, with each hand-off recorded in CloudTrail. Pair it with real process isolation and a reviewed catalogue of task policies, and least privilege for agents becomes a few lines of code rather than a role-management project.

References

Top comments (0)