Sub topic 6
Resource-based policies
This chapter assumes you can read IAM JSON, know what an ARN is, and have met identity-based policies in the previous chapter. Here you will see how a policy attached to a resource changes the evaluation.
What a resource-based policy is
A resource-based policy is a policy attached directly to a resource, such as an S3 bucket, an SQS queue, a KMS key, or the trust policy of an IAM role. It states who may act on that resource.
An identity-based policy answers "what can this principal do?" A resource-based policy answers "who can do things to this resource?" Because it is attached to the resource, the policy does not know who the caller is unless you say so. It therefore needs a Principal element. An identity policy has no Principal, because the principal is whoever the policy is attached to.
Reading the Principal element
The Principal element names the account, user, role, or service that the statement applies to. Here is a bucket policy from account 111122223333 that names a role in another account:
{
"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/*"
}
]
}Three details matter:
- The
Principalis a role ARN in account 222233334444. The bucket lives in 111122223333. - The
Resourcemust match the resource the policy is attached to. Object actions such ass3:GetObjectuse the/*form of the ARN. Bucket actions such ass3:ListBucketuse the bucket ARN without/*. - Setting
Principalto"*"means everyone, including anonymous callers. Pair it with aCondition, or avoid it.
How it fits the evaluation rule
The core rule from earlier still applies. A request is denied by default. An explicit Deny in any applicable policy wins. Otherwise at least one Allow is needed. A resource-based policy is one more source of Allow and Deny statements.
Its special property is that, in the same account, it can grant access on its own. If a bucket policy allows a specific role ARN in the same account, that role can read the objects even when its identity policy says nothing about S3. The two policy types are combined, and either one is enough.
The diagram shows the same-account case with no other guardrails. Boundaries and SCPs, covered in later chapters, can still remove permissions.
Cross-account access needs both sides
When the caller is in a different account from the resource, one Allow is not enough. The resource's account must allow the principal, and the caller's own account must also allow the action. For the bucket policy above to work, the identity policy on ReportReader in account 222233334444 must also allow s3:GetObject on that bucket. If either side is missing, the request is denied by default.
Explicit deny in a resource policy
A resource policy can also block access. This statement denies any request that does not use TLS, and no Allow elsewhere overrides it:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyInsecureTransport",
"Effect": "Deny",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::example-reports-bucket",
"arn:aws:s3:::example-reports-bucket/*"
],
"Condition": { "Bool": { "aws:SecureTransport": "false" } }
}
]
}Note that Principal is omitted here. A Deny statement in a bucket policy normally needs a Principal, and "Principal": "*" is the usual choice. Without it, S3 rejects the policy as malformed. Add "Principal": "*" before you apply this statement to a real bucket.
Common errors
- Forgetting the identity-side allow in a cross-account request.
- Using the bucket ARN for object actions, or the object ARN for bucket actions.
- Naming a principal that does not exist. The policy may be saved with an internal ID in place of the ARN, and then it no longer matches a recreated role.
- Expecting a resource policy to bypass a permission boundary or SCP. It does not.
Exercise
A role in account 222233334444 is named in a bucket policy in account 111122223333 that allows s3:GetObject. The role's identity policy does not mention S3. Is a GetObject request allowed? What changes if the role is in account 111122223333 instead?
Show answer
Not allowed. For a cross-account request, both the bucket policy and the role's identity policy must allow the action, so the missing identity-side allow causes a default deny. If the role is in the same account as the bucket, the bucket policy alone is enough, provided no explicit deny, boundary, or SCP blocks the request.