
Hosting a Secure Static Website on AWS with a Private S3 Bucket, CloudFront and Terraform
Learn how to serve a static site over HTTPS from a fully private S3 bucket using CloudFront Origin Access Control, all defined as repeatable Terraform code.
Hosting a static website on AWS looks easy: create an S3 bucket, make it public, and turn on static website hosting. It works, but it leaves your bucket open to the internet, offers no HTTPS in a custom setup, and provides no edge caching.
The better pattern keeps the bucket completely private and puts Amazon CloudFront in front of it. CloudFront serves your files over HTTPS from edge locations, and only that CloudFront distribution can read from S3. In this tutorial, we build this with Terraform so the whole setup is repeatable.
Architecture
Visitor --HTTPS--> CloudFront --(signed request, OAC)--> Private S3 bucket- S3 bucket: stores the files. All public access is blocked.
- Origin Access Control (OAC): lets CloudFront sign its requests to S3.
- Bucket policy: allows
s3:GetObjectonly from your specific distribution.
- CloudFront distribution: serves the site over HTTPS and caches content.
OAC is the recommended replacement for the older Origin Access Identity (OAI).
Prerequisites
- An AWS account and the AWS CLI configured (
aws configure)
- Terraform 1.5 or later
- A folder named
site/containing at least anindex.html
Step 1: Provider and variables
Create
main.tf :hcl
terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } }}
provider "aws" { region = "us-east-1"}
variable "bucket_name" { description = "Globally unique name for the site bucket" type = string}Step 2: Create a private S3 bucket
hcl
resource "aws_s3_bucket" "site" { bucket = var.bucket_name}
resource "aws_s3_bucket_public_access_block" "site" { bucket = aws_s3_bucket.site.id block_public_acls = true block_public_policy = true ignore_public_acls = true restrict_public_buckets = true}The public access block is your safety net. Even if someone adds a permissive policy later, S3 will refuse to make the bucket public.
Step 3: Create the Origin Access Control
hcl
resource "aws_cloudfront_origin_access_control" "site" { name = "${var.bucket_name}-oac" origin_access_control_origin_type = "s3" signing_behavior = "always" signing_protocol = "sigv4"}Step 4: Create the CloudFront distribution
hcl
resource "aws_cloudfront_distribution" "site" { enabled = true default_root_object = "index.html" price_class = "PriceClass_100"
origin { domain_name = aws_s3_bucket.site.bucket_regional_domain_name origin_id = "s3-site" origin_access_control_id = aws_cloudfront_origin_access_control.site.id }
default_cache_behavior { target_origin_id = "s3-site" viewer_protocol_policy = "redirect-to-https" allowed_methods = ["GET", "HEAD"] cached_methods = ["GET", "HEAD"] compress = true
# AWS managed policy: CachingOptimized cache_policy_id = "658327ea-f89d-4fab-a63d-7e88639e58f6" }
restrictions { geo_restriction { restriction_type = "none" } }
viewer_certificate { cloudfront_default_certificate = true }}Key choices here:
-
redirect-to-httpsforces all traffic onto HTTPS.
-
PriceClass_100limits edge locations to the cheapest regions, which is fine for a learning project.
- The default certificate gives you an
xxxx.cloudfront.netdomain. For your own domain, add an ACM certificate (issued inus-east-1) and Route 53 records.
Step 5: Allow only CloudFront to read the bucket
hcl
data "aws_iam_policy_document" "site" { statement { sid = "AllowCloudFrontRead" actions = ["s3:GetObject"] resources = ["${aws_s3_bucket.site.arn}/*"]
principals { type = "Service" identifiers = ["cloudfront.amazonaws.com"] }
condition { test = "StringEquals" variable = "AWS:SourceArn" values = [aws_cloudfront_distribution.site.arn] } }}
resource "aws_s3_bucket_policy" "site" { bucket = aws_s3_bucket.site.id policy = data.aws_iam_policy_document.site.json}
output "site_url" { value = "https://${aws_cloudfront_distribution.site.domain_name}"}The
AWS:SourceArn condition is what makes this secure. It means the bucket trusts your distribution only, not every CloudFront distribution in existence.Step 6: Deploy
bash
terraform initterraform apply -var="bucket_name=my-unique-site-bucket-12345"Upload your site and clear the cache:
bash
aws s3 sync ./site s3://my-unique-site-bucket-12345 --delete
aws cloudfront create-invalidation \ --distribution-id <YOUR_DISTRIBUTION_ID> \ --paths "/*"Open the
site_url output in your browser. Your site is live over HTTPS. Try visiting the raw S3 URL of a file: you should get Access Denied, which is exactly what we want.Best practices
- Keep the bucket private. Never rely on a public bucket for a production site.
- Use OAC, not OAI. OAC supports SSE-KMS and all regions, and is the current recommendation.
- Version your files. Use hashed file names (like
app.3f9a1c.js) so you can cache aggressively and avoid constant invalidations.
- Add security headers. Attach a CloudFront response headers policy for HSTS and similar headers.
- Store Terraform state remotely. Use an S3 backend with locking once you work in a team.
- Automate deploys. Run
terraform applyandaws s3 syncfrom GitHub Actions instead of your laptop.
Clean up
To avoid charges from a test project:
bash
aws s3 rm s3://my-unique-site-bucket-12345 --recursiveterraform destroy -var="bucket_name=my-unique-site-bucket-12345"Conclusion
With about 60 lines of Terraform, you have a static site that is private at the origin, encrypted in transit, cached globally and fully reproducible. From here, the natural next steps are a custom domain with ACM and Route 53, and a CI/CD pipeline that deploys on every push.
If you found this useful, let me know in the comments what you would like to see next.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article