AWS in 1 Min · Episode 188

Static site with S3 and CloudFront: private bucket, origin access control

Private bucket, global edge. Watch the one-minute video, then read the details.

David Gomez1:17 video · 2 min read

"A static site needs a public bucket." It shouldn't. A public bucket lets anyone read every object in it directly, skipping your CDN, your caching and your logs. The pattern AWS recommends is the opposite: keep the S3 bucket private and make Amazon CloudFront the only way in. (Episode 98 covered S3's own website hosting and its limits; this is the production version.)

The pattern

  1. Upload your built site (HTML, JavaScript, CSS, images) to a regular, private S3 bucket. Leave Block Public Access on.
  2. Create a CloudFront distribution with that bucket as its origin. CloudFront serves the files from edge locations close to your users, over HTTPS.
  3. Attach an origin access control (OAC). CloudFront then signs every request it sends to S3.
  4. Add a bucket policy that allows the cloudfront.amazonaws.com service principal to s3:GetObject, with a condition that AWS:SourceArn equals your distribution's ARN. No other distribution and no anonymous user can read the bucket.

OAC replaces the older origin access identity (OAI), which AWS now calls legacy; OAC also works with SSE-KMS encrypted objects and with all Regions. Note that OAC needs the bucket's REST endpoint, not the S3 website endpoint.

Your own domain

  • Request a free public certificate in AWS Certificate Manager, in us-east-1 (N. Virginia). CloudFront only accepts certificates from that Region, wherever your bucket is. See ACM.
  • Add your domain as an alternate domain name on the distribution.
  • In Route 53, create an alias record pointing the name at the distribution.

Caching and deploys

CloudFront caches files at the edge, so a new deploy may not show up right away. Two options:

  • Invalidate the paths that changed, such as /index.html.
  • Better for assets: give built files content-hashed names (app.4f2a.js). A new name is a new object, so there is never a stale copy; only index.html needs a short cache time or an invalidation.

Single-page apps

React, Vue or Angular apps handle routes in the browser. A deep link like /menu/today has no file in S3, and with a private bucket S3 answers 403 or 404. Configure CloudFront custom error responses to return /index.html with status 200 for those codes, so the app loads and routes itself. A common mistake is mapping errors this way and then wondering why missing images also return HTML; scope it carefully, or use a CloudFront Function to rewrite only route paths.

Where AI fits

This is the natural front end for a serverless AI app. Add a second cache behavior for /api/* that points to Amazon API Gateway or a Lambda function URL. That backend checks the user, calls Amazon Bedrock, and can stream the answer back so text appears as it is generated. The browser talks only to your domain: no cross-origin setup, and no model credentials ever ship in front-end code.

Quick recap

  • Private bucket, Block Public Access on.
  • CloudFront with origin access control is the only way in; certificate from ACM in us-east-1.
  • Your API on its own path (/api/*) for dynamic and AI features.
AWS in 1 Min #188 · 1:17

Need an AWS expert on your team?

I'm David, an AWS expert and fractional cloud consultant. I work alongside your team to run your cloud and infrastructure: architecture, security, cost and day-to-day operations. You get senior AWS experience when you need it, without the cost of a full-time hire.

  • Architecture reviews and a clear plan for what to fix first
  • Security hardening: IAM, networking, audits
  • Lower AWS bills without slowing your team down
  • Hands-on help with AI workloads on AWS