AWS IAM Explained: Users, Groups, Roles, Policies, Least Privilege and Policy Evaluation
What IAM Does
AWS Identity and Access Management (IAM) decides who (authentication) can do what (authorization) on which resources. IAM is global (not tied to a Region) and free.
| IAM piece | What it is | Example |
| User | A person or app with long-term credentials (password, access keys) | alice |
| Group | A set of users that share permissions. Groups cannot contain groups | Developers |
| Role | An identity with no password, used by AWS services or people for a short time (temporary credentials) | EC2-S3-ReadRole |
| Policy | A JSON document that allows or denies actions | AmazonS3ReadOnlyAccess |
A Policy Document
json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadReportsBucket",
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:ListBucket"],
"Resource": [
"arn:aws:s3:::acme-reports",
"arn:aws:s3:::acme-reports/*"
],
"Condition": {
"Bool": {"aws:SecureTransport": "true"}
}
}
]
}
| Element | Meaning |
Version | Policy language version. Always 2012-10-17 |
Statement | One or more rules |
Effect | Allow or Deny |
Action | API calls such as s3:GetObject, wildcards allowed (s3:*) |
Resource | ARN of the target. Bucket ARN for list, bucket/* for objects |
Condition | Extra rules: source IP, MFA, time, tags, HTTPS, Region |
Principal | Who is allowed. Used only in resource-based and trust policies |
ARNs
text
arn:aws:service:region:account-id:resource
arn:aws:s3:::my-bucket/path/file.txt (S3 has no Region or account in the ARN)
arn:aws:ec2:ap-south-1:123456789012:instance/i-0abc123
arn:aws:iam::123456789012:role/AppRole
Types of Policies
| Type | Attached to | Notes |
| Identity-based | User, group, role | Says what this identity may do. AWS managed, customer managed or inline |
| Resource-based | A resource (S3 bucket, SQS queue, KMS key, Lambda) | Has a Principal. Can allow cross-account access directly |
| Permission boundary | User or role | A maximum limit. Effective permissions are the overlap with the identity policy |
| Service control policy (SCP) | AWS Organizations account or OU | A guardrail for whole accounts. Does not grant, only limits |
| Session policy | Temporary session | Further limits an assumed-role session |
| Trust policy | A role | Says who may assume the role |
| Managed policy kind | Meaning |
| AWS managed | Created and updated by AWS, for example AdministratorAccess, ReadOnlyAccess |
| Customer managed | You write it, reuse it on many identities, and it has versions |
| Inline | Embedded in one identity. One-to-one, avoid unless you need that strict link |
Roles and Temporary Credentials
A role is like a hat: someone or something puts it on to get the permissions for a while. Credentials are temporary (issued by STS, the Security Token Service) and expire, so there is nothing long-lived to leak.
json
// Trust policy: EC2 instances may assume this role
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": {"Service": "ec2.amazonaws.com"},
"Action": "sts:AssumeRole"
}]
}
bash
# Create a role for EC2 that can read one S3 bucket
aws iam create-role --role-name AppS3Read --assume-role-policy-document file://trust-ec2.json
aws iam put-role-policy --role-name AppS3Read --policy-name ReadReports --policy-document file://read-reports.json
# EC2 uses roles through an instance profile
aws iam create-instance-profile --instance-profile-name AppS3Read
aws iam add-role-to-instance-profile --instance-profile-name AppS3Read --role-name AppS3Read
aws ec2 associate-iam-instance-profile --instance-id i-0abc123 --iam-instance-profile Name=AppS3Read
| Who uses roles | Example |
| AWS services | EC2 reading S3, Lambda writing to DynamoDB, ECS tasks |
| People in other accounts | A role in the prod account that a dev account user assumes |
| Federated users | Staff signing in with company SSO (SAML, OIDC) |
| Applications outside AWS | IAM Roles Anywhere, or OIDC from GitHub Actions |
bash
# Assume a role from the CLI
aws sts assume-role --role-arn arn:aws:iam::222222222222:role/ProdReadOnly --role-session-name alice-check
# returns AccessKeyId, SecretAccessKey, SessionToken, Expiratio
Rule of thumb: Never put access keys on an EC2 instance or in code. Attach a role. The SDK finds the temporary credentials automatically from the instance metadata service.
How AWS Decides: Policy Evaluation
- Everything is denied by default (implicit deny)
- An explicit Deny anywhere always wins
- An Allow is needed in the right places (identity policy, and resource policy for cross-account)
- SCPs and permission boundaries must also allow it
text
Request ─► explicit Deny anywhere? ─ yes ─► DENIED
│ no
â–¼
SCP (if Organizations) allows? ─ no ─► DENIED
â–¼ yes
Permission boundary allows? ─ no ─► DENIED
â–¼ yes
Identity or resource policy has an Allow? ─ no ─► DENIED (implicit)
â–¼ yes
ALLOWED
| Case | Result |
Policy A allows s3:*, policy B denies s3:DeleteObject | Delete is denied (explicit deny wins) |
| No policy mentions EC2 | EC2 actions are denied (implicit deny) |
| Same account: identity policy allows, bucket policy silent | Allowed (same account needs one allow) |
| Cross-account: identity policy allows but bucket policy of the other account does not | Denied: both sides must allow |
Useful Conditions
json
{
"Effect": "Deny",
"Action": "*",
"Resource": "*",
"Condition": {
"BoolIfExists": {"aws:MultiFactorAuthPresent": "false"}
}
}
json
{
"Effect": "Allow",
"Action": "ec2:*",
"Resource": "*",
"Condition": {
"StringEquals": {"aws:RequestedRegion": ["ap-south-1", "eu-west-1"]},
"IpAddress": {"aws:SourceIp": "203.0.113.0/24"}
}
}
| Condition key | Use |
aws:MultiFactorAuthPresent | Require MFA for sensitive actions |
aws:SourceIp, aws:SourceVpce | Limit by network or VPC endpoint |
aws:RequestedRegion | Allow only certain Regions |
aws:PrincipalTag, aws:ResourceTag | Attribute-based access control (ABAC): allow when tags match |
aws:PrincipalOrgID | Allow only principals in your AWS Organization |
aws:SecureTransport | Require HTTPS |
Users, Passwords and Keys
- Set a password policy (length, rotation if required) and MFA for every human user
- Use groups to attach permissions to many users at once
- Access keys: create only when needed, two per user at most, rotate, delete unused. Check the credential report
- For humans prefer IAM Identity Center (single sign-on) over many IAM users
- Use IAM Access Analyzer to find resources shared outside your account and to generate least-privilege policies from CloudTrail activity
bash
aws iam generate-credential-report && aws iam get-credential-report --query Content --output text | base64 -d | head
aws iam list-access-keys --user-name alice
aws iam get-account-password-policy
aws accessanalyzer list-analyzers
Least Privilege in Practice
- Start with no access, add what is needed
- Use AWS managed policies to start, then replace with narrow custom policies
- Scope by resource ARN and condition, not just action
- Review with Access Analyzer and the last accessed information in IAM, remove unused permissions
- Separate duties: developers, admins, auditors
- Give break-glass admin access only through an audited, MFA-protected role
Avoid: "Action": "*", "Resource": "*" for anything except a temporary lab. It is the same as handing over the whole account.
Practice Tasks
- Create a group
Developers, attach ReadOnlyAccess, add a user, and test that the user cannot create an EC2 instance
- Write a policy that lets a role read only the prefix
reports/ of one bucket
- Create a role for EC2 with S3 read access and attach it to an instance, then run
aws s3 ls on the instance without keys
- Write a policy that denies everything unless MFA is present
- Create a role in a second account and assume it with
aws sts assume-role
Conclusion
IAM controls access with users, groups, roles and policies. Everything is denied by default, an explicit deny always wins, and roles give temporary credentials so no keys are stored. Use least privilege, MFA, conditions, Access Analyzer and roles for every AWS service and application.
Quick Summary: users/groups/roles/policies; JSON structure + ARNs; policy types; roles + trust policy + instance profile; evaluation order; conditions (MFA, Region, tags); keys + credential report; least privilege.