AWS · SECURITY
Static website on S3 and CloudFront: secure it with OAC
On this page
- Keep the bucket private and let only your CloudFront distribution read it, through origin access control.
- Use the bucket's normal REST endpoint as the origin. OAC does not work with the S3 website endpoint.
- Scope the bucket policy to your one distribution with the aws:SourceArn condition.
- Origin access identity (OAI) is the legacy way. Move to OAC; it can be done without downtime.
A static website on S3 and CloudFront is the simplest serious hosting AWS offers: no servers, HTTPS on your own domain, and pages served from edge locations near your visitors. The part people most often get wrong is the bucket. It should not be public, and with CloudFront's origin access control (OAC) it does not need to be. This post draws the design, explains each piece, and covers the traps.
The problem: fast, safe hosting for files
Marketing sites, documentation and single-page apps are just files. Running servers for them wastes money and adds patching. Serving them straight from a public bucket works, but makes the bucket readable by anyone, bypasses your CDN and its protections, and gives you no HTTPS on your own domain. CloudFront in front of a private bucket fixes all three.
The design
Follow a request through the diagram (open the diagram):
- The visitor's browser looks up your domain in Amazon Route 53, where an alias record points at the CloudFront distribution.
- The browser requests the page over HTTPS. CloudFront presents a certificate from AWS Certificate Manager for your domain, and an AWS WAF web ACL can filter requests first.
- If the edge location has the file cached, it answers at once.
- On a cache miss, CloudFront fetches the file from S3, signing the request with SigV4 under origin access control. The bucket accepts it because its policy allows the CloudFront service principal, and only for this distribution.
- To publish, your pipeline uploads the new build to the bucket and invalidates the changed paths in CloudFront.
Why origin access control
OAC arrived in August 2022 as the successor to origin access identity. The older OAI does not support SSE-KMS encryption, opt-in Regions launched after December 2022, or requests such as PUT and DELETE without workarounds. OAC uses short-lived credentials and IAM service principals, and AWS now calls OAI legacy.
The bucket policy
The policy grants s3:GetObject to cloudfront.amazonaws.com with a condition that the request comes from your distribution's ARN. That condition matters: without it, any distribution in any account could be pointed at your bucket. The CloudFront console shows the exact statement to copy once the distribution exists. Add s3:PutObject only if visitors really upload through CloudFront.
Traps worth knowing
- The website endpoint. If the origin is the bucket's static website endpoint, CloudFront treats it as a custom origin and you cannot use OAC. Use the REST endpoint and set a default root object such as
index.html. - Folder index pages. Unlike an S3 website's index document, CloudFront's default root object applies only to the root URL, not to
/docs/. Have your build link to explicit file names, or rewrite folder requests at the edge. - Object ownership. OAC expects S3 Object Ownership set to bucket owner enforced, the default for new buckets.
- KMS. If objects use SSE-KMS, the key policy must also allow CloudFront, again scoped to your distribution.
- Migrating from OAI. Allow both the OAI and the distribution in the bucket policy, switch the origin to OAC, wait for the deployment, then remove the OAI statement. Visitors notice nothing.
Cost and security notes
- Cost is mostly CloudFront data transfer and requests, plus small S3 storage and request charges on cache misses. Long cache lifetimes on fingerprinted assets keep S3 requests and latency down.
- WAF is billed on top of CloudFront. For a brochure site with no forms, decide whether you need it; for anything with a login or an API behind it, you do.
- Turn on Block Public Access for the bucket. With OAC there is no reason for any public grant.
When not to use it
- Pages must be rendered per request on a server: put CloudFront in front of that server instead.
- You want previews per branch, form handling and builds with no AWS set-up. A managed front-end host may be quicker, at the cost of control.
- The site is internal-only: CloudFront is a public edge, so you would add authentication at the edge, which changes the design.
How to draw it in archhy
- From the Services drawer (AWS pack), place an AWS Cloud. Put Route 53 and CloudFront directly in it, as global services, and S3 inside a Region container.
- WAF, Certificate Manager and KMS have no palette item of their own: place Generic Resources, each with a clear name; their official AWS icons are not in the palette.
- Draw the visitor's path in blue, the cache-miss rail to S3 in green, and the publishing rails in amber, so three different stories read at a glance.
- Open the bucket's Data tab and add fields such as "Public access: blocked" and "Origin: REST endpoint", and show them in an info box.
- Download a PNG for your README from the File menu.
Related: blue/green deployments to ECS and multi-tenant SaaS on AWS.
Frequently asked questions
Does my S3 bucket need to be public for a static website with CloudFront?
No. Keep it private and allow only your CloudFront distribution to read it, using origin access control and a bucket policy scoped with aws:SourceArn.
What is the difference between OAC and OAI?
OAC is the newer, recommended way. It supports SSE-KMS, all Regions and methods such as PUT and DELETE; OAI is legacy.
Can I use OAC with the S3 website endpoint?
No. A website endpoint is treated as a custom origin, so use the bucket's REST endpoint with OAC instead.
How do I update the site after a change?
Upload the new files to the bucket and invalidate the changed paths in CloudFront, or use versioned file names so new files never collide with cached ones.
based on Redrawn and discussed from Amazon CloudFront introduces Origin Access Control (OAC) by Ben Lee, Anuj Butail and Igor Kushnirov (AWS Networking & Content Delivery Blog, 25 August 2022), checked against the CloudFront developer guide page Restrict access to an Amazon S3 origin. The diagram and commentary are archhy's own.


