S3 bucket security – Block Public Access, policies, KMS, TLS, Object Lock, OAC and presigned URLs (CDK, JSON)
We secure the pierogi shop's buckets layer by layer – Block Public Access and disabled ACLs, a bucket policy enforcing TLS, the KMS key and the organization perimeter, SSE-KMS encryption with Bucket Keys, versioning, MFA Delete and Object Lock, lifecycle rules, CloudFront with Origin Access Control, hardened presigned URLs, plus logs, CloudTrail, Macie and GuardDuty. Code in AWS CDK (TypeScript) and JSON policies.
The pierogi shop's accountant asked a simple question: "Are those invoices in the cloud safe?". The honest answer: S3 itself is very secure, but most high-profile S3 leaks were not break-ins into AWS – they were configuration: a public bucket, an overly broad role, a leaked key. In this tutorial we secure the buckets layer by layer, so that a mistake in one layer is not enough for a disaster.
Prerequisites: S3 basics from the S3 tutorial (buckets, presigned URLs, CloudFront), AWS CLI v2, Node.js 22+ and AWS CDK v2. Knowing IAM and least privilege helps.
- 2023
- since April – new buckets: Block Public Access on, ACLs disabled
- 2026
- since April – SSE-C blocked by default on new buckets
- up to 99%
- fewer KMS requests – thanks to S3 Bucket Keys
- 7 days
- max presigned URL validity – with a role – shorter, until the session expires
Threat model: what actually goes wrong
| Threat | Real example | Control |
|---|---|---|
| Public bucket | in 2017 UpGuard described a series of leaks from public buckets, including Verizon customer data held by a partner | account-level Block Public Access, CloudFront with OAC for public content |
| Hijacked role with broad permissions | the classic chain: SSRF in an app on EC2 → role credentials from the metadata service (IMDSv1) → bulk download of S3 data; incidents like this are why AWS introduced IMDSv2 | IMDSv2, least privilege, data perimeter, GuardDuty |
| Leaked access key + ransomware | the "Codefinger" campaign (January 2025, described by Halcyon): objects encrypted with SSE-C using the attacker's key plus a 7-day deletion rule | no long-lived keys, SSE-C blocked, versioning, Object Lock |
| Operator error | aws s3 rm --recursive on the wrong prefix | versioning, Object Lock, backups (AWS Backup), restricted DeleteObject |
| Leaked link | a presigned URL in logs, analytics or the Referer header | short lifetime, a limit in the bucket policy, Referrer-Policy: no-referrer |
Layer 1. Block Public Access and Object Ownership
Block Public Access (BPA) is four switches that override everything else: block public ACLs, ignore existing public ACLs, block public policies and restrict access to buckets with a public policy. You set them per bucket and – more importantly – at the account level (aws s3control put-public-access-block). Then even a hand-made bucket with BPA switched off cannot become public. In an AWS organization, add an SCP that forbids changing these settings.
Object Ownership = Bucket owner enforced disables ACLs. They are the old per-object permission mechanism that caused mistakes for years (public-read on upload). With ACLs disabled, the bucket owner owns every object and access is decided by policies alone. Since April 2023 this is the default for new buckets – switch old buckets once you are sure nothing relies on ACLs.
| Mechanism | Where it lives | What it's for today |
|---|---|---|
| IAM (identity) policy | role, user, group | what the app, people and CI may do |
| Bucket (resource) policy | bucket | the bouncer: denies for everyone, access for other accounts and services (CloudFront) |
| ACL | the bucket and every object | nothing – disable it (Bucket owner enforced) |
| KMS key policy | key | who may encrypt and decrypt – a second, independent gate |
| SCP / RCP (organization) | accounts in AWS Organizations | boundaries nobody in the account can cross |
Step 1. A secure bucket in CDK
The SecureBucket construct (AWS CDK)
import { Construct } from 'constructs';
import { Duration, RemovalPolicy } from 'aws-cdk-lib';
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as kms from 'aws-cdk-lib/aws-kms';
import * as cloudtrail from 'aws-cdk-lib/aws-cloudtrail';
export interface SecureBucketProps {
/** Object Lock retention for regulated data (e.g. invoices), in days. */
lockDays?: number;
logsBucket: s3.IBucket;
trail?: cloudtrail.Trail;
}
/** A private, encrypted, versioned bucket with TLS-only access, audit logs and sane lifecycle rules. */
export class SecureBucket extends Construct {
readonly bucket: s3.Bucket;
readonly key: kms.Key;
constructor(scope: Construct, id: string, props: SecureBucketProps) {
super(scope, id);
this.key = new kms.Key(this, 'Key', {
enableKeyRotation: true, // yearly rotation of the key material
description: `SSE-KMS for ${id}`,
removalPolicy: RemovalPolicy.RETAIN,
});
this.bucket = new s3.Bucket(this, 'Bucket', {
blockPublicAccess: s3.BlockPublicAccess.BLOCK_ALL,
objectOwnership: s3.ObjectOwnership.BUCKET_OWNER_ENFORCED, // ACLs disabled
encryption: s3.BucketEncryption.KMS,
encryptionKey: this.key,
bucketKeyEnabled: true, // far fewer KMS requests (and a smaller KMS bill)
blockedEncryptionTypes: [s3.BlockedEncryptionType.SSE_C],
enforceSSL: true, // Deny when aws:SecureTransport = false
minimumTLSVersion: 1.2,
versioned: true,
objectLockDefaultRetention: props.lockDays
? s3.ObjectLockRetention.governance(Duration.days(props.lockDays))
: undefined,
serverAccessLogsBucket: props.logsBucket,
serverAccessLogsPrefix: `${id}/`,
lifecycleRules: [
{ id: 'abort-mpu', abortIncompleteMultipartUploadAfter: Duration.days(7) },
{ id: 'old-versions', noncurrentVersionExpiration: Duration.days(90), noncurrentVersionsToRetain: 3 },
{ id: 'delete-markers', expiredObjectDeleteMarker: true },
],
removalPolicy: RemovalPolicy.RETAIN,
});
// CloudTrail data events: who wrote and deleted which object (reads are optional – and cost more).
props.trail?.addS3EventSelector([{ bucket: this.bucket }], {
readWriteType: cloudtrail.ReadWriteType.WRITE_ONLY,
includeManagementEvents: false,
});
}
}One construct, used for every bucket in the project, is the best protection against "that one bucket someone created differently". A few decisions deserve a comment:
enforceSSLandminimumTLSVersion– CDK adds denies foraws:SecureTransport = falseands3:TlsVersion < 1.2to the bucket policy. This is control S3.5 of the AWS Foundational Security Best Practices in Security Hub.blockedEncryptionTypes: [SSE_C]– since April 2026 S3 blocks SSE-C by default on new buckets, but setting it explicitly in code documents the intent and protects older buckets.bucketKeyEnabled– instead of calling KMS on everyPutObjectandGetObject, S3 uses a short-lived intermediate key. Fewer requests, a smaller bill, less risk of hitting KMS limits.- Object Lock only where needed – invoices get 30 days of governance retention; menu photos don't.
- CloudTrail for writes only – data events are billed per event. Invoice writes and deletes are rare and valuable in an investigation; reads of menu photos are numerous and unimportant.
The rest of this article is for members
Create a free account or sign in to read it all. Still ahead:
- Step 2. The bucket policy as a bouncer
- Encryption: SSE-S3, SSE-KMS, DSSE-KMS and SSE-C
- Versioning, MFA Delete and Object Lock
- Lifecycle: housekeeping is security too
- CloudFront with OAC instead of a public bucket
- Presigned URLs: a bearer ticket
- Logs and detection
- A five-minute audit
- Common mistakes
- Test yourself
- What's next