IAM — Identity & Access Management

How AWS decides whether a request is allowed — principals, users, groups and roles, identity vs resource policies, policy JSON, the evaluation logic, STS and temporary credentials, assuming roles across accounts, instance and task roles, permission boundaries, SCPs, least privilege and common mistakes.

Intermediate 7 min readLesson 2 of 19 Interview Q&A

IAM answers one question for every single AWS API call: is this principal allowed to perform this action on this resource, under these conditions? Almost every AWS security incident — leaked keys, public data, privilege escalation — comes down to IAM. It's the most important AWS service to understand properly.

The building blocks

ConceptWhat it isUse for
Root userThe account's email login; can do everything, can't be restricted by IAMAlmost nothing — lock it with MFA
IAM userA long-lived identity with a password and/or access keysLegacy; prefer Identity Center for people and roles for machines
IAM groupA set of users sharing policiesOrganising users (not a principal itself)
IAM roleAn identity with no long-term credentials, assumed temporarily by someone or something trustedServices (EC2, Lambda, ECS), cross-account access, federated people
PolicyA JSON document granting or denying permissionsAttached to identities or resources

The single most important habit: use roles and temporary credentials, not access keys. Long-lived access keys leak — into Git repos, CI logs, laptops — and bots scan GitHub for them within minutes.

Policy anatomy

JSON
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "ReadInvoices",
      "Effect": "Allow",
      "Action": ["s3:GetObject", "s3:ListBucket"],
      "Resource": [
        "arn:aws:s3:::invoices-prod",
        "arn:aws:s3:::invoices-prod/*"
      ],
      "Condition": {
        "StringEquals": { "aws:PrincipalTag/team": "billing" }
      }
    },
    {
      "Effect": "Deny",
      "Action": "s3:DeleteObject",
      "Resource": "arn:aws:s3:::invoices-prod/*"
    }
  ]
}
  • Effect: Allow or Deny.
  • Action: service:Operation, wildcards allowed (s3:Get*). Each API call maps to an action.
  • Resource: ARNs the statement applies to. Note ListBucket acts on the bucket ARN, GetObject on the object ARN — a classic source of "access denied".
  • Condition: optional context checks — source IP, VPC endpoint, MFA present, tags, time, encryption headers.
  • Principal: appears only in resource-based policies (who is being granted access).

Identity-based vs resource-based policies

Identity-based policy
  • Attached to a user, group or role
  • "This role may read bucket X"
  • No Principal element
  • Managed (reusable, AWS- or customer-managed) or inline
Resource-based policy
  • Attached to the resource: S3 bucket policy, SQS queue policy, KMS key policy, Lambda resource policy, role trust policy
  • "Account 222… may read this bucket"
  • Has a Principal element
  • Enables cross-account access without assuming a role

Roles and STS

A role has two policies:

  1. A trust policy (resource-based): who may assume it.
  2. Permission policies: what the role can do once assumed.
JSON
{
  "Version": "2012-10-17",
  "Statement": [{
    "Effect": "Allow",
    "Principal": { "Service": "lambda.amazonaws.com" },
    "Action": "sts:AssumeRole"
  }]
}

Assuming a role calls the Security Token Service (STS), which returns temporary credentials — an access key, secret key and session token valid for 15 minutes to 12 hours — that expire on their own.

EC2 app (SDK)Instance metadataSTSS3GET credentials (IMDSv2, with token)AssumeRole for instance profiletemp keys, expire in ~6 htemp keysGetObject signed with SigV4200 OK (IAM check passed)

Where roles appear:

WorkloadMechanism
EC2Instance profile → credentials via the instance metadata service (use IMDSv2)
LambdaExecution role
ECS / FargateTask role (app permissions) and task execution role (pull images, write logs)
EKS podsEKS Pod Identity or IRSA (IAM roles for service accounts)
GitHub Actions, GitLab CIOIDC federation — the CI job assumes a role with a short-lived token; no stored keys (CI/CD)
PeopleIAM Identity Center with your identity provider (Okta, Entra ID, Google)
Another accountCross-account role whose trust policy names the other account

How a request is evaluated

  1. Authenticate the request (signature, valid credentials).
  2. Gather all applicable policies: SCPs from Organizations, resource control policies, the resource's policy, permission boundaries, session policies, and identity policies.
  3. Explicit Deny anywhere → denied. Nothing overrides a deny.
  4. Each guardrail layer that applies (SCP, permission boundary, session policy) must allow the action — they set the maximum, they never grant.
  5. An identity-based or (within the same account) resource-based policy must allow it.
  6. Otherwise → implicit deny (the default).

Cross-account requests need both sides: the caller's identity policy must allow the action and the resource policy (or the target role's trust policy) must allow the caller.

Guardrails

  • Service Control Policies: set the maximum permissions for every principal in an account (except the management account) — e.g. deny all regions except ap-south-1 and eu-west-1; deny cloudtrail:StopLogging.
  • Permission boundaries: cap what a role or user can ever be granted — lets you delegate "developers may create roles" safely, because the roles they create can't exceed the boundary.
  • Session policies: further restrict a single assumed-role session.

Least privilege in practice

  • Start from AWS managed policies only for exploration; production roles get customer-managed policies scoped to specific actions and resource ARNs.
  • One role per workload, not one shared "app role".
  • Use conditions: aws:SourceVpce to require traffic from a VPC endpoint, aws:MultiFactorAuthPresent for sensitive operations, tags for attribute-based access control (ABAC).
  • IAM Access Analyzer flags resources shared outside your account and can generate policies from CloudTrail activity — run the workload, then tighten to what it actually used.
  • Review last-accessed information and remove unused permissions and roles.

The dangerous permissions

Some permissions are effectively admin:

PermissionWhy it's dangerous
iam:PassRole on *Can hand any powerful role to a new EC2/Lambda and act through it
iam:CreatePolicyVersion, iam:AttachRolePolicyCan grant yourself anything
sts:AssumeRole on *Can become any role whose trust policy allows the account
lambda:UpdateFunctionCode on a privileged functionRuns your code with that function's role

Scope these tightly — they're the standard privilege-escalation paths in cloud pentests.

Common mistakes

  • Access keys committed to Git or baked into AMIs and container images.
  • "Action": "*", "Resource": "*" on application roles.
  • Using the root user day to day; no MFA.
  • S3 bucket policies with "Principal": "*" (keep Block Public Access on — S3).
  • Trust policies that allow an entire external account without an ExternalId condition (the "confused deputy" problem for third-party integrations).
  • Debugging by adding permissions until it works — instead, read the error (it names the action and resource) and check CloudTrail.
Interview questions
QWhat's the difference between an IAM user and an IAM role?easy

A user is a long-lived identity with permanent credentials — a password and possibly access keys. A role has no permanent credentials: a trusted principal (a service like EC2 or Lambda, a user, another account, or a federated identity) assumes it and receives temporary credentials from STS that expire automatically. Roles are preferred for workloads and people because there are no long-lived secrets to leak or rotate.

QHow does IAM evaluate a request when multiple policies apply?medium

Everything starts as an implicit deny. If any applicable policy has an explicit Deny for the action, the request is denied. Guardrails — SCPs, permission boundaries and session policies — must each allow the action because they define the maximum available permissions. Then an identity-based or resource-based policy must explicitly allow it. For cross-account access, both the caller's account and the resource side must allow it.

QHow would you let an application running on EC2 read from an S3 bucket?easy

Create an IAM role with a trust policy for ec2.amazonaws.com and a permission policy allowing s3:GetObject on that bucket's object ARN (and s3:ListBucket on the bucket ARN if it needs to list). Attach it to the instance through an instance profile. The SDK automatically fetches temporary credentials from the instance metadata service — no access keys in code or config. Enforce IMDSv2 to protect those credentials against SSRF.

QWhat is iam:PassRole and why does it matter?hard

PassRole is the permission to give a role to an AWS service — for example, launching an EC2 instance or creating a Lambda function that runs as a particular role. Without restrictions, someone who can create functions and pass any role can pass an admin role to their own function and escalate privileges. Restrict PassRole to specific role ARNs and use the iam:PassedToService condition.

Key takeaways
  • Every AWS API call is authorised by IAM: principal, action, resource and conditions.
  • Prefer roles with temporary STS credentials over IAM users with access keys; use Identity Center for people and OIDC for CI.
  • Identity-based policies attach to principals; resource-based policies attach to resources and name a Principal; roles have a trust policy plus permission policies.
  • Evaluation: explicit deny wins, guardrails (SCPs, boundaries) cap permissions, then an allow is required — default is deny.
  • Apply least privilege with scoped actions and ARNs, conditions, Access Analyzer and last-accessed data.
  • Guard privilege-escalation permissions like iam:PassRole and policy editing.