Private S3 bucket with encryption, versioning and a TLS-only policy
A bucket for uploads, exports or backups that cannot be made public by mistake, keeps earlier copies of changed files, and cleans up old copies so the bill does not grow without limit.
Application
TLS request
Bucket policy
Encrypted object
How it works
How bucket data ends up exposed
A bucket is private when created, and it stays private until someone adds a public policy or ACL to make one file shareable. That change is easy to make and easy to forget. This pattern turns on the settings that make S3 refuse such a change, and adds the ones that let you undo a bad overwrite or delete.
Block public access and switch off ACLs
The four public access block settings stop public policies and ACLs from taking effect on this bucket. Object ownership is set to bucket owner enforced, which disables ACLs, so access is decided in one place: IAM and the bucket policy.
Encrypt at rest and require TLS
Default encryption applies to every object written to the bucket. The bucket policy denies any request where aws:SecureTransport is false, so data is not read or written over plain HTTP. Pass kms_key_arn if you want your own KMS key and a second permission check on every read.
Turn on versioning, with a cleanup rule
With versioning, an overwrite or delete keeps the previous copy for 30 days. The mistake people make is turning on versioning with no lifecycle rule: every old version is then stored and billed forever. The rule here also removes the parts of uploads that never finished.
Give your application access by role
Nothing can read the bucket until an IAM policy allows it. Grant the role your application runs as only the actions it uses, such as s3:GetObject and s3:PutObject, on the bucket_arn output. To share a file with a user, generate a presigned URL in your application and leave the bucket private.
What you set
bucket_name
Globally unique name for the bucket For example acme-prod-uploads
region
Region to create the bucket in For example us-east-1
noncurrent_version_days
Days to keep an overwritten or deleted object For example 30
What it costs to run
Depends on how much you store: about 2 to 3 cents per GB a month for standard storage, plus small request charges. A bucket holding a few GB costs under 1 dollar a month. Estimate.
The configuration
# A private S3 bucket for application data such as uploads, exports and backups.
#
# What this creates:
# - a bucket with all public access blocked and ACLs switched off
# - default encryption at rest and versioning
# - a lifecycle rule that removes old versions and abandoned uploads
# - a bucket policy that denies any request not made over TLS
terraform {
required_version = ">= 1.6.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 5.0"
}
}
}
provider "aws" {
region = var.region
}
variable "region" {
description = "AWS region to create the bucket in."
type = string
default = "us-east-1"
}
variable "bucket_name" {
description = "Name of the bucket. Bucket names are global, so include your company or account name."
type = string
}
variable "kms_key_arn" {
description = "ARN of a KMS key to encrypt objects with. Leave null to use keys that S3 manages."
type = string
default = null
}
variable "noncurrent_version_days" {
description = "Days to keep an overwritten or deleted object before it is removed for good."
type = number
default = 30
}
resource "aws_s3_bucket" "this" {
bucket = var.bucket_name
# Stops `tofu destroy` from deleting the bucket and the data in it.
# To tear down a test, remove this block first.
lifecycle {
prevent_destroy = true
}
}
# ACLs off: access is decided by IAM and the bucket policy only, and every
# object belongs to the bucket owner no matter who uploaded it.
resource "aws_s3_bucket_ownership_controls" "this" {
bucket = aws_s3_bucket.this.id
rule {
object_ownership = "BucketOwnerEnforced"
}
}
# All four settings on. Even if someone later adds a public policy or ACL by
# mistake, S3 refuses it or ignores it.
resource "aws_s3_bucket_public_access_block" "this" {
bucket = aws_s3_bucket.this.id
block_public_acls = true
block_public_policy = true
ignore_public_acls = true
restrict_public_buckets = true
}
# With versioning, an overwrite or delete keeps the previous copy, so a bug or
# a wrong command can be undone.
resource "aws_s3_bucket_versioning" "this" {
bucket = aws_s3_bucket.this.id
versioning_configuration {
status = "Enabled"
}
}
resource "aws_s3_bucket_server_side_encryption_configuration" "this" {
bucket = aws_s3_bucket.this.id
rule {
apply_server_side_encryption_by_default {
sse_algorithm = var.kms_key_arn == null ? "AES256" : "aws:kms"
kms_master_key_id = var.kms_key_arn
}
# Cuts the number of KMS requests when a KMS key is used.
bucket_key_enabled = var.kms_key_arn != null
}
}
resource "aws_s3_bucket_lifecycle_configuration" "this" {
bucket = aws_s3_bucket.this.id
# Lifecycle rules for old versions only work once versioning is on.
depends_on = [aws_s3_bucket_versioning.this]
rule {
id = "expire-old-versions"
status = "Enabled"
filter {}
# Without this, every old version is kept and billed forever.
noncurrent_version_expiration {
noncurrent_days = var.noncurrent_version_days
}
# Clears the leftover marker once all versions of a deleted object are gone.
expiration {
expired_object_delete_marker = true
}
# Parts of uploads that never finished are invisible in the console but
# still billed.
abort_incomplete_multipart_upload {
days_after_initiation = 7
}
}
}
data "aws_iam_policy_document" "tls_only" {
statement {
sid = "DenyRequestsWithoutTls"
effect = "Deny"
actions = ["s3:*"]
resources = [
aws_s3_bucket.this.arn,
"${aws_s3_bucket.this.arn}/*",
]
principals {
type = "*"
identifiers = ["*"]
}
condition {
test = "Bool"
variable = "aws:SecureTransport"
values = ["false"]
}
}
}
resource "aws_s3_bucket_policy" "this" {
bucket = aws_s3_bucket.this.id
policy = data.aws_iam_policy_document.tls_only.json
# Applying both at once can fail, so the public access block goes first.
depends_on = [aws_s3_bucket_public_access_block.this]
}
output "bucket_name" {
description = "Name of the bucket, for your application's configuration."
value = aws_s3_bucket.this.id
}
output "bucket_arn" {
description = "Use this in the IAM policy of the role that reads or writes the bucket."
value = aws_s3_bucket.this.arn
}
Works with OpenTofu 1.6 or later, and with Terraform. Read it before you apply it: it creates real resources in your account.