Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 70 ยท Storage with S3

S3 Security

Secure Amazon S3 buckets: Block Public Access, bucket policies, why ACLs are legacy, SSE-S3 and SSE-KMS encryption, access points and Object Lock, with runnable evaluators.

S3 buckets are private by default, yet "open S3 bucket" has been the headline of many data breaches. The cause is almost never a flaw in S3. It is a human attaching a policy or an ACL that says "everyone" and a team that did not notice. This lesson gives you the layers AWS provides to prevent that, and the tools to keep data confidential, intact and recoverable.

The real service cannot run here, so every console setting, CLI command and JSON policy is read-only. The runnable parts are Python stand-ins: a bucket-policy evaluator that models explicit deny, allow, implicit deny and Block Public Access, and an Object Lock model. They simplify AWS in order to show the logic; the real evaluation also involves IAM identity policies, organization policies and more (How Policy Evaluation Works covers the full order).

Layer 1: Block Public Access

S3 Block Public Access (BPA) is a safety net that sits above policies and ACLs. It has four independent settings, applicable to a single bucket, to an access point or to the whole account:

Setting Effect
BlockPublicAcls Rejects requests that add public ACLs
IgnorePublicAcls Ignores any existing public ACLs
BlockPublicPolicy Rejects bucket policies that grant public access
RestrictPublicBuckets Limits access through an existing public policy to AWS service principals and authorized users of the account

Since April 2023, new buckets are created with all four turned on and with ACLs disabled. The recommendation is to enable BPA at the account level so no bucket can become public by mistake, and to turn it off for a specific bucket only when you truly serve public content, ideally through CloudFront instead (CloudFront CDN).

Terminal
aws s3control put-public-access-block \
  --account-id 111122223333 \
  --public-access-block-configuration \
  BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true

Layer 2: bucket policies

A bucket policy is a resource-based JSON policy attached to a bucket. It uses the same grammar as IAM policies (Writing IAM Policies) with one difference: it must name a Principal. Common patterns:

JSON
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "DenyInsecureTransport",
      "Effect": "Deny",
      "Principal": "*",
      "Action": "s3:*",
      "Resource": ["arn:aws:s3:::acme-data", "arn:aws:s3:::acme-data/*"],
      "Condition": { "Bool": { "aws:SecureTransport": "false" } }
    },
    {
      "Sid": "AppCanReadWrite",
      "Effect": "Allow",
      "Principal": { "AWS": "arn:aws:iam::111122223333:role/app" },
      "Action": ["s3:GetObject", "s3:PutObject"],
      "Resource": "arn:aws:s3:::acme-data/uploads/*"
    }
  ]
}

Two details deserve attention. The bucket ARN (arn:aws:s3:::acme-data) covers bucket-level actions such as s3:ListBucket, while object actions need the /* form. And the evaluation rule is always the same: any matching Deny wins, otherwise a matching Allow grants access, otherwise the result is an implicit deny.

Bucket policies are also the way to grant cross-account access and to enforce rules for everyone, for instance "all traffic must use TLS" or "uploads must be encrypted".

ACLs are legacy

Access control lists are the original S3 permission mechanism, predating IAM. They grant coarse rights per object or bucket to accounts or predefined groups such as "Authenticated Users" or "Everyone". They cannot express conditions and are easy to misread. The modern default is the Object Ownership setting BucketOwnerEnforced, which disables ACLs entirely, so the bucket owner owns every object and policies are the only permission mechanism. Keep ACLs off unless a specific legacy workflow requires them, such as some older log-delivery setups.

The policy evaluator, runnable

The stand-in below takes a bucket policy and a few requests. It supports wildcard matching in actions and resources, principals (* or specific ARNs, including an account root), the Bool and StringNotEquals operators, and a Block Public Access switch that rejects public policies when they are attached.

Python A bucket-policy evaluator with Block Public Access

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Look at the results line by line. The TlsOnly deny beats the public allow, a missing allow gives an implicit deny (the app cannot list the bucket), and the encryption rule turns an otherwise valid upload into a denial. The KMS rule works because the missing header makes a StringNotEquals condition true.

Layer 3: encryption

Encryption in transit is TLS; deny requests with aws:SecureTransport set to false, as above.

Encryption at rest is applied on the server side. Since January 2023, every new object is encrypted by default with SSE-S3 at no cost, so unencrypted objects no longer exist. The options:

  • SSE-S3: S3 manages the keys (AES-256). Simplest, no extra permissions.
  • SSE-KMS: keys live in AWS KMS. You choose an AWS managed key or your own customer managed key, control its key policy, and get an audit trail in CloudTrail for every use. Each request that needs the key counts as a KMS call with its own cost and quota, so enable S3 Bucket Keys to cut those calls sharply. Callers need both S3 and KMS permissions. Encryption and KMS goes deeper.
  • DSSE-KMS: two layers of KMS-based encryption for strict compliance needs.
  • SSE-C: you supply the key on every request. AWS does not store it, so losing it means losing the data, and it is rarely the right choice.
  • Client-side encryption: you encrypt before upload and S3 only sees ciphertext.

Setting a bucket default (aws s3api put-bucket-encryption) chooses what new objects without an explicit setting receive.

Layer 4: access points and Object Lock

An access point is a named endpoint attached to a bucket, with its own policy and optionally restricted to a VPC. Instead of one giant bucket policy shared by dozens of teams, each team or application gets an access point whose policy only covers its prefix, for example arn:aws:s3:us-east-1:111122223333:accesspoint/analytics. The bucket policy then delegates to access points with a single statement.

Object Lock gives write-once-read-many protection. It needs versioning and, for new buckets, is enabled at creation. A locked version cannot be overwritten or deleted during its retention period:

  • Governance mode: users with a special permission (s3:BypassGovernanceRetention) and a bypass header can still delete. Good for protecting against mistakes.
  • Compliance mode: nobody, including the root user, can shorten the retention or delete the version until it expires. Meant for regulation, and impossible to undo, so test with governance mode first.
  • Legal hold: an on/off flag with no end date, independent of retention, removed by someone with s3:PutObjectLegalHold.
flowchart TD
    D["Delete version request"] --> H{"Legal hold on?"}
    H -->|"yes"| X["Denied"]
    H -->|"no"| R{"Retention active?"}
    R -->|"no"| OK["Deleted"]
    R -->|"yes"| M{"Mode"}
    M -->|"compliance"| X
    M -->|"governance"| B{"Bypass permission<br/>and header?"}
    B -->|"yes"| OK
    B -->|"no"| X

Deleting a locked version succeeds only when no legal hold applies and either retention has ended or governance mode is bypassed.

Python Object Lock retention and legal hold

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

A practical checklist

  1. Turn on Block Public Access for the whole account; allow exceptions deliberately.
  2. Disable ACLs with BucketOwnerEnforced.
  3. Grant access through IAM roles and narrow bucket policies; use access points for many consumers.
  4. Deny non-TLS requests, and require KMS where policy demands it.
  5. Enable versioning (and Object Lock where needed) for data you cannot lose; turn on logging with CloudTrail data events for sensitive buckets.
  6. Use IAM Access Analyzer for S3 to find buckets shared outside your account.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which Block Public Access setting stops someone from attaching a bucket policy that grants access to everyone?

    BlockPublicPolicy rejects public bucket policies when they are submitted. RestrictPublicBuckets limits access under an already existing public policy.
  2. 2. A policy has an Allow for a user and a matching explicit Deny from a bucket policy. What is the outcome?

    In AWS policy evaluation any matching explicit Deny wins over every Allow.
  3. 3. What is the main difference between governance and compliance mode in Object Lock?

    Governance mode can be bypassed with a special permission and header. Compliance retention cannot be shortened or removed by anyone, including the root user.
  4. 4. Why enable S3 Bucket Keys with SSE-KMS?

    A bucket-level key generates data keys inside S3, so far fewer calls are made to KMS for busy buckets.

Summary

  • Buckets are private by default; Block Public Access at the account level prevents accidental exposure.
  • Bucket policies are resource policies: explicit deny beats allow, and no match means implicit deny.
  • ACLs are legacy; disable them with Bucket owner enforced ownership.
  • All new objects are encrypted at rest; choose SSE-KMS when you need key control and audit trails, and enforce TLS with a deny condition.
  • Access points split access per consumer; Object Lock adds write-once protection with governance, compliance and legal hold.

Next: Static Websites and S3 Events.