AI agent IAM: IAM for Agents: Roles, Not God Keys

Day 63: IAM for Agents: Roles, Not God Keys
8 views
This guide focuses on AI agent IAM for production systems, with practical trade-offs for reliability, security, and cost. God keys in .env for “the bot” are how you get a very polite breach. Agents need per-tool IAM roles, short-lived credentials, and Access Analyzer on any policy an agent proposes.
⚡ TL;DR: One role per tool family. STS AssumeRole with session tags (run_id). Never embed long-lived keys in prompts. Analyze generated policies before apply.

AI agent IAM: production guidance

hcl
# terraform/agent_roles.tf
resource "aws_iam_role" "agent_s3_read" {
  name = "agent-s3-read"
  assume_role_policy = data.aws_iam_policy_document.agent_trust.json
}

resource "aws_iam_role" "agent_sandbox_apply" {
  name = "agent-sandbox-apply"
  assume_role_policy = data.aws_iam_policy_document.agent_trust.json
}
python
# creds.py
import boto3
sts = boto3.client("sts")

def session_for(tool_family: str, run_id: str):
    role = ROLES[tool_family]
    out = sts.assume_role(
        RoleArn=role,
        RoleSessionName=f"agent-{run_id}"[:64],
        DurationSeconds=900,
        Tags=[{"Key": "run_id", "Value": run_id}],
    )
    c = out["Credentials"]
    return boto3.Session(
        aws_access_key_id=c["AccessKeyId"],
        aws_secret_access_key=c["SecretAccessKey"],
        aws_session_token=c["SessionToken"],
    )

Access Analyzer on generated policies

bash
aws accessanalyzer validate-policy \
  --policy-document file://proposed.json \
  --policy-type IDENTITY_POLICY
❌ Applying model-written IAM JSON because “it looks least-privilege” — validate and human-review.

Closing checklist

  • [ ] Per-tool roles + STS
  • [ ] Session tags with run_id
  • [ ] Validate policies before apply
  • [ ] Short durations (≤15m)
  • [ ] CloudTrail on AssumeRole

Series navigation

Day 62: SSRF and Tool Allowlists · Day 64: Secret-Aware Context Filters Last updated September 11, 2026 Related: IAM for AI Agents: Condition Keys That Limit Blast Radius by Tag; Day 51: Multi-Agent Roles: Planner, Implementer, Critic; Day 76: p99 of Agents: Queueing, Not Just Model Latency.

Watch: 100 Days of AI on YouTube

Short videos from CheatCoders, one AI engineering topic per day.

Open the playlist on YouTube · CheatCoders on YouTube

Deep-dive PDF

Get the expanded guide for this post — extra diagrams-style checklists, failure modes, and a production walkthrough. Free when you subscribe to CheatCoders.

Already subscribed? or open the subscribe page.


Discover more from CheatCoders

Subscribe to get the latest posts sent to your email.

Comments

No comments yet. Why don’t you start the discussion?

Leave a comment

No account needed. Name and email are optional.