Sub topic 3
Choosing the REST endpoint or the website endpoint
This chapter assumes you know what an S3 bucket, object, and key are, and that you can read an HTTP status code and a JSON bucket policy. It also assumes you have seen the previous chapter on using a bucket as a CloudFront origin. By the end you should be able to pick the right S3 endpoint for a distribution, write the origin configuration for it, and diagnose the errors that come from picking the wrong one.
Two addresses for the same bucket
Every S3 bucket can be reached through two different kinds of address. CloudFront treats them as two different kinds of origin.
- The REST endpoint is the S3 API address, such as
amzn-s3-demo-bucket.s3.us-east-1.amazonaws.com. It accepts signed requests, supports HTTPS, and enforces bucket policies and Block Public Access. - The website endpoint is the static website hosting address, such as
amzn-s3-demo-bucket.s3-website-us-east-1.amazonaws.com. It behaves like a small web server. It resolves index documents, applies redirect rules, and serves a custom error page.
The website endpoint's hostname format differs by region. Some regions use a dash (s3-website-us-east-1) and others use a dot (s3-website.eu-central-1). Copy the exact hostname from the bucket's Static website hosting panel instead of typing it.
The website endpoint exists only after you turn on static website hosting for the bucket. The REST endpoint always exists.
How CloudFront sees each endpoint
CloudFront decides how to talk to an origin from the origin's configuration type.
With the REST endpoint, CloudFront uses an S3 origin. It can sign requests with SigV4 through Origin Access Control (OAC), so the bucket can stay fully private. It always connects to S3 over HTTPS when OAC is in use.
With the website endpoint, CloudFront uses a custom origin. It sees the endpoint as an ordinary HTTP server. It cannot sign requests for it, and the website endpoint does not accept HTTPS. The origin protocol policy must be http-only.
Side-by-side comparison
| Feature | REST endpoint | Website endpoint |
|---|---|---|
| CloudFront origin type | S3 origin | Custom origin |
| Origin Access Control | Supported | Not supported |
| Bucket can stay private | Yes | No, objects must be publicly readable |
| CloudFront to S3 protocol | HTTPS | HTTP only |
| Index document in subfolders | No, only the distribution's default root object | Yes, for every folder |
| Redirect rules | No | Yes |
| Custom error document from S3 | No | Yes |
| Missing object status | 403 or 404, depending on `s3:ListBucket` | 404 |
| Works with SSE-KMS objects | Yes, with extra key permissions | No, the endpoint cannot read KMS-encrypted objects for anonymous access |
The default recommendation: REST endpoint with OAC
For almost every site, choose the REST endpoint with OAC. The bucket keeps Block Public Access enabled, so nobody can bypass CloudFront and read objects directly from S3. Traffic between CloudFront and S3 is encrypted. A bucket policy such as the one in the earlier chapter limits reads to your distribution.
The website endpoint needs a public bucket. If you still must use it, the usual protection is a shared secret. CloudFront adds a custom origin header such as Referer or X-Origin-Secret, and the bucket policy allows reads only when the header matches. This is weaker than OAC. The secret travels in clear text over HTTP, and anyone who learns it can read the bucket directly.
Configuring each origin
For the REST endpoint, use the bucket's regional domain name. In CloudFormation, !GetAtt SiteBucket.RegionalDomainName gives the right value, as in the distribution excerpt from the previous chapter. The global name bucket.s3.amazonaws.com can answer with a temporary redirect (HTTP 307) for a newly created bucket until DNS propagates. CloudFront does not follow that redirect to a signed request, so viewers see errors. The regional name avoids the problem.
For the website endpoint, the origin needs CustomOriginConfig and no OAC. This excerpt shows only the origin, and the bucket name and region are placeholders.
Origins:
- Id: s3-website-origin
DomainName: amzn-s3-demo-bucket.s3-website-us-east-1.amazonaws.com
CustomOriginConfig:
OriginProtocolPolicy: http-only
HTTPPort: 80
HTTPSPort: 443
OriginCustomHeaders:
- HeaderName: Referer
HeaderValue: replace-with-a-long-random-secretNote that DomainName has no http:// prefix. The WebsiteURL attribute of a bucket resource includes the scheme, so you cannot pass it directly.
In the console, the origin domain dropdown lists REST endpoints. If you pick a bucket from it and the bucket has website hosting enabled, the console offers a "Use website endpoint" button. Choosing a bucket from the list keeps you on the REST path.
What you lose on the REST endpoint, and how to get it back
Choosing the REST endpoint means giving up three website features. Each has a CloudFront-side replacement.
Index documents in subfolders
The default root object serves index.html only for requests to /. A request for /docs/ goes to S3 as the key docs/, which usually does not exist. The website endpoint would translate it to docs/index.html. On the REST endpoint, rewrite the URI at the edge with a CloudFront Function attached to the viewer request event.
function handler(event) {
var request = event.request;
var uri = request.uri;
if (uri.endsWith('/')) {
request.uri = uri + 'index.html';
} else if (!uri.includes('.')) {
request.uri = uri + '/index.html';
}
return request;
}This function appends index.html to paths ending in a slash and to paths with no file extension. Adjust the second rule if your site has extensionless files that are not folders.
Error pages
The website endpoint returns your configured error document with a 404 status. With the REST endpoint, configure custom error responses on the distribution instead. The next chapters cover this in detail, but the key point here is the status code S3 sends. If the caller lacks s3:ListBucket on the bucket, S3 answers a missing key with 403, not 404, so that callers cannot discover which keys exist. OAC callers usually have only s3:GetObject, so missing pages appear as 403. You have two options:
- Map both 403 and 404 to your error page in the distribution's custom error responses.
- Add
s3:ListBucketon the bucket ARN to the bucket policy for the distribution, so S3 returns a true 404. This also lets the distribution see which keys exist, which is usually acceptable for a public site.
Redirects
Website redirect rules have no direct REST equivalent. Use a CloudFront Function or a Lambda@Edge function that returns a 301 or 302 response, or store a redirect object with the x-amz-website-redirect-location metadata only if you use the website endpoint.
Common errors and their causes
AccessDeniedon every request after adding OAC. The origin is the website endpoint. OAC cannot sign requests for it, and a private bucket denies unsigned ones. Switch to the REST endpoint or make the content public and drop OAC.- CloudFront rejects the config when you enable OAC on a custom origin. OAC applies only to S3 origins. A website endpoint is a custom origin, so the combination is invalid.
- Subfolder URLs return 403 or an XML error. The REST endpoint does not resolve index documents. Add the rewrite function above.
- Missing pages return 403 instead of 404. The policy lacks
s3:ListBucket. See the error pages section. - Redirect or
307 Temporary Redirectfrom a new bucket. The origin uses the global domain name. Use the regional one. - HTTPS origin errors with the website endpoint. The origin protocol policy is
https-onlyormatch-viewer. Sethttp-only.
Decision checklist
- Does the content need to stay private, or is Block Public Access required by your organization? Use the REST endpoint with OAC.
- Do you need S3-managed redirect rules and per-folder index documents, and can you accept a public bucket and HTTP to S3? Use the website endpoint.
- Can a viewer-request function and custom error responses provide what you need? Then use the REST endpoint, since those features cost very little and remove the need for a public bucket.
Exercise
Your distribution uses the website endpoint of a bucket as its origin, and you attach an OAC to it. Requests return AccessDenied. Give two valid fixes, and say which one you would choose for a site that must keep Block Public Access on.
Show answer
Fix one: change the origin to the bucket's REST (regional) endpoint, which is an S3 origin that OAC can sign for, and add a bucket policy that allows cloudfront.amazonaws.com to s3:GetObject for your distribution's ARN. Fix two: keep the website endpoint, remove OAC, make the bucket publicly readable (ideally restricted by a secret custom header), and set the origin protocol policy to http-only. For a site that must keep Block Public Access on, choose fix one, because the website endpoint cannot work with a private bucket. Replace the lost index-document behavior with a viewer-request CloudFront Function and custom error responses.
Exercise
A user requests /guides/ on a distribution with a REST endpoint origin and default root object index.html. S3 holds guides/index.html. What does the user get, and what change fixes it?
Show answer
The default root object applies only to the root path "/", so CloudFront asks S3 for the key "guides/". That key does not exist, and without s3:ListBucket permission S3 returns 403 (or 404 with it). The fix is a viewer-request CloudFront Function that rewrites URIs ending in "/" to add "index.html", so the request becomes guides/index.html.