IAM users, roles, policies & policy evaluation
Write least-privilege policies and predict whether a request is allowed.
By Invicly Tech, , in AWS Solutions Architect – Associate. About 12 minutes to read.
In short
IAM controls who can do what in AWS. Users and groups represent people, while roles give temporary credentials to services, apps and other accounts. Every request is evaluated against all applicable policies, and an explicit deny always wins.
Think of it like this: A role is a visitor badge: you pick it up, use it for a while, then it expires.
Explained simply
IAM (Identity and Access Management) answers two questions for every request to AWS: who is asking, and are they allowed to do this? Every click in the console and every CLI or SDK call is an API request, and IAM checks each one. If nothing says "allow", the answer is no.
There are three kinds of identity. A user is one person or app with a permanent password or access key. A group is a bundle of users so you can give the same permissions to many people at once. A role has no permanent password at all: someone or something borrows it for a while and gets short-lived keys.
Permissions are written as policies: small JSON documents that list actions (like s3:GetObject), resources (like one bucket) and optional conditions (like "only over HTTPS"). You attach a policy to an identity, or to a resource such as a bucket, which then says who may use it.
Least privilege means giving only the permissions a job really needs. It feels slower at first, but it limits the damage when a key leaks or an app has a bug. Most real AWS breaches start with credentials that had far more access than they needed.
Press Play on a diagram to walk through it one step at a time.
- 1Explicit deny?
Any matching Deny in any policy ends with deny.
- 2SCP and RCP allow?
Organization policies must allow the action.
- 3Resource policy allow?
Can grant access on its own within the account.
- 4Identity policy allow?
Needed if no resource policy grants access.
- 5Boundary and session allow?
Permissions boundaries and session policies further limit.
- 6Otherwise implicit deny
No allow found means the request is denied.
- EC2 app to Instance metadata: Request role credentials1Request role credentials
- Instance metadata to AWS STS: AssumeRole via instance profile2AssumeRole via instance profile
- AWS STS to Instance metadata: Temporary keys and token3Temporary keys and token
- Instance metadata to EC2 app: Return temporary credentials4Return temporary credentials
- EC2 app to Amazon S3: Signed GetObject request5Signed GetObject request
- Amazon S3 to EC2 app: Object returned if allowed6Object returned if allowed
- Identity-based
- AWS managed
- Customer managed
- Inline
- Resource-based
- S3 bucket policy
- Role trust policy
- KMS key policy
- SQS queue policy
- Guardrails (limit only)
- SCP
- RCP
- Permissions boundary
- Session policy
- Passed during AssumeRole
- Narrows one session
In depth
Users, groups and roles: when to use each
Use IAM users only when you must: break-glass access, or an old tool that cannot use roles. Their access keys never expire on their own, so they leak easily. For people, prefer IAM Identity Center, which issues role sessions behind the scenes.
Groups are just a way to attach policies to many users. They cannot be nested, and a group cannot be named as a principal in a resource policy.
Roles are the workhorse. EC2 instances, Lambda functions, ECS tasks, other AWS accounts and federated users all assume roles. A role session lasts one hour by default, and the role's maximum session duration can be set from 1 to 12 hours. When one role assumes another (role chaining), the session is limited to one hour.
- Long-term password or keys
- One person or legacy app
- Keys must be rotated by you
- Avoid for daily human access
- Collection of users
- Attach policies once
- Cannot be nested
- Not a principal itself
- No long-term credentials
- Assumed via AWS STS
- 1 hour default session
- Used by services and accounts
Anatomy of a policy
Every policy has a Version, always "2012-10-17", and one or more statements. Each statement has an Effect (Allow or Deny), an Action list, a Resource list, and optionally a Condition. Resource-based policies, such as bucket policies and role trust policies, also have a Principal that names who the statement applies to.
Conditions make policies precise. aws:SourceIp limits callers to an IP range. aws:MultiFactorAuthPresent requires MFA. aws:PrincipalOrgID allows anyone in your organization without listing every account. aws:RequestedRegion limits Regions. Tag keys such as aws:PrincipalTag and aws:ResourceTag enable attribute-based access control (ABAC), where a user can only touch resources carrying the same team tag.
Policies come in three flavors: AWS managed (maintained by AWS, often too broad), customer managed (yours, reusable and versioned), and inline (embedded in one identity). Prefer customer managed policies for anything you write.
- Effect, Action, Resource, Condition, plus Principal in resource policies
- NotAction and NotResource invert a list; use them carefully
- A customer managed policy can hold up to 6,144 characters
- Policy variables like aws:username personalize one policy for many users
Same account versus cross-account
Inside one account, a request is allowed if an identity-based policy or a resource-based policy allows it, and nothing denies it. A bucket policy that names a user can grant access even if the user has no IAM policy for S3.
Across accounts the rule is stricter: both sides must agree. The caller's account must allow the action in an identity policy, and the resource's account must allow it in a resource policy or a role trust policy. If either side is silent, the request fails.
On top of that, every guardrail still applies: SCPs on the caller's account, RCPs on the resource's account, permissions boundaries, and session policies. Guardrails can only remove permissions. A common surprise is KMS: reading an SSE-KMS object in another account also needs kms:Decrypt allowed in the key policy, not just the bucket policy.
- Identity policy OR resource policy allows
- No explicit deny anywhere
- SCPs, boundaries and session policies allow
- A bucket policy alone can be enough
- Identity policy in caller account allows
- AND resource or trust policy allows
- No explicit deny anywhere
- Guardrails in both accounts allow
Cross-account roles and the confused deputy
To let account A use account B, create a role in B. Its trust policy names A as the principal, for example arn:aws:iam::111122223333:root, which means "any identity in A that has permission". In A, give the caller an identity policy allowing sts:AssumeRole on that role ARN. The caller then calls AssumeRole and receives temporary keys for B.
When a third party, such as a monitoring vendor, assumes a role in your account, add an ExternalId condition. Without it, another customer of that vendor could trick the vendor into using your role, the so-called confused deputy problem. For AWS services acting on your behalf, use aws:SourceArn and aws:SourceAccount conditions for the same reason.
Every AssumeRole call is logged in CloudTrail in both accounts, so you can see who borrowed which role and when.
- App in account A to AWS STS: AssumeRole for the role ARN in B1AssumeRole for the role ARN in B
- AWS STS to Role in account B: Check trust policy allows account A2Check trust policy allows account A
- AWS STS to App in account A: Temporary keys, about 1 hour3Temporary keys, about 1 hour
- App in account A to DynamoDB in B: GetItem signed with role keys4GetItem signed with role keys
- DynamoDB in B to App in account A: Item returned if role policy allows5Item returned if role policy allows
Testing and tightening policies
Do not guess whether a policy works. The IAM policy simulator, in the console or through aws iam simulate-principal-policy, evaluates real policies against chosen actions and resources and tells you allowed, implicitly denied or explicitly denied, and which statement decided it.
IAM Access Analyzer does more. Policy validation flags errors and risky patterns while you write. External access findings show resources, such as buckets, roles and KMS keys, that are shared outside your account or organization. Unused access findings list roles, keys and permissions nobody has used. Policy generation reads CloudTrail history and drafts a policy containing only the actions a role really called.
The last accessed information on each user or role shows which services were used recently. Removing services nobody touched in 90 days is an easy, safe way to move toward least privilege.
Learn it in 5 short steps
About 1h each. Track them with a timer in your plan.
- 1Compare IAM users, groups and roles~1h
- 2Write identity-based and resource-based policies~1h
- 3Walk through policy evaluation logic~1h
- 4Use roles for EC2 and cross-account access~1h
- 5Test policies with the IAM policy simulator~1h
Key points
- Default is implicit deny; explicit deny overrides any allow.
- Use roles, not access keys, for EC2, Lambda and cross-account access.
- Cross-account access needs a trust policy on the role and permissions in the caller account.
- Permissions boundaries and SCPs only limit permissions; they never grant them.
- Grant least privilege by scoping actions, resources and conditions.
Key terms
Tap a card to flip it. 0/6 known
Examples
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": ["s3:GetObject"],
"Resource": "arn:aws:s3:::reports-bucket/2026/*",
"Condition": {"Bool": {"aws:SecureTransport": "true"}}
}]
}Least-privilege read access to one prefix over TLS only.
Team-scoped EC2 control with tags (ABAC)
Developers should start and stop only their own team's instances. Step 1: tag each developer's role or user with team=payments or team=search. Step 2: tag each instance with the same key. Step 3: write one policy that compares the two tags, so it works for every team without listing instance IDs. Step 4: test it: a payments developer stopping a search instance gets AccessDenied. Describe calls do not support resource-level conditions, so they get a separate statement.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["ec2:StartInstances", "ec2:StopInstances"],
"Resource": "arn:aws:ec2:*:*:instance/*",
"Condition": {"StringEquals":
{"aws:ResourceTag/team": "${aws:PrincipalTag/team}"}}
},
{"Effect": "Allow", "Action": "ec2:DescribeInstances", "Resource": "*"}
]
}Predicting a cross-account S3 read
A role in account A wants to read s3://partner-data/2026/sales.csv in account B. Step 1: the role's identity policy allows s3:GetObject on that ARN. Not enough yet. Step 2: the bucket policy in B is checked; it does not mention A, so the request is denied. Step 3: B adds a bucket policy statement allowing that role ARN to call s3:GetObject on partner-data/2026/*. Now it works for SSE-S3 objects. Step 4: the file is encrypted with a customer managed KMS key in B, and the read fails again. Step 5: B adds the role to the key policy with kms:Decrypt, and A's identity policy allows kms:Decrypt on that key ARN. Now every layer agrees, and the read succeeds.
Quick reference
- Default is implicit deny; any explicit Deny beats every Allow
- Same account: identity OR resource policy can allow. Cross-account: both sides must allow
- SCPs, RCPs, boundaries and session policies only limit; they never grant
- Role sessions default to 1 hour, max 12 hours; role chaining caps at 1 hour
- Use ExternalId in trust policies for third parties to stop the confused deputy
- aws iam simulate-principal-policy tests real policies against actions and ARNs
- Access Analyzer finds external sharing, unused access, and can generate policies from CloudTrail
- Groups cannot be nested and cannot be a principal
Common mistakes
- Storing access keys on EC2 instead of attaching an instance profile role.
- Thinking an SCP Allow grants access; users still need an IAM allow.
- Using wildcards like s3:* on * when one action and one ARN would do.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write a policy that lets a user list one bucket called team-docs and read objects in it, nothing else.
- Exercise 2medium
Create a role in your account that a second account (222233334444) can assume, then assume it from the CLI.
- Exercise 3hard
Use the policy simulator CLI to prove that a role called app can read but not delete objects under reports-bucket/2026/.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between an IAM role and an IAM user?
- A user has permanent credentials tied to one identity. A role has no permanent credentials; trusted principals assume it and get temporary keys that expire.
- Why is the policy Version always 2012-10-17?
- It is the current policy language version, not a date you update. Older versions lack features such as policy variables, so always use 2012-10-17.
- Does an explicit Allow override an SCP deny?
- No. An explicit deny anywhere, including in an SCP, always wins over any allow.
- How many managed policies can I attach to a role?
- Ten by default, and you can request a quota increase to 20. If you need more, combine permissions into fewer customer managed policies.