AWS Builder Center
The Role That Starved

The Role That Starved

An IAM role that starts empty and earns each permission from a real AccessDenied — raced against grant-then-trim on live AWS, with the over-privilege window measured by Access Analyzer itself.

AI Developer
For about ninety seconds, my support agent could have deleted every table in the account.
It didn't. It had no reason to, and no intent I could find in the logs afterwards. But for that minute and a half — while it read a config file, looked up a customer, and got on with an entirely mundane job — emptying my data was one decision away, inside a role I'd built for it myself.
That it could is the whole story.

The role was broad because I'd made it broad, and I'd made it broad because that's the advice. I had a new support-triage agent to stand up — read a config file, look up a customer, write an audit row, log a line, summarise the ticket with a model — and I didn't yet know exactly which API calls it would make. So I did what the guides say: grant it wide, let it run, watch what it touches, tighten afterwards with Access Analyzer. Start loose, clean up later. I typed  s3:* ,  dynamodb:* ,  logs:* ,  bedrock:*  into the role and turned it loose.
For a script that does the same twelve things every morning, that advice is fine — the window where it holds too much is short and boring, and nothing chooses to walk through it. But my agent isn't a script. A model picks its next move. And that one fact moves the danger somewhere the advice doesn't reach: the ninety seconds where a thing that can be talked into anything is holding the keys to everything.
So I wanted to build the role the other way round — from nothing — and find out, in real numbers on a real account, what that cost me.
This is the walk-through I wish I'd had.

The idea, in one sentence
Don't grant permissions. Earn them.
Start the agent with an empty policy — a role that can do nothing at all. Let it try its job. Every call fails with  AccessDenied , and every denial names exactly what it wanted. Read the action and the resource out of the failure, grant precisely that, and let it try again. The role climbs from nothing to a working policy one refusal at a time, and at no single moment does it hold a permission it hasn't already been forced to need.
The whole thing turns on one fact about IAM that's easy to forget: a denial is not just a wall. It's a perfectly specified request. AWS tells you the action and the resource it would have taken, which is exactly the information you'd need to grant it. The wall is holding the blueprint.

Starting at nothing




The role begins with no inline policy at all. Not a reduced policy — an empty one. With nothing attached, IAM's implicit deny covers everything, which is the clean floor I want every run to start from.
iam.create_role(
RoleName="starving-role",
AssumeRolePolicyDocument=json.dumps(trust), # only I can assume it
Description="Earns each permission from a real AccessDenied.",
)
# no put_role_policy. it can do nothing, on purpose.
Then the agent assumes that role and starts its task. The first thing it does is fail:
AccessDenied: not authorized to perform: s3:GetObject on resource:
arn:aws:s3:::felina-lab-config/config.json
Good. That denial is the first instruction.
The loop that earns




Here's the core, and it's smaller than you'd expect. The agent, running under the empty role's temporary credentials, attempts a call. If it's denied, I pull the action and resource straight out of the message, add one scoped statement granting precisely that, and — the part that took me two tries to get right — re-assume the role so the next attempt runs under fresh credentials that can see the new policy.
for action, resource in agent.attempts():
for _ in range(RETRIES):
result = attempt(session, action, resource)
if result.ok:
break
grant = parse_denial(result.message) # ("s3:GetObject", "arn:aws:s3:::…/config.json")
policy.add(grant) # one scoped statement
iam.put_role_policy(
RoleName="starving-role",
PolicyName="earned",
PolicyDocument=policy.as_json(), # the role grows by one line
)
time.sleep(SETTLE) # IAM needs a moment (more on this below)
session = assume("starving-role") # fresh creds, new policy
Parsing the denial is a small regex, because AWS words these consistently:  to perform: <service:Action> on resource: <arn> . Pull those two out and you have a grant scoped to exactly one action on exactly one resource — never a wildcard.
And that's the quiet win. Because every statement is minted from a real refusal, and the refusal names a real ARN, the role never asks for  s3:* . It asks for that object. Across six runs, not one grant fell back to a wildcard resource. One denial for the log write even named the log stream — so the role earned a permission narrower than the  log-group:*  I'd have lazily typed by hand. The wall wrote a tighter policy than I would have.
Five walls, five keys, task done — by a role that a minute earlier could do nothing:
s3:GetObject arn:aws:s3:::felina-lab-config/config.json
dynamodb:GetItem arn:aws:dynamodb:us-east-1:…:table/felina-lab-customers
dynamodb:PutItem arn:aws:dynamodb:us-east-1:…:table/felina-lab-customers
logs:PutLogEvents …:log-group:/felina-lab/support-agent:log-stream:run
bedrock:InvokeModel arn:aws:bedrock:us-east-1::foundation-model/amazon.nova-lite-v1:0
The scars I'll save you




Three things bit me, and every one of them is the useful part.
IAM is not instant, and the loop feels it. When you  put_role_policy  and re-assume half a second later, the fresh session sometimes still gets denied — the new statement hasn't propagated to where the request is evaluated yet. Across six runs I counted 39 of these repeat denials: the role refused for something I'd already granted it, because AWS hadn't caught up with itself. The loop handles it by waiting and retrying, and nothing ever got permanently stuck, but budget for it. "Earn a permission the instant you need it" includes a few seconds where the permission exists on paper and not yet in effect. If you build this, put the sleep and the re-assume in from the start; don't discover them at run four.
Resetting between runs will lie to you. I wanted six clean trials, so I dropped the earned policy between each one to start fresh. The first time, the next run's opening call just… succeeded. No denial, no grant. For a second that looked like a result. It wasn't — it was the same propagation lag in reverse: IAM had accepted my delete and told me so instantly, while the just-assumed session was still being evaluated against the permissions it held a moment ago. If I'd trusted it, every run after the first would have quietly inherited the last one's keys. The fix is to verify by behaviour, never by the API's word: poll the role with a known call until it's actually denied again, and only then start earning.
def wait_until_starving(role):
while True:
s = assume(role)
if denied(attempt(s, "s3:GetObject", CONFIG_ARN)):
return # confirmed empty; safe to start the next run
time.sleep(4)
The role earns the agent's mistakes, too. My agent doesn't only make the five calls it needs. Like anything a model drives, it explores — it tried to  Scan  the table, to  ListBucket , to  DescribeTable , none of which the task required. The loop granted those too, faithfully, because they arrived as real denials and got scoped and added. So the earned role is least privilege relative to what the agent attempted, not to what it needed: precision landed around 0.77, roughly one or two honest over-grants a run. Starving the role doesn't fix an over-curious agent. It just stops rewarding curiosity with a wildcard — the over-grant is  dynamodb:DescribeTable  on one table, not  dynamodb:* .
What the empty start actually buys you




Here's the part I got wrong in my own head before I measured it.
I assumed the starving role would end up tighter than the broad-then-trimmed one. It doesn't. If you let it run broad and hand the logs to Access Analyzer, you land at more or less the same final policy — same actions, same job done. On the finished artefact, it's a tie.
So the smaller policy was never the point. The difference is everything that was reachable on the way there.
I measured what the broad role put within reach while it ran: nineteen actions across the four services, eight of them destructive —  DeleteObject ,  DeleteBucket ,  DeleteItem ,  DeleteTable ,  DeleteLogGroup , and the rest. The agent needed none and called none. But for the whole first run — the run whose only purpose was to watch an unpredictable thing — deleting my table sat one sentence away in a model's context. The starving role never opens that window. It's least privilege the entire time it runs, because at every instant it holds only what its past refusals have earned.
And I didn't want that to be my word against anyone's, so I let Access Analyzer referee it. Its  check-access-not-granted  call takes a policy and a set of actions and tells you, flatly, whether the policy grants any of them:
aa.check_access_not_granted(
policyDocument=broad_policy, # the grant-then-trim baseline
access=[{"actions": ["s3:DeleteObject","dynamodb:DeleteTable","logs:DeleteLogGroup"]}],
policyType="IDENTITY_POLICY",
)
# -> result: FAIL "the policy document grants access to one or more of the listed actions"
The broad baseline comes back FAIL. The role my agent starved into comes back PASS. Same neutral tool, opposite verdicts, and not a number I chose.
The denial that keeps its job




Then the part I didn't expect to like as much as I do.
Once the role has earned its stable policy, every new denial means something. The permissions it needs, it has — so a fresh  AccessDenied  is no longer a step in building the role. It's the role telling you it just tried something it has never, in its whole life, needed to do.
I set the role to its earned policy and had the agent reach for one thing the task never used:
AccessDeniedException: not authorized to perform: dynamodb:Query on resource:
…:table/felina-lab-customers because no identity-based policy allows the action
The same mechanism that granted least privilege on the way up is, once it's settled, an intrusion signal. A policy that Access Analyzer generated once, offline, from a log is a snapshot — and a snapshot doesn't notice anything. A role that earned every permission it holds does notice, because a denial is now a departure from a history you can trust. You get the tripwire for free, out of the same refusals that built the role. Point an EventBridge rule at  AccessDenied  in CloudTrail and the thing that built your policy also watches it.
Where this sits, honestly




This is well-trodden ground and you should know who's on it. Access Analyzer generates a policy from your CloudTrail history. IAM role manager, new this August, hands you an AWS-managed default role to start from. There's lovely academic work too — an OOPSLA 2024 paper from Amazon Science that formally reduces an over-broad policy to observed usage — and on the runtime side, tools like ARMO watch an agent for weeks and then enforce on deviation. All of them good, and all of them the same shape: they start above least privilege and come down, which means they need a window where the agent has more than it should so there's something to observe. Earning from zero is the one direction nobody's shipped.
One of those tools, I couldn't even get to play. I tried to have Access Analyzer generate the broad role's policy the real way, from the trail — and it couldn't. Partly an access-role permissions snag, but underneath it a sharper reason worth more than the experiment I'd planned: my account's trail logs management events only, and everything my agent does — reading the object, the DynamoDB item calls, invoking Bedrock — is data-plane. A management-events trail can't see any of it. The tool meant to trim your agent's role can be structurally blind to everything your agent does, and it fails quietly. The starving role captured all five needs anyway, because it never reads a log. It learns from the refusal in the moment.
What I haven't earned the right to say yet

One honest limit, because I'd rather flag the seam than paint over it. For these six runs the agent's decisions were scripted with controlled randomness, running against real IAM — real denials, real grants, real propagation — but not a live model choosing its own moves. So when I say the earned policy's floor converges (the five real needs, every run) while its ceiling wanders (ten distinct permissions across six runs, only five in all of them), I've shown the mechanism holds under a varied request stream — not that a language model's earned permissions drift run to run. That experiment, the same loop with a model in the driver's seat, is the next one. I know how it'll be built; I haven't run it, so I'm not going to tell you how it comes out.
And I'm not suggesting you wire production agents to grant themselves IAM permissions. Watching a thing rewrite its own role live, even in a sandbox account I was babysitting, is a way to measure something the usual workflow hides — not a pattern I'd deploy. If you want to run it yourself, it's one command up and one command down; the probes fill in the table on your own account, including the last column, the tripwire firing on a permission the role never needed.
The quiet thing it changes




I still start most roles the normal way. Most workloads do the same twelve things every morning, and for those, grant-then-trim is fine — the window where they're over-powered is short and boring and nothing chooses to walk through it.
But the agent I gave  dynamodb:*  to last week doesn't do the same twelve things. It decides. And for a thing that decides, the honest version of least privilege isn't a cleanup you do afterwards — because a cleanup assumes you survived the mess in between. The starving role doesn't clean up. It starts with nothing and eats only what it's forced to. It gets hungry before it's allowed to eat.
I never did type  dynamodb:*  again. I let the wall decide, and the meals it refuses now are how I find out something's wrong.
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