Lesson 7 of 30Identity and security
IAM Policies in Depth
The full evaluation flow, cross-account roles, condition keys, boundaries and SCPs, with a runnable policy evaluator to test your own policies.
A Lambda function in your data account must read last night's export from a bucket in the production account. The bucket is encrypted with a customer managed KMS key. You give the role s3:GetObject, deploy, and get AccessDenied. You add a bucket policy. Still denied. The fix needs a third policy in a service you never touched, and an SCP somewhere above both accounts could block it anyway. In the IAM lesson you learned the core rule: explicit deny wins, then allow, otherwise deny. This lesson is everything that sits around that rule, because that is where real AccessDenied tickets come from.
This unit goes a level deeper than Core services. If you are new to AWS, skim it on a first pass and come back when you hit your first cross-account AccessDenied.
Seven policy types, two jobs
Every policy type does one of two jobs. Some grant permissions. Others only cap them: they can never give access, they can only take it away.
| Policy type | Attached to | Grants or caps | Written by |
|---|---|---|---|
| Identity-based | User, group, role | Grants | App and platform teams |
| Resource-based | Bucket, key, queue, secret, role (trust policy) | Grants | Resource owner |
| Permissions boundary | User or role | Caps | Platform team |
| Session policy | One AssumeRole session | Caps | Whoever assumes the role |
| SCP (service control policy) | Org root, OU or account | Caps principals | Cloud platform team |
| RCP (resource control policy) | Org root, OU or account | Caps resources | Cloud platform team |
| VPC endpoint policy | A VPC endpoint | Caps | Network team |
Two facts surprise people. First, a role's trust policy is a resource-based policy. The role is the resource, sts:AssumeRole is the action. Second, SCPs and RCPs are not "allow lists" in the sense of granting anything. The default FullAWSAccess SCP only says "this layer does not object". You still need an identity or resource policy that grants the action.
SCPs limit principals in member accounts (including each member account's root user). RCPs, added in late 2024, limit resources in member accounts, no matter who calls them, even principals from outside your organization. Neither affects the management account or service-linked roles. RCPs cover a growing list of services, including S3, STS, KMS, SQS and Secrets Manager. Check the Organizations docs for the current list.
The full evaluation flow
When a request arrives, AWS gathers every policy that applies and walks through them. Step through the flow.
AWS collects every applicable policy: identity, resource, boundary, session, SCPs and RCPs. It looks for a matching Deny in all of them first.
Read as set algebra, the usual path is: identity ∩ boundary ∩ session ∩ SCP ∩ RCP. Adding a boundary or SCP can only shrink the result. That is why platform teams like them: they are safe to add.
The same-account shortcut, precisely
Inside one account, a resource-based policy that names a principal can grant access even when no identity policy does. The details depend on which ARN it names:
- An IAM user ARN: not limited by an implicit deny in the user's identity policy or boundary.
- A role session ARN (
arn:aws:sts::111111111111:assumed-role/app/session-1): not limited by identity policy, boundary or session policy. - A role ARN (
arn:aws:iam::111111111111:role/app): does not need an identity policy, but is limited by the boundary and session policy. - The account (
arn:aws:iam::111111111111:root): grants nothing by itself. It delegates to IAM, so identity policies decide.
Two resource types break the shortcut entirely: KMS key policies and role trust policies must explicitly allow access. An identity policy alone is never enough for them. The KMS lesson comes back to this.
Same account vs cross-account
Cross-account requests are evaluated twice: once in the caller's account, once in the resource's account. Both must allow.
- The caller's account checks the identity policy, boundary, session policy and the caller's SCPs.
- The resource's account checks the resource policy and the resource's RCPs.
So cross-account access always needs two policies, one on each side. A bucket policy that names a role in another account does nothing if that role's identity policy does not also allow s3:GetObject on the bucket. And an identity policy alone cannot reach into another account.
s3:* on *. A bucket in account B has no bucket policy. The role calls GetObject on that bucket. What happens?Cross-account roles and the confused deputy
Most cross-account access is done with roles, not resource policies. Account B creates a role whose trust policy names account A. A principal in A calls sts:AssumeRole (its own identity policy must allow that), gets temporary credentials for B, and works in B with the role's permissions.
Third parties add a twist. A monitoring vendor assumes a role in hundreds of customer accounts. If the vendor's trust relationship is just "the vendor's account may assume this role", an attacker who is also a vendor customer could type your role ARN into their own settings page. The vendor, acting with its own trusted identity, would assume your role for them. That is the confused deputy problem: a privileged service tricked into acting for the wrong customer.
The fix is an external ID: a value the vendor generates per customer and always sends. Your trust policy requires it.
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::333333333333:role/vendor-scanner" },
"Action": "sts:AssumeRole",
"Condition": { "StringEquals": { "sts:ExternalId": "acme-7f3a9c" } }
}]
}The attacker is a customer of the same vendor and enters your role ARN in their settings.
AWS services hit the same problem. When S3 publishes events to your SNS topic, the topic policy trusts s3.amazonaws.com, which could be acting for any bucket in any account. Pin it with aws:SourceArn (the exact bucket) and aws:SourceAccount (your account ID) in the condition. Whenever a resource policy trusts a service principal, add those two keys.
Condition keys that matter
Conditions are where policies get precise, and where most subtle bugs live.
| Key | What it checks | Typical use |
|---|---|---|
aws:PrincipalOrgID | Caller's organization ID | Bucket policy: "anyone in my org" without listing accounts |
aws:SecureTransport | Request used TLS | Deny plain HTTP to S3 |
aws:RequestedRegion | Region the call targets | SCP region lock |
aws:SourceIp | Caller's public IP | Office-only access. Not present for calls through a VPC endpoint |
aws:SourceVpce | VPC endpoint ID | Bucket reachable only from your VPC |
aws:PrincipalTag/K, aws:ResourceTag/K | Tags on caller and target | ABAC |
aws:MultiFactorAuthPresent | MFA used for the session | Protect destructive actions |
aws:SourceArn, aws:SourceAccount | Resource that made a service call | Confused deputy protection |
The operators have rules of their own:
StringEqualsis exact.StringLikeallows*and?wildcards. UsingStringEqualswith a*in the value is a common bug: it matches only a literal asterisk.- Several values for one key are OR. Several keys or operators in one block are AND.
- When a key is missing from the request, a normal operator is false, a negated one (
StringNotEquals) is true, and theIfExistsvariants are true.Nulltests presence directly. aws:MultiFactorAuthPresentis missing entirely for long-term access keys. So"Bool": {"aws:MultiFactorAuthPresent": "false"}does not catch them. AWS recommends a Deny withBoolIfExistsset tofalseinstead.ForAllValuesandForAnyValueare for multivalued keys likeaws:TagKeys.ForAllValuesreturns true when the key is absent, so an Allow that meant "only these tag keys" also allows a request with no tags at all. Pair it with"Null": {"aws:TagKeys": "false"}. Never use these qualifiers on single-valued keys.
Match each requirement to the condition key that enforces it.
ABAC: permissions from tags
Role-based access control writes one policy per team and resource. Attribute-based access control (ABAC) writes one policy that compares tags:
{
"Effect": "Allow",
"Action": ["ec2:StartInstances", "ec2:StopInstances"],
"Resource": "*",
"Condition": {
"StringEquals": { "aws:ResourceTag/team": "${aws:PrincipalTag/team}" }
}
}${aws:PrincipalTag/team} is a policy variable, filled from the caller's tag at request time. A new team needs no new policy, just tags. Tags on a role session can come from the identity provider as session tags, so your Okta or Entra ID department attribute can drive AWS permissions directly.
The catch: ABAC is only as strong as control over tags. If engineers can retag an instance or their own role, they can grant themselves access. Deny ec2:CreateTags and ec2:DeleteTags on the team key for everyone but the platform role, and do not let people tag their own principals.
Test it yourself: a policy evaluator
Here is a working model of the evaluation flow. It handles explicit deny, Allow, NotAction, wildcards, policy variables, common condition operators with IfExists, Null, ForAllValues and ForAnyValue, SCP levels, RCPs, boundaries, session policies and the cross-account two-sided check. It is a model, not AWS: real evaluation has more operators and service-specific rules. But it gets the logic right, and you can paste in your own policies.
Press Run (⌘ Enter) to see what happens.Walk through the output. Scenario 2 is the SCP region lock: the Deny matches because us-east-1 is not in the list. Scenario 3 fails on account B's side, and scenario 4 fixes it with a bucket policy. Scenario 5 shows a boundary capping an admin policy. Scenario 7 is the ForAllValues trap: an untagged launch slips through a policy that was meant to require tags.
Add this line to the end of the evaluator and run it. What does it print?
print(evaluate(put, identity=[read_reports], boundary=boundary,
session={"Statement": [{"Effect": "Deny", "Action": "s3:*", "Resource": "*"}]}))Permission boundaries: letting developers create roles
Platform teams face a dilemma. Developers need to create roles for their Lambdas without a ticket. But iam:CreateRole plus iam:AttachRolePolicy is a path to admin: create a role with AdministratorAccess, assume it, done.
A permissions boundary solves it. You allow role creation only when the new role carries a boundary you control:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "CreateRolesOnlyWithBoundary",
"Effect": "Allow",
"Action": ["iam:CreateRole", "iam:PutRolePermissionsBoundary",
"iam:AttachRolePolicy", "iam:PutRolePolicy"],
"Resource": "arn:aws:iam::111111111111:role/app-*",
"Condition": {
"StringEquals": {
"iam:PermissionsBoundary": "arn:aws:iam::111111111111:policy/app-boundary"
}
}
},
{
"Sid": "NeverRemoveOrEditTheBoundary",
"Effect": "Deny",
"Action": ["iam:DeleteRolePermissionsBoundary", "iam:CreatePolicyVersion",
"iam:SetDefaultPolicyVersion", "iam:DeletePolicy"],
"Resource": ["arn:aws:iam::111111111111:role/*",
"arn:aws:iam::111111111111:policy/app-boundary"]
},
{
"Sid": "PassOnlyAppRoles",
"Effect": "Allow",
"Action": "iam:PassRole",
"Resource": "arn:aws:iam::111111111111:role/app-*"
}
]
}Now a developer can attach AdministratorAccess to an app-* role, and it does not matter: the role's effective permissions are the intersection with app-boundary. The Deny stops them from removing or rewriting the boundary, and the name prefix keeps them away from platform roles.
Dangerous patterns to catch in review
NotActionwithAllow."NotAction": "iam:*"with Allow on*grants everything except IAM, including every service AWS launches next year.NotActionbelongs in Deny statements (like the region lock).- Write actions on
"Resource": "*". Fine forDescribe*andList*. Not fine fors3:DeleteObjectordynamodb:DeleteTable. - Wildcard principals.
"Principal": "*"in a bucket, queue or key policy is public unless a condition such asaws:PrincipalOrgIDoraws:SourceVpcenarrows it. NotPrincipalwith Deny. It behaves badly with boundaries and sessions. AWS recommendsArnNotEqualsonaws:PrincipalArninstead.- Trust policies that trust a whole account when one role would do.
lambda:CreateFunction, lambda:InvokeFunction and iam:PassRole on *. Nothing else. Why is this a security finding?Tools: reading denials and proving policies
Read the error. AccessDenied messages for many services now name the policy type, and sometimes its ARN:
User: arn:aws:sts::111111111111:assumed-role/report-lambda/fn is not authorized to perform:
s3:GetObject on resource: arn:aws:s3:::reports-b/q3.csv
with an explicit deny in a service control policy"with an explicit deny in a X policy" means find the Deny. "because no X policy allows" means an Allow is missing in that layer. When several layers deny, only one is named.
IAM Access Analyzer does four jobs worth knowing:
- Policy validation: grammar errors, security warnings and suggestions, in the console editor or the CLI. Run it in CI.
- Custom policy checks:
check-no-new-accesscompares a changed policy with the old one,check-access-not-grantedfails if a policy grants actions you list (likeiam:PassRole), andcheck-no-public-accesscatches public resource policies. These are how teams block risky policy changes in pull requests. - External and unused access findings: resources shared outside your account or org, and roles, keys and permissions nobody has used.
- Policy generation: builds a starting policy from what a role actually called, using CloudTrail.
aws accessanalyzer validate-policy --policy-type IDENTITY_POLICY --policy-document file://developer-iam.jsonaws iam simulate-principal-policy --policy-source-arn arn:aws:iam::111111111111:role/report-lambda --action-names s3:GetObject --resource-arns arn:aws:s3:::reports-eu/q3.csvThe policy simulator (simulate-principal-policy) evaluates the identity policies and boundary of a real principal against an action. It does not cover everything (for example, cross-account resource policies need extra input), so treat it as one signal alongside CloudTrail.
Put the layers of a same-account evaluation in the order AWS considers them.
- Tap the steps below in the right order.
- permissions boundary
- A managed policy set on a user or role that caps what its identity policies can grant. Grants nothing itself.
- SCP
- Service control policy. An Organizations policy that caps what principals in member accounts can do. Never grants.
- RCP
- Resource control policy. An Organizations policy that caps what anyone can do to resources in member accounts.
- session policy
- A policy passed in an AssumeRole call that narrows that one session's permissions.
- confused deputy
- A trusted service tricked into using its access on behalf of the wrong party. Prevented with an external ID or aws:SourceArn.
- ABAC
- Attribute-based access control: one policy that compares principal tags with resource tags.
- external ID
- A per-customer value a third party must send when assuming your role, checked with the sts:ExternalId condition.