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.
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
| Concept | What it is | Use for |
|---|---|---|
| Root user | The account's email login; can do everything, can't be restricted by IAM | Almost nothing — lock it with MFA |
| IAM user | A long-lived identity with a password and/or access keys | Legacy; prefer Identity Center for people and roles for machines |
| IAM group | A set of users sharing policies | Organising users (not a principal itself) |
| IAM role | An identity with no long-term credentials, assumed temporarily by someone or something trusted | Services (EC2, Lambda, ECS), cross-account access, federated people |
| Policy | A JSON document granting or denying permissions | Attached 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
{
"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:
AlloworDeny. - Action:
service:Operation, wildcards allowed (s3:Get*). Each API call maps to an action. - Resource: ARNs the statement applies to. Note
ListBucketacts on the bucket ARN,GetObjecton 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
- Attached to a user, group or role
- "This role may read bucket X"
- No
Principalelement - Managed (reusable, AWS- or customer-managed) or inline
- 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
Principalelement - Enables cross-account access without assuming a role
Roles and STS
A role has two policies:
- A trust policy (resource-based): who may assume it.
- Permission policies: what the role can do once assumed.
{
"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.
Where roles appear:
| Workload | Mechanism |
|---|---|
| EC2 | Instance profile → credentials via the instance metadata service (use IMDSv2) |
| Lambda | Execution role |
| ECS / Fargate | Task role (app permissions) and task execution role (pull images, write logs) |
| EKS pods | EKS Pod Identity or IRSA (IAM roles for service accounts) |
| GitHub Actions, GitLab CI | OIDC federation — the CI job assumes a role with a short-lived token; no stored keys (CI/CD) |
| People | IAM Identity Center with your identity provider (Okta, Entra ID, Google) |
| Another account | Cross-account role whose trust policy names the other account |
How a request is evaluated
- Authenticate the request (signature, valid credentials).
- Gather all applicable policies: SCPs from Organizations, resource control policies, the resource's policy, permission boundaries, session policies, and identity policies.
- Explicit Deny anywhere → denied. Nothing overrides a deny.
- Each guardrail layer that applies (SCP, permission boundary, session policy) must allow the action — they set the maximum, they never grant.
- An identity-based or (within the same account) resource-based policy must allow it.
- 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-1andeu-west-1; denycloudtrail: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:SourceVpceto require traffic from a VPC endpoint,aws:MultiFactorAuthPresentfor 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:
| Permission | Why it's dangerous |
|---|---|
iam:PassRole on * | Can hand any powerful role to a new EC2/Lambda and act through it |
iam:CreatePolicyVersion, iam:AttachRolePolicy | Can grant yourself anything |
sts:AssumeRole on * | Can become any role whose trust policy allows the account |
lambda:UpdateFunctionCode on a privileged function | Runs 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
ExternalIdcondition (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.
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.
- 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.