Sub topic 3
Choosing the REST endpoint or the website endpoint
This chapter assumes you know S3 buckets, objects, and keys, can read HTTP status codes, and can run AWS CLI commands. You will learn the two ways CloudFront can reach a bucket and which one to pick.
Two endpoints for one bucket
Every S3 bucket can be reached through two different hostnames. They behave differently, and CloudFront treats them differently.
- The REST endpoint looks like
amzn-s3-demo-bucket.s3.us-east-1.amazonaws.com. It speaks the S3 API over HTTPS. - The website endpoint looks like
amzn-s3-demo-bucket.s3-website-us-east-1.amazonaws.com. It exists only after you enable static website hosting on the bucket. Some regions use a dot instead of a dash before the region name.
When you add a bucket as an origin in the console, CloudFront offers the REST endpoint (the bucket's regional domain name). The website endpoint must be entered by hand as a custom origin.
Comparing them
| Feature | REST endpoint | Website endpoint |
|---|---|---|
| Origin type in CloudFront | S3 origin | Custom origin |
| Origin Access Control | Supported | Not supported |
| Bucket can stay private | Yes | No, objects must be public |
| Protocol from CloudFront to bucket | HTTPS | HTTP only |
| Index document for subfolders | No | Yes (`index.html` in any folder) |
| Redirect rules and custom error document | No | Yes |
| Error for a missing key | 403 or 404, depending on permissions | 404 |
The key difference is access control. Origin Access Control (OAC) signs requests to S3 with SigV4, and that only works with the REST endpoint. The website endpoint accepts anonymous requests only, so the bucket would need public read access. Anyone could then bypass CloudFront and read the bucket directly.
What you gain and lose
The website endpoint gives you folder-style index pages. A request for /docs/ returns docs/index.html. It also supports S3 redirect rules.
The REST endpoint does not do either. A request for /docs/ asks for a key named docs/, which usually does not exist. CloudFront's default root object setting only handles the root path /. For subfolders you typically rewrite the URI with a CloudFront Function. The next chapters cover the default root object and error pages.
A common error
A frequent mistake is creating the origin with the website endpoint and then trying to attach OAC. The console blocks this, and in infrastructure code the origin is created as a custom origin, so OAC settings do not apply. Another mistake is the reverse. A team uses the REST endpoint with a private bucket and then wonders why /about/ returns an error with an AccessDenied or NoSuchKey body.
You can tell which endpoint an origin uses by looking at its domain name. If it contains s3-website, it is the website endpoint.
aws cloudfront get-distribution-config --id EDFDVBD6EXAMPLE \
--query "DistributionConfig.Origins.Items[].DomainName"How to choose
Use the REST endpoint with OAC by default. It keeps the bucket private, uses HTTPS to the origin, and matches the rest of this tutorial.
Use the website endpoint only when you depend on S3 redirect rules or on index documents in many subfolders, and you accept a public bucket with HTTP between CloudFront and S3. Even then, a CloudFront Function on the REST endpoint often replaces those features.
Exercise
Your single-page site needs a private bucket, and users request / only. Which endpoint and which access method do you choose, and why?
Show answer
Use the REST endpoint as an S3 origin with Origin Access Control. OAC works only with the REST endpoint, it lets Block Public Access stay on, and the default root object covers the / request, so you do not need the website endpoint's index document feature.