Part 1 of the Core Foundations series.

Every call to AWS, whether a click in the console, a CLI command or an SDK request from a Lambda function, is an API request, and every API request is checked by IAM. If you understand how that check works, most “Access Denied” errors stop being mysterious.

The mental model

An IAM decision answers one question:

Can this principal perform this action on this resource, under these conditions?

  • Principal: who is making the request. An IAM user, an IAM role session, the account root user, or an AWS service acting on your behalf.
  • Action: the API operation, written as service:Operation, for example s3:GetObject or ec2:StartInstances.
  • Resource: what the action is performed on, identified by an ARN such as arn:aws:s3:::my-bucket/photos/cat.jpg.
  • Conditions: extra context such as source IP, time, whether MFA was used, or tags on the resource.

Identities

The root user is the email address you signed up with. It can do anything, including closing the account, and can’t be restricted by IAM policies. Protect it with MFA, don’t create access keys for it, and put it away. You’ll rarely need it.

IAM users are long-lived identities with a password and/or access keys. They were the original way to give people and programs access, and they’re now the option of last resort. Long-lived access keys leak: into Git repositories, CI logs and laptops.

IAM roles are identities with no permanent credentials. A trusted principal assumes the role and receives temporary credentials from AWS STS that expire automatically (after an hour by default). Roles are how:

  • EC2 instances, Lambda functions and ECS tasks get permissions (no keys baked into code),
  • people sign in, via IAM Identity Center,
  • one account accesses another, and
  • external systems such as GitHub Actions deploy to AWS using OIDC federation.

A good rule of thumb: humans use IAM Identity Center, workloads use roles, and IAM users are the exception you can justify.

Policies

Permissions are granted with JSON policies. This one allows reading objects from one bucket:

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::my-bucket/*"
    }
  ]
}

The policy types you’ll meet most often:

  • Identity-based policies attach to a user, group or role and say what that identity can do.
  • Resource-based policies attach to a resource, such as an S3 bucket policy or a role’s trust policy, and say who can access it. They have a Principal element.
  • Permissions boundaries and service control policies (SCPs) set maximum permissions. They never grant anything by themselves; they only limit what other policies can grant.

How AWS evaluates a request

Within a single account, the logic boils down to three rules:

  1. Everything is denied by default (an implicit deny).
  2. An Allow in an applicable policy overrides the default.
  3. An explicit Deny in any applicable policy overrides every Allow.

So “Access Denied” means one of two things: nothing allowed the request, or something explicitly denied it. The error message often tells you which policy type was involved, and IAM Access Analyzer and the policy simulator help you find out why.

Roles have two policies

This trips up nearly everyone at first. A role has:

  • a trust policy, which says who may assume the role, and
  • permissions policies, which say what the role can do once assumed.

If the trust policy doesn’t match, it doesn’t matter what the permissions policies say: you’ll get Not authorized to perform sts:AssumeRoleWithWebIdentity (or sts:AssumeRole) before any permissions are checked.

Hands-on: a deploy role for GitHub Actions

Here’s a real example from this site. Its content is deployed by GitHub Actions, using a role with no stored AWS keys. GitHub issues the workflow a short-lived OIDC token, and AWS exchanges it for temporary credentials.

The trust policy only accepts tokens from one repository’s prod environment:

AssumeRolePolicyDocument:
  Version: 2012-10-17
  Statement:
    - Effect: Allow
      Principal:
        Federated: !Sub arn:aws:iam::${AWS::AccountId}:oidc-provider/token.actions.githubusercontent.com
      Action: sts:AssumeRoleWithWebIdentity
      Condition:
        StringEquals:
          token.actions.githubusercontent.com:aud: sts.amazonaws.com
          token.actions.githubusercontent.com:sub: repo:my-org/my-repo:environment:prod

The permissions policy grants only what the deploy needs: list and write the website bucket, and invalidate the CloudFront distribution. Nothing else.

Two lessons I learned the hard way setting this up:

  • The OIDC identity provider must exist in the account first. Without it, the role assumption fails before the trust policy is even evaluated.
  • Check the exact sub claim your token sends. Repositories can customise it, and newer repositories may use an “immutable” form that includes numeric owner and repository IDs (repo:my-org@123/my-repo@456:environment:prod). If the string doesn’t match exactly, the StringEquals condition fails.

Gotchas

  • IAM is eventually consistent. A newly created role or policy can take a few seconds to take effect everywhere. Retries usually fix “it works on the second try”.
  • Avoid "Action": "*" and "Resource": "*" outside of break-glass admin roles. Start narrow and widen when something is denied, rather than the reverse.
  • AWS managed policies are convenient but broad. Fine for learning, but write customer managed policies for production workloads.
  • IAM itself is free. There’s no cost reason to skimp on separate, well-scoped roles.

Checklist

  • MFA on the root user, and no root access keys
  • Day-to-day human access through IAM Identity Center
  • Workloads use roles, never embedded access keys
  • CI/CD uses OIDC federation instead of stored secrets
  • Least privilege: specific actions on specific resources

Next: VPC: your private network in the cloud.

← Series overview