S3 security: Block Public Access, bucket policies, encryption

Most S3 data leaks aren’t clever hacks at all. Somebody opened a bucket “for a minute”, or a policy allowed more than anyone intended. An S3 bucket policy is where you write down the rules for a bucket: who can read, who can write, and what gets refused whatever else says yes.

You’ll switch on Block Public Access for the whole account, refuse plain HTTP, move the bucket to SSE-KMS and give a reader role access to a single prefix. After that you’ll run into the two errors everybody hits while writing these policies.

TimeYou needBy the end you can
50 minutesLesson 16 done, your laptop signed in as saat-admin, a recent AWS CLI. Cost: well under USD 0.01 (a few KMS and S3 requests).Set Block Public Access at account level, write a bucket policy that enforces TLS and gives a role access to one prefix, make SSE-KMS the default, and understand what 403 versus 404 is telling you.

What it is, in one line

An S3 bucket policy is a JSON document attached to a bucket that allows or denies actions on the bucket and its objects for whichever principals you name.

In Simple Words

The rules on the locker room door

In Lesson 16 your bucket was a bank locker, and a bucket policy is the notice pinned on the locker room door. It might say “Ravi from accounts may take files from the reports shelf” and “nobody comes in without showing ID at the desk”. Above that sits the bank manager’s own rule, Block Public Access: “No locker is ever open to the street, whatever the notice says”, so even a careless notice can’t override it. Encryption is the lock on each item, and with KMS the bank keeps that key in a separate safe and writes down every time it’s used.

Engineer's View

How S3 authorization really works

A bucket policy is a resource-based policy, so unlike an IAM identity policy it has a Principal. When a request comes in, S3 combines the caller’s IAM policies, the bucket policy, Block Public Access, SCPs (Lesson 33) and, for SSE-KMS objects, the KMS key policy, and an explicit Deny in any of them wins.

Inside one account, an Allow in either the IAM policy or the bucket policy is enough, but across accounts both sides have to allow. Bucket-level actions such as s3:ListBucket use the ARN arn:aws:s3:::bucket, and object actions such as s3:GetObject use arn:aws:s3:::bucket/*. Getting those two mixed up is the most common policy error there is.

A few condition keys you’ll use a lot: aws:SecureTransport to refuse HTTP, aws:SourceVpce to allow only your VPC endpoint, aws:PrincipalOrgID to allow only your organization, and s3:prefix to limit listing. Also, if the caller doesn’t have s3:ListBucket, S3 returns 403 instead of 404 for a missing key, so nobody can probe which keys exist.

How it works, step by step

This is what S3 checks when a request arrives.

1Block Public Access first

If a policy or ACL would make data public, Block Public Access either refuses to save it or ignores it. Set it at account level so that no single bucket can slip through.

S3 bucket policy evaluation flow: Block Public Access, explicit deny, allow, KMS

2Look for an explicit Deny

S3 collects every policy that applies, and a single matching Deny, like “refuse when aws:SecureTransport is false”, ends the request with AccessDenied.

3Look for an Allow

If nothing allows the request, that’s an implicit deny. In this lab the reader role has no IAM policy at all, so the only Allow it gets comes from the bucket policy.

4Check the KMS key

For SSE-KMS objects, S3 asks KMS to decrypt. The AWS managed key aws/s3 can be used through S3 by any principal in your account, while a customer managed key needs its own key policy (Lesson 30).

Here’s how the four controls fit together:

Four S3 security controls: Block Public Access, bucket policy, IAM policy, encryption

None of them is enough by itself. A perfect bucket policy won’t help if somebody later adds a public statement, and Block Public Access won’t stop an over-broad grant to another role.

✅ Checkpoint: You can explain why an explicit Deny wins, which ARN goes with ListBucket and which with GetObject, and why a missing key can come back as 403.

Hands-on lab: write an S3 bucket policy that holds

Before you run any command, double-check it

Read every command twice before you press Enter. Check the target: the system or account, the IDs, the names and the sizes. These commands are for learning and were tested in our lab, not on your system. At work, run the read-only list commands first, follow your change process, make sure a backup exists, and ask a second person to check anything that changes or deletes data. You run them at your own risk.

Sign in and set your variables:

cd C:\aws-lab
aws login --profile saat-admin
$env:AWS_PROFILE = 'saat-admin'
$acct = aws sts get-caller-identity --query Account --output text
$myArn = aws sts get-caller-identity --query Arn --output text
$bucket = "saat-sec-$acct-ap-south-1-an"

1Block Public Access for the whole account

Paste this whole block:

aws s3control put-public-access-block --account-id $acct --public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true
aws s3control get-public-access-block --account-id $acct --query PublicAccessBlockConfiguration --output text

You should see this:

True    True    True    True

From now on no bucket in this account can be made public, even by accident. Real companies do this on day one, usually backed by an SCP so nobody can turn it off.

2Create the bucket with SSE-KMS by default

Paste this whole block:

aws s3api create-bucket --bucket $bucket --bucket-namespace account-regional --region ap-south-1 --create-bucket-configuration LocationConstraint=ap-south-1 | Out-Null
aws s3api put-bucket-tagging --bucket $bucket --tagging 'TagSet=[{Key=course,Value=saatvedha-aws},{Key=lesson,Value=17}]'
'{ "Rules": [ { "ApplyServerSideEncryptionByDefault": { "SSEAlgorithm": "aws:kms" }, "BucketKeyEnabled": true } ] }' | Set-Content enc.json
aws s3api put-bucket-encryption --bucket $bucket --server-side-encryption-configuration file://enc.json
Set-Content -NoNewline q3.csv "quarter,revenue`nQ3,1825000`n"
Set-Content salaries.csv "name,salary"
aws s3 cp q3.csv "s3://$bucket/reports/q3.csv"
aws s3 cp salaries.csv "s3://$bucket/private/salaries.csv"
aws s3api head-object --bucket $bucket --key reports/q3.csv --query "[ServerSideEncryption,BucketKeyEnabled]" --output text

You should see something like this:

upload: .\q3.csv to s3://saat-sec-111122223333-ap-south-1-an/reports/q3.csv
upload: .\salaries.csv to s3://saat-sec-111122223333-ap-south-1-an/private/salaries.csv
aws:kms True

If you leave out the key ID, S3 uses the AWS managed key aws/s3. The bucket key cuts down KMS requests, and their cost, by using one short-lived data key per bucket instead of calling KMS for every object.

3Create a reader role with no permissions

Paste this whole block:

@'
{
  "Version": "2012-10-17",
  "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "MY_ARN" }, "Action": "sts:AssumeRole" } ]
}
'@ -replace 'MY_ARN', $myArn | Set-Content reader-trust.json
$roleArn = aws iam create-role --role-name saat-report-reader --assume-role-policy-document file://reader-trust.json --tags Key=course,Value=saatvedha-aws Key=lesson,Value=17 --query Role.Arn --output text
aws configure set role_arn $roleArn --profile saat-reader
aws configure set source_profile saat-admin --profile saat-reader
aws configure set region ap-south-1 --profile saat-reader
Start-Sleep 15
aws sts get-caller-identity --profile saat-reader --query Arn --output text

You should see something like this:

arn:aws:sts::111122223333:assumed-role/saat-report-reader/botocore-session-1760951234

The saat-reader profile assumes the role using your saat-admin sign-in. There’s no IAM policy on the role, so for now it can’t read anything. The 15-second pause gives IAM time to make the new role visible everywhere.

4Write the bucket policy

Paste this whole block:

@'
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "DenyInsecureTransport",
      "Effect": "Deny",
      "Principal": "*",
      "Action": "s3:*",
      "Resource": ["arn:aws:s3:::BUCKET", "arn:aws:s3:::BUCKET/*"],
      "Condition": { "Bool": { "aws:SecureTransport": "false" } }
    },
    {
      "Sid": "ReaderGetReports",
      "Effect": "Allow",
      "Principal": { "AWS": "ROLE_ARN" },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::BUCKET/reports/*"
    }
  ]
}
'@ -replace 'BUCKET', $bucket -replace 'ROLE_ARN', $roleArn | Set-Content policy.json
aws s3api put-bucket-policy --bucket $bucket --policy file://policy.json
aws s3 ls "s3://$bucket" --endpoint-url http://s3.ap-south-1.amazonaws.com

The last command fails, and it’s supposed to:

An error occurred (AccessDenied) when calling the ListObjectsV2 operation: User: arn:aws:iam::111122223333:user/yourname.admin is not authorized to perform: s3:ListBucket on resource: "arn:aws:s3:::saat-sec-111122223333-ap-south-1-an" with an explicit deny in a resource-based policy

Even you, as an administrator, get refused over plain HTTP, because the explicit Deny beats your AdministratorAccess.

S3 bucket policy lab output: HTTP denied, SSE-KMS, reader role access
HTTP refused, SSE-KMS confirmed and the reader limited to reports. Yours will look much like this.

5Test as the reader

Paste this whole block:

aws s3 cp "s3://$bucket/reports/q3.csv" - --profile saat-reader
aws s3 cp "s3://$bucket/private/salaries.csv" - --profile saat-reader

You should see this:

quarter,revenue
Q3,1825000
fatal error: An error occurred (403) when calling the HeadObject operation: Forbidden

The - at the end prints the file to the screen instead of saving it. The reader gets the reports, decrypted through KMS, and nothing more.

✅ Checkpoint: Account-level Block Public Access shows True four times, objects are aws:kms with a bucket key, HTTP is refused even for you, and saat-reader can read reports/q3.csv but not private/salaries.csv.

Break It, Then Fix It

Two mistakes that eat up hours of an engineer’s day: the wrong ARN in a statement, and forgetting that reading and listing are separate permissions.

1Break it: object action on the bucket ARN

Try adding a statement that uses the bucket ARN for GetObject:

@'
{
  "Version": "2012-10-17",
  "Statement": [ { "Effect": "Allow", "Principal": { "AWS": "ROLE_ARN" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::BUCKET" } ]
}
'@ -replace 'BUCKET', $bucket -replace 'ROLE_ARN', $roleArn | Set-Content bad-policy.json
aws s3api put-bucket-policy --bucket $bucket --policy file://bad-policy.json

You’ll get this error:

An error occurred (MalformedPolicy) when calling the PutBucketPolicy operation: Action does not apply to any resource(s) in statement

GetObject works on objects, but here the resource is the bucket, so the statement could never match anything. S3 rejects it and your good policy stays as it was. Fix: object actions take arn:aws:s3:::bucket/* (or a prefix like /reports/*), while bucket actions such as ListBucket take arn:aws:s3:::bucket.

2Break it: ask for a file that is not there

As the reader, ask for a report that isn’t there:

aws s3 cp "s3://$bucket/reports/q4.csv" - --profile saat-reader

You’ll get this error:

fatal error: An error occurred (403) when calling the HeadObject operation: Forbidden
S3 bucket policy errors: MalformedPolicy and 403 instead of 404 with fixes
Both errors and how to fix them. Yours will look much like this.

The file doesn’t exist, and still S3 says 403 rather than 404, because without s3:ListBucket S3 won’t confirm whether a key exists. Teams lose hours checking permissions on files that were never uploaded in the first place.

Fix: give the reader ListBucket, limited to the reports prefix. Paste this whole block:

$p = Get-Content policy.json -Raw | ConvertFrom-Json
$list = [pscustomobject]@{ Sid = 'ReaderListReports'; Effect = 'Allow'; Principal = @{ AWS = $roleArn }; Action = 's3:ListBucket'; Resource = "arn:aws:s3:::$bucket"; Condition = @{ StringLike = @{ 's3:prefix' = 'reports/*' } } }
$p.Statement += $list
$p | ConvertTo-Json -Depth 10 | Set-Content policy.json
aws s3api put-bucket-policy --bucket $bucket --policy file://policy.json
aws s3 ls "s3://$bucket/reports/" --profile saat-reader
aws s3 cp "s3://$bucket/reports/q4.csv" - --profile saat-reader

You should see something like this:

2026-10-20 15:05:41         27 q3.csv
fatal error: An error occurred (404) when calling the HeadObject operation: Key "reports/q4.csv" does not exist

Now the error is honest: the key doesn’t exist. Clean up everything from this lesson, except account-level Block Public Access, which stays on as it should:

aws s3 rm "s3://$bucket" --recursive
aws s3 rb "s3://$bucket"
aws iam delete-role --role-name saat-report-reader
Remove-Item q3.csv, salaries.csv, enc.json, reader-trust.json, policy.json, bad-policy.json
notepad $HOME\.aws\config
.\Find-LabResources.ps1

In Notepad, delete the three lines under [profile saat-reader] and save the file.

✅ Checkpoint: You fixed MalformedPolicy by using the right ARN, and turned a confusing 403 into an honest 404 with a prefix-limited ListBucket. The lesson 17 bucket, role and profile are gone.

This happened at work

The policy that locked out the admins

A fintech company wanted its statements bucket reachable only from inside its VPC, so an engineer added a bucket policy that denied s3:* to everyone unless aws:SourceVpce matched their endpoint. He tested it from an EC2 instance, it worked, and it went live on a Friday evening. By Monday morning the nightly export from the payments partner had failed three times, the CI pipeline couldn’t publish reports, and the security team couldn’t even read the bucket policy from their laptops. The Deny also covered s3:GetBucketPolicy and s3:PutBucketPolicy, so no administrator outside the VPC could fix it.

In the end the account owners signed in as root, which can always delete a bucket policy, and took it off. The new version denies only object actions and uses aws:PrincipalArn to leave out the partner’s role and a break-glass admin role, and every bucket policy change now goes through a pull request with IAM Access Analyzer checks.

Common mistakes and troubleshooting

MistakeWhat happensWhat to do
Object action with the bucket ARNMalformedPolicy: Action does not apply to any resource(s)Use bucket/* for objects and the bucket ARN for ListBucket
Deny s3:* with a VPC endpoint conditionAdmins and partners get locked outDeny only object actions, and exclude break-glass roles
No ListBucket for readersMissing files show 403 and people waste hoursGrant ListBucket with an s3:prefix condition
Relying on bucket-level Block Public Access onlyA new bucket can still be made publicTurn it on at account level and protect that with an SCP
Creating the policy before the role existsInvalid principal in policyCreate the role first, then wait a few seconds
Principal “*” with Allow and no conditionThe bucket is open to anyoneBlock Public Access stops it; name the exact principals instead

Interview questions

Click a question to open it. They’re worded the way an interviewer would ask them, and under each one you’ll find why they ask it and one way to answer, which you should put in your own words.

Fresher level

1. Can you explain the difference between a bucket policy and an IAM policy in simple words?

Why they ask this: These are the two policy types that control S3 access, and most S3 access problems involve one of them, or both.

How you might answer: A bucket policy sits on the bucket and names who can get in. An IAM policy sits on a user, group or role and says what that identity is allowed to do. S3 looks at both when it decides on a request.

2. How would you explain Block Public Access to a developer who just wants their bucket to work?

Why they ask this: It’s basic S3 security awareness, and they also want to see you can explain a guardrail without coming across as the person who blocks everything.

How you might answer: I’d call it a safety switch. At bucket or account level it stops public ACLs and public bucket policies from taking effect, even when somebody writes one by mistake.

3. Your security team says all S3 access must use HTTPS. How would you enforce that on a bucket?

Why they ask this: It’s one of the most common bucket policy patterns and it turns up in almost every security review.

How you might answer: I’d add a Deny statement for s3:* on the bucket and its objects with the condition aws:SecureTransport equal to false, so anything over plain HTTP gets denied.

2–5 years experience

4. A user gets a 403 error for a file that doesn’t even exist. Why would S3 do that?

Why they ask this: It’s real troubleshooting knowledge. This one confuses a lot of people and sends them chasing the wrong problem.

How you might answer: If the user doesn’t have s3:ListBucket, S3 returns 403 for keys that don’t exist, so callers can’t work out which keys are there. Once I grant ListBucket, the same request comes back as 404.

5. What’s the difference between SSE-S3 and SSE-KMS, and when would you choose KMS?

Why they ask this: They want to see you understand encryption choices and what they cost, since KMS without bucket keys can push the bill up.

How you might answer: With SSE-S3 the keys are managed by S3 and I don’t get any extra control. SSE-KMS uses a KMS key, so I get key policies, a CloudTrail record of every use and the option to disable the key. I’d pick KMS when we need that control or audit trail, and since it adds KMS request costs, I’d switch on bucket keys to keep them down.

6. A team in another AWS account needs to read files from your bucket. How would you set that up?

Why they ask this: They’re checking you know cross-account access needs both sides to agree. Forgetting the KMS part is the usual slip.

How you might answer: I’d allow the other account’s role in my bucket policy, and they’d allow the action in that role’s IAM policy, because both sides have to say yes. If the objects use a customer managed KMS key, the key policy has to allow that role as well.

Senior level

7. You join a regulated company, like a bank. How would you design S3 data protection there?

Why they ask this: They’re looking for defence in depth, because in regulated work one layer of protection is never enough.

How you might answer: I’d turn on account-level Block Public Access, enforce it with an SCP, and use SSE-KMS with customer managed keys for each data class. Bucket policies would allow only TLS and only our organization, with VPC endpoint policies as another layer. Records would get versioning and Object Lock, Access Analyzer and Config rules would catch drift, and CloudTrail data events would cover the sensitive buckets.

8. Someone pushed a bucket policy that locked everyone out, including admins. What do you do?

Why they ask this: They want calm incident handling and a plan to stop a repeat, and the way out of an S3 lockout isn’t obvious.

How you might answer: I’d sign in as the account root user, which can always delete a bucket policy, remove it, and then rebuild a scoped version with exceptions for break-glass roles. After that I’d make sure policy changes go through code review and policy validation so it can’t happen the same way again.

Quick quiz

Q1. Which resource ARN does s3:GetObject need?

  1. arn:aws:s3:::bucket
  2. arn:aws:s3:::bucket/*
  3. arn:aws:iam::bucket
  4. Any ARN
Show answer

B: Object actions apply to objects, so the ARN ends with /* or a prefix.

Q2. Which condition key refuses plain HTTP?

  1. aws:SourceIp
  2. aws:SecureTransport
  3. s3:prefix
  4. aws:PrincipalOrgID
Show answer

B: Deny the request when aws:SecureTransport is false.

Q3. Why did the reader get 403 for a missing file?

  1. The bucket was public
  2. It lacked s3:ListBucket
  3. KMS was disabled
  4. The file was encrypted
Show answer

B: Without ListBucket, S3 won’t reveal whether a key exists.

Q4. In one account, what does a role need to read an object?

  1. Allows in both IAM and bucket policy
  2. An Allow in either, and no explicit Deny
  3. Root access
  4. A public bucket
Show answer

B: In the same account one Allow is enough, unless a Deny applies.

Q5. Who can always delete a bucket policy that locked everyone out?

  1. Any IAM user
  2. The account root user
  3. AWS Support
  4. Nobody
Show answer

B: The root user can delete a bucket policy even when that policy denies everyone.

Quick recap

  • Switch on Block Public Access at account level before anything else.
  • An explicit Deny beats every Allow, administrators included.
  • Object actions use bucket/*, and ListBucket uses the bucket ARN.
  • If the caller lacks ListBucket, missing keys return 403 instead of 404.
  • SSE-KMS with a bucket key gives you key control and an audit trail at low cost.

Next lesson

Lesson 18: S3 versioning, lifecycle rules and replication → You’ll get deleted files back with versioning, cut costs with lifecycle rules and copy data to another Region.

Further reading: S3 bucket policy

Want to go deeper into S3 bucket policy? Read Examples of Amazon S3 bucket policies and Blocking public access to your Amazon S3 storage. You can also see the full plan for this course on the AWS Cloud Administration course page.

Scroll to Top

This website uses cookies

We use a few cookies to keep this site working. If you allow it, we also use Google Analytics to count visits so we can improve our lessons. You can accept all, deny the optional ones, or choose what to allow.