{"slug": "let-your-ai-agent-write-least-privilege-iam-policies", "title": "Let Your AI Agent Write Least-Privilege IAM Policies", "summary": "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.", "body_md": "## \n\nIAM 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.\n\nWriting 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.\n\nWe wanted to see whether an AI agent could do this work. We gave [Claude Code](https://www.claude.com/product/claude-code) access to the [LocalStack MCP server](https://github.com/localstack/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.\n\nThe 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.\n\n## \n\nBy default, LocalStack accepts AWS requests without checking IAM. This keeps local development fast, but it also hides broken or overly broad policies.\n\n[IAM policy enforcement](https://docs.localstack.cloud/aws/capabilities/security-testing/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`.\n\nThis 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.\n\nEnforcement has three modes:\n\n- `DISABLED` is the default. LocalStack allows every request.\n- `SOFT_MODE` evaluates every request and logs the ones that would be denied, but still lets them through.\n- `ENFORCED` denies requests that the policies do not allow.\n\nEach denial is written to the LocalStack logs with the principal, the action, and the resource:\n\nThe LocalStack MCP server provides a `localstack-iam-policy-analyzer` tool with three actions:\n\n- `set-mode` changes the mode without restarting LocalStack.\n- `get-status` returns the current mode.\n- `analyze-policies` reads recent logs, removes duplicate denials, and creates a policy that would allow those requests. It groups actions by resource.\n\nThe agent also uses `localstack-management` to start LocalStack, `localstack-deployer` to apply the Terraform stack, and `localstack-aws-client` to run AWS CLI commands.\n\n## \n\n- [Docker](https://docs.docker.com/get-docker/) installed and running.\n- A `LOCALSTACK_AUTH_TOKEN` from a[LocalStack account](https://app.localstack.cloud/sign-up) . IAM policy enforcement is included in the Base and Ultimate plans.\n- [Node.js 22+](https://nodejs.org/) to run the MCP server with`npx` .\n- [Terraform](https://developer.hashicorp.com/terraform/install) and[`tflocal`](https://github.com/localstack/terraform-local) (` pip install terraform-local` ). The deployer tool uses them to apply the stack.\n- Python 3 and `curl` for the sample app’s test script.\n- [Claude Code](https://www.claude.com/product/claude-code) or another MCP client.\n\nThe Lambda layer uses `x86_64`. Docker Desktop runs it through emulation on Apple Silicon without extra setup.\n\n## \n\nThe MCP server comes with a wizard that writes the client configuration for you:\n\nThe 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.\n\nYou do not need to configure anything for IAM enforcement. The agent switches modes with the analyzer tool after LocalStack starts.\n\n## \n\nThe sample app is a smaller version of the serverless blog module from [AWSGoat](https://github.com/ine-labs/AWSGoat), an intentionally vulnerable AWS project from INE.\n\nA 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.\n\nClone the repository:\n\nWe 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:\n\nThe 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.\n\nTerraform 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.\n\n## \n\nOpen Claude Code in the repository folder. We used Claude Opus 5.5. This prompt gives the agent a goal and a clear sequence:\n\nStep 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.\n\n## \n\nThe agent found LocalStack stopped, started it, and deployed the stack:\n\nThe 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:\n\nAll eight checks passed. However, this baseline does not validate the policy because IAM enforcement was disabled.\n\n## \n\n### \n\nThe 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:\n\nThe 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.\n\n### \n\nNext, the agent called the analyzer:\n\nThe analyzer reduced 12 denials to six unique permissions. All came from the function’s execution role. It generated one policy statement for each resource:\n\nThis is much narrower than the original policy. However, it only covers the requests that the test script made.\n\n## \n\nThe agent reviewed the generated policy and found three gaps.\n\nFirst, 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/*`.\n\nSecond, 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.\n\nThird, 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.\n\nThe final policy:\n\nEach 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.\n\n## \n\nThe agent applied the reviewed policy, switched to `ENFORCED`, and ran the routes again:\n\nAll 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.\n\n## \n\nPassing 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:\n\nThe 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:\n\nIt restored the least-privilege policy and ran the same probe again:\n\nThe 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.\n\n## \n\nThe full process took less than eight minutes and did not need an AWS account:\n\n| Metric | Value | \n|---|---|\n| Model | Claude Opus 5.5 | \n| Cost | $1.91 | \n| Time | 7m 39s | \n| Turns | 44 | \n| Tool calls | 43 | \n\nThe 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.\n\nYou can use the same process for your own stacks:\n\n1. Deploy the stack to LocalStack.\n2. Reduce the policy.\n3. Run your tests in soft mode.\n4. Generate and review a policy.\n5. Verify it with enforcement enabled.\n\nReview is essential. A generated policy only reflects the traffic you ran. You must still test or inspect any code paths that were not exercised.\n\n## \n\n- [IAM Policy Enforcement](https://docs.localstack.cloud/aws/capabilities/security-testing/iam-policy-enforcement/) : how LocalStack evaluates IAM policies.\n- [Explainable IAM](https://docs.localstack.cloud/aws/capabilities/security-testing/explainable-iam/) : the denial messages the analyzer reads.\n- [IAM Policy Stream](https://docs.localstack.cloud/aws/capabilities/security-testing/iam-policy-stream/) : a live view of the policies your requests need.\n- [LocalStack MCP server](https://github.com/localstack/localstack-mcp-server) : the server, its tools, and setup.\n- [Sample app](https://github.com/localstack-samples/sample-iam-policy-analyzer-awsgoat) : the trimmed AWSGoat app used in this post.\n- [LocalStack Slack Community](https://localstack.cloud/slack) : join for questions and discussion.", "url": "https://wpnews.pro/news/let-your-ai-agent-write-least-privilege-iam-policies", "canonical_source": "https://blog.localstack.cloud/let-your-ai-agent-write-least-privilege-iam-policies/", "published_at": "2026-11-19 00:00:00+00:00", "updated_at": "2026-11-19 14:13:57.947342+00:00", "lang": "en", "topics": ["ai-agents", "ai-tools", "developer-tools", "ai-safety"], "entities": ["LocalStack", "Claude Code", "LocalStack MCP server", "Anthropic", "AWS", "Terraform", "Docker", "IAM Policy Analyzer"], "also_reported_by": [], "takeaway": "Run LocalStack IAM enforcement in SOFT_MODE or ENFORCED mode so denials are logged with principal, action and resource, then let the localstack-iam-policy-analyzer's analyze-policies action deduplicate those denials into a resource-grouped least-privilege policy instead of hand-mapping each API call", "recommendation": {"advice": "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.", "playbook": "agent-security", "playbook_url": "https://wpnews.pro/playbooks/agent-security"}, "alternates": {"html": "https://wpnews.pro/news/let-your-ai-agent-write-least-privilege-iam-policies", "markdown": "https://wpnews.pro/news/let-your-ai-agent-write-least-privilege-iam-policies.md", "text": "https://wpnews.pro/news/let-your-ai-agent-write-least-privilege-iam-policies.txt", "jsonld": "https://wpnews.pro/news/let-your-ai-agent-write-least-privilege-iam-policies.jsonld"}}