Difficulty: Intermediate
Explain the difference between an IAM User, an IAM Role and an IAM Policy. When would you use a role instead of a user?
An IAM user is a long-lived identity with permanent credentials (a password and/or access keys). An IAM role is an identity with no permanent credentials that a trusted principal, such as an EC2 instance, a Lambda function or a user from another account, assumes to get temporary credentials. An IAM policy is a JSON document that grants or denies permissions; it does nothing on its own until it is attached to a user, group, role or resource.
Think of it as "who" and "what":
Effect (Allow or Deny), Action (like s3:GetObject), Resource (an ARN) and optional Condition.A user with no policy attached can authenticate but cannot do anything, because IAM denies everything by default.
A role has two policies that people often confuse:
ec2.amazonaws.com or a specific account).When a principal calls sts:AssumeRole (or the service does it for you, as EC2 does through an instance profile), AWS STS returns an access key, secret key and session token that expire. The default session is 1 hour and a role's maximum session duration can be set between 1 and 12 hours. Nothing long-lived is stored on the server, and the SDK refreshes credentials automatically.
| Situation | Use |
|---|---|
| Code on EC2, ECS, EKS or Lambda calling AWS APIs | Role (instance profile, task role, IRSA/Pod Identity, execution role) |
| Engineers signing in to the console | IAM Identity Center with SSO, which issues role sessions |
| Cross-account access (dev account reading prod S3) | Role in the target account with a trust policy |
| GitHub Actions deploying to AWS | Role assumed through OIDC federation |
| Legacy tool that only accepts static keys | IAM user, as a last resort, with tightly scoped policy |
In 2026 the guidance is simple: humans use federation, workloads use roles, and IAM users with access keys are the exception.
Principal.Evaluation order: an explicit Deny anywhere wins, then an Allow is required from the applicable policies, otherwise the result is an implicit deny.
.env files on EC2 instead of attaching a role.AdministratorAccess "temporarily" and never removing it.{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": { "Service": "ec2.amazonaws.com" },
"Action": "sts:AssumeRole"
}
]
}
This answers "who can become this role". Without it, no one can assume the role regardless of its permissions.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ReadInvoices",
"Effect": "Allow",
"Action": ["s3:GetObject"],
"Resource": "arn:aws:s3:::acme-invoices/2026/*"
},
{
"Sid": "ListOnlyThatPrefix",
"Effect": "Allow",
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::acme-invoices",
"Condition": { "StringLike": { "s3:prefix": "2026/*" } }
}
]
}
This answers "what can the role do". Note that ListBucket applies to the bucket ARN while GetObject applies to object ARNs.
aws sts assume-role \
--role-arn arn:aws:iam::222233334444:role/ReadOnlyAuditor \
--role-session-name sachin-audit \
--duration-seconds 3600
# Or let the CLI handle it via ~/.aws/config
# [profile prod-audit]
# role_arn = arn:aws:iam::222233334444:role/ReadOnlyAuditor
# source_profile = default
aws s3 ls --profile prod-audit
The response contains AccessKeyId, SecretAccessKey, SessionToken and Expiration; a named profile makes the CLI assume and refresh the role for you.
IAM User, IAM Role, IAM Policy, Trust policy, STS AssumeRole, Policy evaluation