Learn/Security
Security · 14 min read · August 2026
IAM from First Principles: Users, Roles, and Least Privilege
Identities, policies, and the failure modes that show up the first time a lab uses root keys. Built from the IAM User Guide.
Source. AWS Identity and Access Management User Guide · IAM policy evaluation logic · Security Pillar
Abstract & theoretical context
IAM answers who can do what on which resource under which conditions. Principals are users, roles, and federated identities. Permissions live in JSON policies attached to identities or resources. AWS documents a strict evaluation order: explicit Deny wins, then Allow, else implicit Deny. Root is the account owner, not a daily operator. Roles are assumed via STS and are how EC2, Lambda, and students should get credentials — never long-lived access keys in git. This reproduction walks the official primitives so chapter labs stop shipping AdministratorAccess on day one.
Models, formulas & variables
1. Policy evaluation
decision = Deny if any explicit Deny else Allow if any Allow else implicit Deny
- — SCPs, permission boundaries, and session policies can only reduce, not secretly expand, permissions.
- — Resource-based policies (S3 bucket policy) are evaluated together with identity policies.
2. Least privilege
scope(action, resource, condition) → minimal set that still completes the task
- — Start with a deny-by-default IAM user, then add s3:GetObject on one bucket ARN, not s3:*.
- — Prefer roles + instance profiles / Lambda execution roles over access keys.
Reference implementation
Copy into a sandbox account. Pin Region ap-south-1 unless the lab says otherwise.
lab-s3-read.json
IAM policy · JSON
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListOneBucket",
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": "arn:aws:s3:::tcet-sbg-lab"
},
{
"Sid": "ReadObjects",
"Effect": "Allow",
"Action": ["s3:GetObject"],
"Resource": "arn:aws:s3:::tcet-sbg-lab/*"
}
]
}Lab procedure
- 01Enable MFA on root, create an IAM admin user, and lock root credentials away.
- 02Create a role assumable by your IAM user with a trust policy and the JSON above.
- 03Run aws sts assume-role and confirm the session token works for GetObject only.
- 04Attach AdministratorAccess briefly, try a denied action with the scoped policy, then detach admin.
- 05Turn on CloudTrail in the lab account so every IAM change is attributable.
Key insights
- Access keys in .env files are an incident, not a shortcut.
- iam:PassRole is how privilege escalation hides in compute launch permissions.
- Condition keys (aws:SourceIp, s3:prefix) are how labs stay tight without writing a new policy per file.
Account & lab requirements
IAM permissions to create users/roles in a sandbox account. Never practice IAM on a production payer account.