Security and bucket policies
How access to S3 is decided, IAM policies vs bucket policies vs ACLs, bucket policy examples, and Block Public Access.
Two kinds of permission
Access to S3 can be granted in two places:
- User-based: IAM policies. Attached to a user, group or role. They say which S3 API calls that identity may make.
- Resource-based: attached to the bucket or object.
- Bucket policies: JSON rules on the bucket, set in the S3 console. They can grant access to other accounts and to the public.
- Object ACLs and bucket ACLs: an older, finer-grained system. Since 2023, new buckets have ACLs disabled (Object Ownership is Bucket owner enforced), and AWS recommends keeping them off.
- Encryption protects the data itself (see Encryption).
How S3 decides
Within one account, an IAM principal can access an S3 object if:
- No policy explicitly denies it (an explicit
Denyalways wins), and - Either its IAM policy or the bucket policy allows it.
If nothing allows the request, it's denied by default. (Across accounts, both sides must allow; see cross-account access.)
Bucket policies
A bucket policy is a JSON document with:
- Resource: the bucket and/or objects it covers (
arn:aws:s3:::examplebucket/*is every object). - Effect:
AlloworDeny. - Action: the API calls, such as
s3:GetObject. - Principal: who it applies to: an account, user, role, service, or
*(everyone).
This policy lets anyone read every object, the classic public website policy:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicRead",
"Effect": "Allow",
"Principal": "*",
"Action": ["s3:GetObject"],
"Resource": ["arn:aws:s3:::examplebucket/*"]
}
]
}Use bucket policies to:
- grant public access to a bucket;
- force encryption of objects at upload, or force HTTPS (see Encryption);
- grant access to another account (cross-account).
Who gets access through what
- Anonymous website visitor → a bucket policy allowing public
GetObject(and Block Public Access off). - IAM user in the same account → an IAM policy on the user.
- EC2 instance → an IAM role attached to the instance. Never put access keys on an instance.
- IAM user in another account → a bucket policy that allows that account (plus an IAM policy on their side).
Block Public Access
Block Public Access is a safety switch that overrides policies and ACLs to stop data leaks. It has four settings:
- block public access granted through new ACLs;
- block public access granted through any ACLs;
- block public access granted through new public bucket or access point policies;
- block public and cross-account access through any public bucket or access point policies.
All four are on by default for new buckets. If a bucket should never be public, leave them on. They can also be set for the whole account or for every account in an organization, which beats any bucket setting.
Real-world examples
- A company leaks customer files because someone made a bucket public. Turning on Block Public Access at the account level would have stopped it, whatever the bucket policy said.
- An app on EC2 reads images from a private bucket through its instance role, with no keys stored anywhere.
- A partner company gets read access to one prefix of a bucket through the bucket policy, naming the partner's account ID as the principal.
Exam tips
- "Give an EC2 instance access to S3" → IAM role (instance profile), not access keys.
- "Grant access to another AWS account" → bucket policy (cross-account).
- "Make objects publicly readable" → bucket policy with
Principal: "*"and Block Public Access off. - "Prevent any bucket in the account from ever being public" → Block Public Access at the account (or organization) level.
- "Explicit deny in a bucket policy vs allow in IAM" → deny wins.
Common traps
- "Use ACLs to manage access." ACLs are disabled by default; use policies.
- "A bucket policy allows public read, so it's public." Not if Block Public Access is on.
- "An IAM allow overrides a bucket policy deny." An explicit deny always wins.
Hands-on lab: a bucket policy and Block Public Access
Goal: try to make one object public, see Block Public Access stop it, then allow it.
Costs money
A few requests and one small object cost less than a cent. Any Region works.
Before you start: use the bucket and image from the first bucket lab.
Try the public URL
Open the object and copy its Object URL. Open it in a private browser window: AccessDenied. The bucket is private.
Try to add a public policy
Bucket → Permissions → Bucket policy → Edit. Paste the public-read policy above, changing examplebucket to your bucket name and the resource to arn:aws:s3:::<your-bucket>/photos/*. Save: S3 refuses, because Block Public Access is on.
Turn off Block Public Access for this bucket
Permissions → Block public access → Edit, untick Block all public access, confirm. Save the bucket policy again: it works now. Reload the Object URL: the image loads.
Expected result: the object is only public once both the bucket policy allows it and Block Public Access is off.
Clean up: delete the bucket policy and turn Block all public access back on.