cd /news/ai-agents/let-your-ai-agent-write-least-privil… · home › topics › ai-agents › article
[ARTICLE · art-172580] src=blog.localstack.cloud ↗ pub= topic=ai-agents verified=true sentiment=↑ positive

Let Your AI Agent Write Least-Privilege IAM Policies

A LocalStack experiment found that an AI agent using Claude Code and the LocalStack MCP server could replace an intentionally over-broad Lambda IAM role — one granting every action across nine AWS services — with a least-privilege policy. The agent used the localstack-iam-policy-analyzer tool to read IAM enforcement denials, generate a policy grouped by resource, then enabled ENFORCED mode and verified the app still worked while the role could no longer reach unrelated resources. The result matters because LocalStack's default DISABLED mode accepts AWS requests without checking IAM, letting overly broad policies like dynamodb:* with Resource: "*" go unnoticed during local development.

recommendation

Enable LocalStack IAM enforcement and have your agent run analyze-policies on the denial logs to generate a resource-grouped least-privilege policy, then re-verify the app under ENFORCED mode before shipping.

more in playbook: Securing agents and AI tools
by read8 min views2 publishedNov 19, 2026
Let Your AI Agent Write Least-Privilege IAM Policies
Image: Blog (auto-discovered)

#

IAM policies often start out too broad. A Lambda function needs to read a table, but the first deployment fails with AccessDenied. Someone adds dynamodb:* with Resource: "*" to fix it. The policy works, so no one revisits it. Soon, the function can access far more than it needs.

Writing a narrow policy by hand takes time. You must find every API call, map it to an IAM action, and identify the right resource ARN. If you miss one, the app fails only when that code path runs.

We wanted to see whether an AI agent could do this work. We gave Claude Code access to the LocalStack MCP server and an intentionally insecure serverless app. Its Lambda role allowed every action in nine AWS services. We asked the agent to replace that policy with a least-privilege policy.

The agent used the IAM Policy Analyzer to find the required permissions. It reviewed the generated policy against the code, enabled IAM enforcement, and verified that the app still worked. It also proved that the role could no longer access unrelated resources.

#

By default, LocalStack accepts AWS requests without checking IAM. This keeps local development fast, but it also hides broken or overly broad policies.

IAM policy enforcement makes LocalStack evaluate the policies that apply to each request. These include identity and resource policies, permission boundaries, and service control policies. If no policy allows a request, LocalStack returns AccessDenied.

This check also applies to Lambda functions. LocalStack gives each function temporary credentials for its execution role. Its SDK calls arrive as assumed-role/<role-name>/<function-name> and are checked against the role’s policy.

Enforcement has three modes:

  • DISABLED is the default. LocalStack allows every request.
  • SOFT_MODE evaluates every request and logs the ones that would be denied, but still lets them through.
  • ENFORCED denies requests that the policies do not allow.

Each denial is written to the LocalStack logs with the principal, the action, and the resource:

The LocalStack MCP server provides a localstack-iam-policy-analyzer tool with three actions:

  • set-mode changes the mode without restarting LocalStack.
  • get-status returns the current mode.
  • analyze-policies reads recent logs, removes duplicate denials, and creates a policy that would allow those requests. It groups actions by resource.

The agent also uses localstack-management to start LocalStack, localstack-deployer to apply the Terraform stack, and localstack-aws-client to run AWS CLI commands.

#

  • Docker installed and running.

  • A LOCALSTACK_AUTH_TOKEN from aLocalStack account . IAM policy enforcement is included in the Base and Ultimate plans.

  • Node.js 22+ to run the MCP server withnpx .

  • Terraform andtflocal ( pip install terraform-local ). The deployer tool uses them to apply the stack.

  • Python 3 and curl for the sample app’s test script.

  • Claude Code or another MCP client. The Lambda layer uses x86_64. Docker Desktop runs it through emulation on Apple Silicon without extra setup.

#

The MCP server comes with a wizard that writes the client configuration for you:

The wizard checks Docker and reads LOCALSTACK_AUTH_TOKEN from your environment. If the token is missing, it asks for one. It then finds installed MCP clients, such as Claude Code, Cursor, VS Code, and Codex, and configures the clients you select. Restart your agent to load the LocalStack tools.

You do not need to configure anything for IAM enforcement. The agent switches modes with the analyzer tool after LocalStack starts.

#

The sample app is a smaller version of the serverless blog module from AWSGoat, an intentionally vulnerable AWS project from INE.

A Python Lambda function behind API Gateway provides the blog backend. Users can register, log in, write posts, and upload images. Two DynamoDB tables store the data, and an S3 bucket stores the images.

Clone the repository:

We removed the parts that need EC2 but kept the serverless app and its IAM policy. The function’s execution role, blog_app_lambda_data, has this policy:

The function only needs two tables and one bucket. However, this policy allows any action in nine services on every resource in the account. An attacker who exploits the function would also gain those permissions.

Terraform loads the policy from resources/iam/data-lambda-policy.json. To change the role, edit this file and apply the stack again. The repository also includes exercise-api.sh. It registers a user, logs in, and tests the main routes. It fails if a route returns an unexpected status.

#

Open Claude Code in the repository folder. We used Claude Opus 5.5. This prompt gives the agent a goal and a clear sequence:

Step 4 is important for two reasons. First, a policy that allows everything produces no denials, so the analyzer has nothing to inspect. Second, SOFT_MODE lets the whole request finish while logging every missing permission. With ENFORCED, the first denial stops the request, so each run might reveal only one missing permission.

#

The agent found LocalStack stopped, started it, and deployed the stack:

The deployment output was long, so the agent checked the result and searched for errors. It then read the role’s policy, checked the enforcement mode, and tested the routes:

All eight checks passed. However, this baseline does not validate the policy because IAM enforcement was disabled.

#

The agent reduced the policy to two statements. They only allowed the function to create and write to its own log group, /aws/lambda/blog-application-data. It then redeployed, enabled soft mode, and tested the routes again:

The routes still passed because soft mode logs denials without blocking requests. LocalStack checked every DynamoDB and S3 call and logged each missing permission. The command waits eight seconds because a running function can briefly keep the old permissions after a policy update.

Next, the agent called the analyzer:

The analyzer reduced 12 denials to six unique permissions. All came from the function’s execution role. It generated one policy statement for each resource:

This is much narrower than the original policy. However, it only covers the requests that the test script made.

#

The agent reviewed the generated policy and found three gaps.

First, the S3 statement allowed only one exact object: images/20261006084009665853.png. Each upload gets a new timestamped key, so the next upload would fail. The agent changed the resource to images/*.

Second, the upload sets ACL="public-read". AWS requires both s3:PutObject and s3:PutObjectAcl for this request. The analyzer did not report the ACL action, so the agent added it.

Third, the script tests eight routes, but the function has about twenty. The agent reviewed every DynamoDB call in the code. It found untested routes that use UpdateItem, DeleteItem, and PartiQLSelect, then added each action to the correct table.

The final policy:

Each statement is limited to one table, one bucket prefix, or one log group. The permissions for untested routes came from code review, so those routes still need tests.

#

The agent applied the reviewed policy, switched to ENFORCED, and ran the routes again:

All eight checks passed with enforcement enabled. However, status codes were not enough. The upload helper catches S3 errors and still returns 200 with the message “Please try with another image.” A failed upload could therefore look successful. The agent confirmed that the object count increased and checked the logs for denials. It found none.

#

Passing routes show that the policy allows the app to work. They do not prove that it blocks unrelated access. To test that, the agent created a small Lambda function with the same blog_app_lambda_data role. It tried three actions that the blog app never uses:

The agent deployed the probe with a test bucket, a fake payroll file, and a test table. It temporarily restored the original wildcard policy, kept enforcement enabled, and invoked the probe:

It restored the least-privilege policy and ran the same probe again:

The old policy allowed all three unrelated actions. The new policy blocked them while still allowing the blog routes. The agent ran the route tests once more, then destroyed the probe stack.

#

The full process took less than eight minutes and did not need an AWS account:

Metric Value
Model Claude Opus 5.5
Cost $1.91
Time 7m 39s
Turns 44
Tool calls 43

The IAM Policy Analyzer found the actions and resources used by the app. Soft mode captured the missing permissions in one pass without stopping requests. The agent then fixed the S3 resource, added the ACL permission, covered untested code paths, and verified the result with enforcement enabled.

You can use the same process for your own stacks:

  1. Deploy the stack to LocalStack.
  2. Reduce the policy.
  3. Run your tests in soft mode.
  4. Generate and review a policy.
  5. Verify it with enforcement enabled.

Review is essential. A generated policy only reflects the traffic you ran. You must still test or inspect any code paths that were not exercised.

#

- [IAM Policy Enforcement](https://docs.localstack.cloud/aws/capabilities/security-testing/iam-policy-enforcement/) : how LocalStack evaluates IAM policies.
- [Explainable IAM](https://docs.localstack.cloud/aws/capabilities/security-testing/explainable-iam/) : the denial messages the analyzer reads.
- [IAM Policy Stream](https://docs.localstack.cloud/aws/capabilities/security-testing/iam-policy-stream/) : a live view of the policies your requests need.
- [LocalStack MCP server](https://github.com/localstack/localstack-mcp-server) : the server, its tools, and setup.
- [Sample app](https://github.com/localstack-samples/sample-iam-policy-analyzer-awsgoat) : the trimmed AWSGoat app used in this post.
- [LocalStack Slack Community](https://localstack.cloud/slack) : join for questions and discussion.
── more in #ai-agents 4 stories · sorted by recency
── more on @localstack 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/let-your-ai-agent-wr…] indexed:0 read:8min 2026-11-19 · —