IAM Users, Groups and Roles
AWS Identity and Access Management (IAM) controls who can do what on which resources in your account. It is a global service (not tied to a Region) and it is free.
Every API call to AWS is checked by IAM. Two questions are answered each time:
- Authentication: who is calling? (a signed request with credentials)
- Authorisation: is this identity allowed to perform this action on this resource? (policies)
The building blocks
| Thing | What it is | Credentials |
|---|---|---|
| Root user | The account owner, unlimited power | Email + password (+ MFA) |
| IAM user | A long-lived identity for one person or app | Password for the console, access keys for the API |
| IAM group | A collection of users; attach policies once, all members get them | None (groups cannot log in) |
| IAM role | An identity that is assumed temporarily by a user, a service or another account | Temporary credentials from STS |
| Policy | A JSON document that allows or denies actions | n/a |
Tip: For people, AWS recommends IAM Identity Center (single sign-on with temporary credentials) instead of creating IAM users. IAM users with access keys remain common in older setups and for some tools.
Creating them with the CLI
aws iam create-group --group-name developers
aws iam attach-group-policy --group-name developers \
--policy-arn arn:aws:iam::aws:policy/ReadOnlyAccess
aws iam create-user --user-name grace
aws iam add-user-to-group --user-name grace --group-name developers
aws iam list-groups-for-user --user-name grace --query "Groups[].GroupName"Sample output of the last command:
[
"developers"
]Model: effective permissions from groups
A user's permissions are the union of the policies attached to the user directly and to every group they belong to. Groups can't contain other groups.
groups = {
"developers": {"s3:GetObject", "s3:ListBucket", "logs:GetLogEvents"},
"deployers": {"lambda:UpdateFunctionCode", "cloudformation:CreateStack"},
"billing": {"ce:GetCostAndUsage"},
}
users = {
"grace": {"groups": ["developers"], "direct": set()},
"linus": {"groups": ["developers", "deployers"], "direct": {"s3:PutObject"}},
"ada": {"groups": ["billing"], "direct": set()},
}
def effective(user):
perms = set(users[user]["direct"])
for g in users[user]["groups"]:
perms |= groups[g]
return sorted(perms)
for name in users:
print(f"{name}: {', '.join(effective(name))}")grace: logs:GetLogEvents, s3:GetObject, s3:ListBucket
linus: cloudformation:CreateStack, lambda:UpdateFunctionCode, logs:GetLogEvents, s3:GetObject, s3:ListBucket, s3:PutObject
ada: ce:GetCostAndUsage
Managing permissions through groups is much easier than per user: when someone changes team you move them to another group instead of editing policies.
Roles and temporary credentials
A role has two policies:
- a trust policy: who may assume the role (a service like
ec2.amazonaws.com, a user, another account); - permission policies: what the role can do once assumed.
When a principal assumes a role, AWS STS (Security Token Service) hands out temporary credentials that expire, by default after one hour. Nothing long-lived is stored on the server: an EC2 instance with an attached role gets fresh credentials automatically.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}]
}aws sts assume-role --role-arn arn:aws:iam::123456789012:role/report-reader \
--role-session-name grace-sessionSample output (shortened):
{
"Credentials": {
"AccessKeyId": "ASIAEXAMPLETEMPKEY01",
"SecretAccessKey": "...",
"SessionToken": "...",
"Expiration": "2026-03-02T11:15:00+00:00"
},
"AssumedRoleUser": {
"Arn": "arn:aws:sts::123456789012:assumed-role/report-reader/grace-session"
}
}Model: assume a role with a trust policy
from datetime import datetime, timedelta
roles = {
"report-reader": {
"trusted": {"arn:aws:iam::123456789012:user/grace", "ec2.amazonaws.com"},
"permissions": {"s3:GetObject"},
},
}
def assume_role(caller, role_name, at, duration_minutes=60):
role = roles[role_name]
if caller not in role["trusted"]:
return f"AccessDenied: {caller} is not trusted by {role_name}"
expires = at + timedelta(minutes=duration_minutes)
return f"OK: temporary credentials for {role_name}, expire {expires:%Y-%m-%d %H:%M}"
start = datetime(2026, 3, 2, 10, 15)
print(assume_role("arn:aws:iam::123456789012:user/grace", "report-reader", start))
print(assume_role("ec2.amazonaws.com", "report-reader", start, duration_minutes=360))
print(assume_role("arn:aws:iam::123456789012:user/linus", "report-reader", start))OK: temporary credentials for report-reader, expire 2026-03-02 11:15
OK: temporary credentials for report-reader, expire 2026-03-02 16:15
AccessDenied: arn:aws:iam::123456789012:user/linus is not trusted by report-reader
Common mistakes
- Using the root user or one shared admin user for everything: no audit trail of who did what.
- Putting access keys on EC2 instances or in Lambda code instead of using a role.
- Forgetting that a role needs both a trust policy and permissions: if either is missing, it doesn't work.
Test yourself
Which IAM identity should an EC2 instance use to read from S3?
- The root user
- An IAM user with access keys copied to the instance
- An IAM role attached to the instance
- An IAM group
What does a role's trust policy define?
- Which actions the role can perform
- Who is allowed to assume the role
- The role's password
- Which Region the role works in
Can an IAM group contain another group?
- Yes, up to 10 levels
- No, groups contain only users
- Only in AWS Organizations
- Only for admins