IAM Roles vs. Policies

Difficulty: Intermediate

Question

Explain the difference between an IAM User, an IAM Role and an IAM Policy. When would you use a role instead of a user?

Answer

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.

Identities vs permissions

Think of it as "who" and "what":

  • Who (principals): users, groups (a collection of users, not a principal you can sign in as), and roles.
  • What (policies): a list of statements, each with 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.

How a role actually works

A role has two policies that people often confuse:

  1. Trust policy: who is allowed to assume the role (for example ec2.amazonaws.com or a specific account).
  2. Permissions policy: what the role can do once assumed.

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.

When to use which

SituationUse
Code on EC2, ECS, EKS or Lambda calling AWS APIsRole (instance profile, task role, IRSA/Pod Identity, execution role)
Engineers signing in to the consoleIAM 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 AWSRole assumed through OIDC federation
Legacy tool that only accepts static keysIAM 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.

Policy types and evaluation

  • Identity-based policies attach to users, groups and roles. They can be AWS managed, customer managed or inline.
  • Resource-based policies attach to resources like S3 buckets, SQS queues and KMS keys, and name a Principal.
  • Guardrails: permissions boundaries, Service Control Policies (SCPs), Resource Control Policies (RCPs) and session policies limit the maximum permissions but never grant anything.

Evaluation order: an explicit Deny anywhere wins, then an Allow is required from the applicable policies, otherwise the result is an implicit deny.

Common mistakes

  • Putting access keys in code or .env files on EC2 instead of attaching a role.
  • Attaching AdministratorAccess "temporarily" and never removing it.
  • Writing a permissions policy for a role but forgetting the trust policy, which gives an "is not authorized to perform: sts:AssumeRole" error.
How an EC2 instance gets temporary credentials from a role. The application never sees a long-lived key; the SDK fetches and refreshes short-lived credentials.
  1. App on EC2 → Instance metadata: Request role credentials (IMDSv2)
  2. Instance metadata → AWS STS: Assume role via instance profile
  3. AWS STS → Instance metadata: Temporary key, secret, session token
  4. Instance metadata → App on EC2: Return credentials with expiry
  5. App on EC2 → Amazon S3: Signed GetObject request
  6. Amazon S3 → App on EC2: Allowed by role permissions policy

Code examples

Trust policy: let EC2 assume the role

{
  "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.

Permissions policy: read one S3 prefix

{
  "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.

Assume a cross-account role from the CLI

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.

Key points

  • User = permanent identity; Role = identity assumed for temporary credentials; Policy = JSON permissions document.
  • A role needs both a trust policy (who can assume) and a permissions policy (what it can do).
  • Explicit Deny beats any Allow; anything not allowed is implicitly denied.
  • SCPs, RCPs and permissions boundaries limit permissions but never grant them.
  • Prefer roles for workloads and IAM Identity Center for humans over IAM users with access keys.

Common follow-up questions

  • How does IAM evaluate a request when identity and resource policies disagree?
  • What is a permissions boundary and how is it different from an SCP?
  • How would you let GitHub Actions deploy to AWS without storing access keys?
  • What is the difference between an instance profile and a role?

Interview tips

  • Mention the trust policy explicitly; many candidates only describe the permissions side of a role.
  • Use the "who vs what" framing to keep the answer crisp before going deeper.
  • Bring up a real scenario like replacing hard-coded keys on EC2 with a role; it signals production experience.

Concepts covered

IAM User, IAM Role, IAM Policy, Trust policy, STS AssumeRole, Policy evaluation

More AWS & Cloud Fundamentals questions