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. It covers policies attached to the resource being accessed, how they differ from identity-based policies, and the errors that come up most often.
What a resource-based policy is
A resource-based policy is a JSON policy document attached to a resource instead of to an identity. S3 buckets, SQS queues, SNS topics, KMS keys, Lambda functions, Secrets Manager secrets, and IAM role trust policies all work this way. The policy says who may do what to that one resource.
The key structural difference is the Principal element. An identity-based policy never has one, because the principal is whoever the policy is attached to. A resource-based policy must have one, because it has to name who is being granted or denied access.
| Aspect | Identity-based policy | Resource-based policy |
|---|---|---|
| Attached to | User, group, or role | A resource such as a bucket or queue |
| `Principal` element | Not allowed | Required |
| `Resource` element | Required | Often optional or implied, depending on the service |
| Can grant to another account directly | No, needs a role | Yes |
| Managed or inline choices | AWS managed, customer managed, inline | One policy per resource, written inline |
A role trust policy is a special resource-based policy. It is attached to the role, and its Principal names who may assume that role.
A cross-account bucket policy
This bucket policy lets any principal in account 444455556666 read objects, as long as that account also allows it.
{
"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/*"
}
]
}Three details matter here.
- The principal
arn:aws:iam::444455556666:rootmeans the whole account, not the root user. It delegates the decision to that account's administrators. They still have to grants3:GetObjectto specific users or roles with identity-based policies. - The
Resourceends in/*, so it covers objects. Bucket-level actions such ass3:ListBucketneed the bucket ARN with no suffix. Put an object action on the bucket ARN, or a bucket action on/*, and the statement never matches. - You can name a narrower principal, such as a single role ARN, to avoid delegating to the entire account.
How principals are written
The Principal element accepts several forms.
| Principal | Example value | Meaning |
|---|---|---|
| AWS account | `arn:aws:iam::444455556666:root` or the 12-digit ID | Everyone the account delegates to |
| IAM user | `arn:aws:iam::444455556666:user/alice` | One user |
| IAM role | `arn:aws:iam::444455556666:role/Reader` | Any session of that role |
| AWS service | `{ "Service": "sns.amazonaws.com" }` | An AWS service acting on your behalf |
| Anyone | `"*"` or `{ "AWS": "*" }` | Everyone, including anonymous callers |
Do not name an assumed-role session as an identity you expect to keep working. If you name a role ARN, the policy matches every session of that role. If you name a specific session ARN, it stops matching once that session ends.
A "*" principal with Allow makes the resource public unless a condition narrows it. Pair it with a condition such as aws:PrincipalOrgID or aws:SourceVpce when you need broad but bounded access.
Same account versus cross-account evaluation
Resource-based policies behave differently depending on whether the caller is in the same account as the resource.
- Same account: an
Allowin either the identity-based policy or the resource-based policy is enough, provided nothing denies the request. For most services, a resource policy that names a user or role ARN grants access with no identity policy at all. - Cross-account: both sides must allow. The resource policy in the resource's account must allow the principal, and an identity-based policy in the caller's account must allow the action.
The same-account rule has an exception. If the resource policy names an IAM role session or federated principal by session ARN, that grant is not additive with the identity policy. Check the service documentation when you rely on this.
The diagram shows the cross-account case.
Resource policy or role: which to use for cross-account access
You have two ways to share a resource across accounts.
| Approach | How it works | Strengths | Limits |
|---|---|---|---|
| Resource-based policy | The caller keeps its own identity and is named in the resource policy | No credential switch. The caller's identity shows in logs. Works with only the resource owner's setup | Only for services that support resource policies. Each resource needs its own policy |
| Assumed role | The caller assumes a role in the resource's account and acts as that role | Works for any service. One role can cover many resources | The caller acts as the role, so it gives up its original permissions during the session |
A concrete consequence of the second row: a principal that assumes a role in another account can no longer use its own account's permissions in that session. If a job copies data from a bucket in account A to a bucket in account B, a resource policy lets one identity touch both. A role forces you to split the work across credentials.
Explicit deny in a resource policy
A Deny in a resource policy overrides every Allow, including ones in identity-based policies. This makes resource policies a good place for a guardrail that no caller can bypass.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "DenyInsecureTransport",
"Effect": "Deny",
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::amzn-s3-demo-bucket",
"arn:aws:s3:::amzn-s3-demo-bucket/*"
],
"Condition": {
"Bool": { "aws:SecureTransport": "false" }
}
}
]
}The Resource list contains both ARN forms so the deny covers bucket-level and object-level actions.
Applying a resource policy from the CLI
Save a policy to a file and attach it to a bucket.
aws s3api put-bucket-policy \
--bucket amzn-s3-demo-bucket \
--policy file://bucket-policy.json
aws s3api get-bucket-policy --bucket amzn-s3-demo-bucketput-bucket-policy replaces the whole policy. It does not merge. Read the current policy first, edit it, and write the full document back, or you will remove existing statements.
Common errors
- Malformed principal. S3 rejects a policy with
Invalid principal in policyif the account or ARN does not exist or is mistyped. A deleted user or role ARN is stored as a unique ID, which shows up as an opaque string likeAIDA...in the saved policy. - Wrong ARN scope. Mixing bucket and object ARNs, described above, gives a policy that saves without error but never matches.
- One-sided cross-account grant. The resource policy allows account B, but account B has no identity policy. The caller gets
AccessDenied. The reverse is equally common. - Accidental public access. A
"*"principal with no condition. S3 Block Public Access can reject or neutralize such a policy, so check that setting before debugging. - Overwriting the policy. Because
put-bucket-policyreplaces the whole document, a script that writes only one statement deletes the rest. - KMS surprises. A KMS key policy is the primary access control for the key. Unlike most resources, the key policy must allow access, either directly or by enabling IAM policies for the account. Identity policies alone are not enough for a key.
Exercises
Exercise
Account A's bucket policy allows s3:GetObject on arn:aws:s3:::amzn-s3-demo-bucket/* for the principal arn:aws:iam::444455556666:root. A role in account 444455556666 calls GetObject and gets AccessDenied. Its identity-based policies contain no S3 permissions. What is missing and why?
Show answer
Account 444455556666 must grant s3:GetObject to the role with an identity-based policy. For cross-account access, both the resource policy and the caller's identity policy must allow the action. The bucket policy only delegates to the account, so it is not enough alone.
Exercise
A statement allows s3:ListBucket with Resource set to arn:aws:s3:::amzn-s3-demo-bucket/*. Users still cannot list the bucket. What is the fix?
Show answer
s3:ListBucket is a bucket-level action, so its resource must be the bucket ARN arn:aws:s3:::amzn-s3-demo-bucket without /*. Object actions such as s3:GetObject use the /* form.