Sub topic 6
Resource-based policies
This chapter assumes you can read IAM policy JSON and know what an ARN and an API action are. It also assumes you know the core rule from earlier in the tutorial: requests are denied by default, an explicit deny always wins, and otherwise at least one allow is needed.
What a resource-based policy is
A resource-based policy is a policy document attached to a resource, not to an identity. It answers the question "who may do what to me?" An identity-based policy answers the opposite question: "what may this user or role do?"
Common places to find resource-based policies:
- S3 bucket policies
- SQS queue policies and SNS topic policies
- KMS key policies
- Lambda function policies
- Secrets Manager secret policies
- ECR repository policies
- IAM role trust policies, which are a resource-based policy attached to a role
Two things set these policies apart from identity-based ones:
- They must contain a
Principalelement, because the policy does not know who is calling. - They can name principals in other AWS accounts. This is the main way to share a resource directly across accounts.
Not every service supports them. Check the service's documentation before you plan around one.
Anatomy of the Principal element
The Principal element says who the statement applies to. The forms you will use most:
| Principal value | Meaning |
|---|---|
| `{"AWS": "arn:aws:iam::222233334444:root"}` | The account 222233334444 as a whole. This delegates the decision to that account's own IAM policies. It does not grant access to every identity in the account. |
| `{"AWS": "arn:aws:iam::222233334444:role/ReportReader"}` | One specific role. |
| `{"AWS": "arn:aws:iam::222233334444:user/alice"}` | One specific user. |
| `{"AWS": "arn:aws:sts::222233334444:assumed-role/ReportReader/session1"}` | One specific role session. |
| `{"Service": "sns.amazonaws.com"}` | An AWS service acting on your resource. |
| `"*"` | Anyone, including anonymous callers. |
When you save a policy that names a user or role, AWS stores the principal's internal unique ID. If you delete the role and create a new one with the same name, the old statement no longer matches the new role. A policy that shows a raw ID such as AROA... in place of an ARN has lost its principal this way.
A cross-account example
This bucket policy lives on a bucket in account 111122223333. It lets one role from account 222233334444 read objects.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowReportReaderRoleFromOtherAccount",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::222233334444:role/ReportReader" },
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/*"
}
]
}You can attach it from the CLI, run in account 111122223333. The command assumes the JSON above is saved as policy.json:
aws s3api put-bucket-policy \
--bucket example-reports-bucket \
--policy file://policy.jsonThis policy alone is not enough. Because the caller is in a different account, the ReportReader role also needs an identity-based policy in account 222233334444 that allows s3:GetObject on the same objects. The next section explains why.
How resource policies combine with identity policies
How the two policy types combine depends on whether the caller and the resource share an account.
Same account
Within one account, an allow in either the identity-based policy or the resource-based policy is enough. The two are a union. If the role's identity policy allows s3:GetObject, the bucket policy does not need to mention the role. If the bucket policy names the role, the role's identity policy does not need to allow it.
Cross account
Across accounts, both sides must allow the request. The resource account must trust the caller, through the resource policy. The caller's account must permit its own principal to make the call, through the identity policy. Either side missing means an implicit deny.
If the resource policy names the other account's root, that only opens the door. Each identity in that account still needs its own identity policy allowing the action.
Same-account exception for named principals
There is one exception to "limits in the identity side still apply". Suppose the resource policy names an IAM user ARN, or a role session ARN, in the same account as the resource. That statement grants access directly. An implicit deny from the caller's permission boundary or session policy does not block it. A statement that names a role ARN, or only the account root, does not get this treatment. The boundary and session policy still limit those.
The flow in a picture
The diagram below shows how the resource policy and the identity policy are consulted for a request, after any explicit deny has been checked.
Permission boundaries, session policies, and SCPs add further checks on the identity side. The full flow chapter combines them. This diagram is the resource-policy part only.
Resource ARNs inside the policy
In an S3 bucket policy, the Resource element must match the type of resource the action works on:
- Object actions such as
s3:GetObjectands3:PutObjectneed the object ARN, such asarn:aws:s3:::example-reports-bucket/*. - Bucket actions such as
s3:ListBucketneed the bucket ARN, such asarn:aws:s3:::example-reports-bucket.
A statement that lists s3:GetObject against only the bucket ARN will never match anything. This is a very common reason for an AccessDenied on a policy that looks right.
Other services work differently. In a KMS key policy, "Resource": "*" means "this key". In many service policies, Resource is the ARN of the resource the policy sits on.
Granting access to an AWS service
When a service such as SNS, S3, or CloudFront acts on your behalf, you name it in Principal.Service. You should also restrict which source it acts for. Without that, any other customer's resource of the same service type might also be able to use your policy. That situation is called the confused deputy problem.
This SQS queue policy lets one specific SNS topic send messages to the queue and nothing else:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowSnsTopicToSend",
"Effect": "Allow",
"Principal": { "Service": "sns.amazonaws.com" },
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:us-east-1:111122223333:report-events",
"Condition": {
"ArnEquals": {
"aws:SourceArn": "arn:aws:sns:us-east-1:111122223333:report-topic"
}
}
}
]
}The condition keys aws:SourceArn and aws:SourceAccount are the standard guard here.
Using conditions to widen safely
Sometimes you want to allow a whole organization instead of one named role. Instead of listing every account, set Principal to "*" and require the caller to belong to your AWS Organization:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowOrgMembersToRead",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::example-reports-bucket/*",
"Condition": {
"StringEquals": { "aws:PrincipalOrgID": "o-exampleorgid" }
}
}
]
}The wildcard principal is only safe because the condition limits it. Without the condition, this statement would make the objects public. Callers from member accounts still need an identity policy that allows the action, because this is a cross-account request.
Common errors
- Allow on one side only. A cross-account request fails because only the bucket policy or only the identity policy has the allow. Check both accounts.
- Wrong ARN type in
Resource. Object actions on the bucket ARN, or the reverse. - Principal that no longer exists. The unique ID changed after the role was recreated. Rewrite the statement with the current ARN.
- Account root misunderstood. Naming
:rootdoes not grant access to every identity. It only lets that account's policies make the decision. - A lockout from your own policy. A broad Deny in a bucket policy can block administrators too. Add a condition that exempts a trusted admin role, or test with the policy simulator before saving.
- KMS keys. An IAM policy cannot grant access to a KMS key unless the key policy allows the account to use IAM policies for that key. The key policy is the starting point, not an optional layer.
Exercise
Account 111122223333 owns a bucket. Its bucket policy allows s3:GetObject on all objects to the role arn:aws:iam::222233334444:role/Analyst. The Analyst role in account 222233334444 has no identity policy that mentions S3. Can the role read an object? What changes if the bucket and the role are in the same account?
Show answer
Across accounts, the request is denied. The bucket policy allows it, but the role's own account has not granted s3:GetObject, so there is an implicit deny on that side. Both sides must allow a cross-account request. If the bucket and the role are in the same account, the bucket policy's allow is enough on its own, because an allow in either the identity policy or the resource policy is sufficient, as long as no explicit deny, and no permission boundary or session policy that applies to this statement, blocks it.