"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
- Upload your built site (HTML, JavaScript, CSS, images) to a regular, private S3 bucket. Leave Block Public Access on.
- Create a CloudFront distribution with that bucket as its origin. CloudFront serves the files from edge locations close to your users, over HTTPS.
- Attach an origin access control (OAC). CloudFront then signs every request it sends to S3.
- Add a bucket policy that allows the
cloudfront.amazonaws.comservice principal tos3:GetObject, with a condition thatAWS:SourceArnequals 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; onlyindex.htmlneeds 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.