Sub topic 13
Resource-based policies
This chapter assumes you can read an identity-based policy (Effect, Action, Resource, Condition) and know the difference between a user and a role. Here you will see how a policy attached to a resource changes who can reach it.
What a resource-based policy is
A resource-based policy is a JSON policy attached to a resource, such as an S3 bucket, an SQS queue, an SNS topic, a KMS key, or a Lambda function. An identity-based policy answers "what can this identity do?" A resource-based policy answers "who can do things to this resource?"
The main difference in the document is the Principal element. It names who the statement applies to. Identity-based policies leave it out, because the principal is whichever identity the policy is attached to.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowPartnerRead",
"Effect": "Allow",
"Principal": { "AWS": "arn:aws:iam::444455556666:root" },
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::amzn-s3-demo-bucket/*"
}
]
}This bucket policy lets account 444455556666 read objects. The :root ARN refers to the whole account, not the root user. The account's administrators must still grant their own users or roles permission to call s3:GetObject.
Principal values
| Principal | Example | Meaning |
|---|---|---|
| Account | `"AWS": "arn:aws:iam::444455556666:root"` | Any identity in that account that is allowed by its own policies |
| IAM role or user | `"AWS": "arn:aws:iam::444455556666:role/Reader"` | One specific identity |
| AWS service | `"Service": "sns.amazonaws.com"` | An AWS service acting on your behalf |
| Everyone | `"AWS": "*"` | Anyone, including anonymous callers |
Same-account and cross-account behavior
The two policy types combine differently depending on where the caller lives.
- Same account: an Allow in either the identity-based policy or the resource-based policy is enough, provided nothing denies the request.
- Cross-account: both sides must allow. The resource policy in the owning account must name the outside principal, and the caller's identity-based policy must allow the action.
Resource policy or role?
Cross-account access can use either a resource-based policy or an assumed role. The difference is what the caller ends up acting as.
- With a resource-based policy, the caller keeps its own identity and its own permissions. It can still use resources in its own account in the same session.
- With an assumed role, the caller gives up its original permissions for the session and uses only the role's permissions.
Use a resource policy when a partner needs to reach one resource and nothing else. Use a role when the caller needs a broader set of permissions in your account.
Common errors
- Wrong ARN for the action.
s3:ListBucketapplies to the bucket ARN, ands3:GetObjectapplies tobucket/*. A mismatched ARN silently grants nothing. - Using
"Principal": "*"without a condition. This makes the resource public. Add a condition such asaws:PrincipalOrgIDto limit it. - Forgetting the caller's side. A bucket policy that allows another account does nothing until that account's identity policy also allows the action.
- Deleted principals. If you name a specific role and later delete and recreate it, the new role has a different internal ID. The old trust no longer matches.
Exercise
Account A owns a bucket with a policy that allows s3:GetObject to account B's root. A user in account B calls GetObject and gets AccessDenied. The user has no identity-based policy for S3. What is the fix, and why does the bucket policy alone not work?
Show answer
Account B must attach an identity-based policy to the user (or its group or role) that allows s3:GetObject on the bucket's objects. For cross-account requests, both the resource policy and the caller's identity policy must allow the action. The bucket policy only trusts account B. It does not grant its users any permission.