S3 bucket policy vs IAM policy
An IAM policy is attached to a user or role and says what that identity can do; a bucket policy is attached to the bucket and says who can access it, so it has a Principal. Within one account either is enough on its own. Across accounts, both the bucket policy and the caller's IAM policy must allow the request.
A cross-account example that needs both
Account 111122223333 runs a role called analytics that must read my-bucket in account 123456789012. The bucket owner adds this bucket policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AnalyticsList",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::111122223333:role/analytics"
},
"Action": "s3:ListBucket",
"Resource": "arn:aws:s3:::my-bucket"
},
{
"Sid": "AnalyticsRead",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::111122223333:role/analytics"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-bucket/*"
}
]
}And account 111122223333 attaches an IAM policy to the analytics role allowing the same two actions on the same two ARNs (see read-only access to one bucket). Remove either half and the request is denied.
How they combine
| Situation | What has to allow it |
|---|---|
| Caller and bucket in the same account | The IAM policy or the bucket policy |
| Caller in another account | The bucket policy and the caller's IAM policy |
| Anonymous (public) access | Only a bucket policy can grant it, and Block Public Access must be off |
Any explicit Deny in either | Denied, whatever else allows it |
SCPs, resource control policies, permissions boundaries, session policies and VPC endpoint policies can still deny a request that both documents allow.
When to use which
- Use IAM policies for "what can this role do across many buckets": the access belongs to the job. You can see everything a role may do in one place.
- Use a bucket policy for rules about the bucket itself, whoever the caller is: cross-account access, CloudFront origin access control, deny non-TLS requests, only via a VPC endpoint, or require a KMS key. Deny statements here protect the data even from admins in your own account.
Size limits differ too: a bucket policy can be up to 20 KB, while a customer managed IAM policy is limited to 6,144 characters (excluding whitespace), so very long lists of ARNs fit better on the bucket.
Apply a bucket policy
aws s3api put-bucket-policy --bucket my-bucket \
--policy file://bucket-policy.json
aws s3api get-bucket-policy --bucket my-bucket --query Policy --output textIn the console it is under the bucket's Permissions tab → Bucket policy. The console runs IAM Access Analyzer policy checks as you type, which catches most syntax problems.
Common mistakes
- No Principal. A bucket policy statement without
Principalis rejected as malformed. IAM policies must not have one. "Principal": "*"with an Allow. That is public access unless a condition narrows it. Block Public Access will refuse it on most accounts.- Granting the account root ARN and stopping there.
arn:aws:iam::111122223333:rootdelegates to that account; its admins still have to grant their roles in IAM. - Objects written by another account. With ACLs enabled, the uploader can own the object and your bucket policy won't cover it. Set Object Ownership to bucket owner enforced.
- KMS keys. Cross-account reads of SSE-KMS objects also need the key policy to allow the other account.
Questions
Do I need both a bucket policy and an IAM policy?
Only for cross-account access. In the same account, an allow in either one is enough, as long as nothing explicitly denies the request.
What is the Principal in an S3 bucket policy?
The identity the statement applies to: an account, a role or user ARN, a service principal such as cloudfront.amazonaws.com, or * for everyone.
Which wins, bucket policy or IAM policy?
Neither wins outright. An explicit Deny in either always wins; otherwise the request needs the allows described above.
Bucket access questions often arrive as a message from another team at an awkward hour. Being able to read the bucket policy and recent CloudTrail events from your phone settles most of them quickly.
About the app → · Download on the App Store