Sub topic 2
Using an S3 bucket as an origin
This chapter assumes you know S3 buckets, objects, and keys, can read HTTP status codes and simple IAM JSON policies, and can run AWS CLI commands. You will see how CloudFront treats a bucket as an origin, what the origin setting contains, and which errors appear when it is wrong.
What an origin is
An origin is the server that holds the original copy of your content. CloudFront keeps copies at edge locations, and it goes back to the origin only when an edge has no fresh copy. When the origin is an S3 bucket, CloudFront sends ordinary HTTPS GET requests to the bucket's domain name. It maps the request path to an object key.
For example, a viewer requests /img/logo.png. CloudFront asks the origin for /img/logo.png, and S3 looks up the key img/logo.png.
What the origin setting contains
An S3 origin in a distribution has a few fields that matter:
| Field | Meaning | Example |
|---|---|---|
| Id | A name that cache behaviors use to point at this origin | `s3-origin` |
| Domain name | The address CloudFront connects to | `amzn-s3-demo-bucket.s3.us-east-1.amazonaws.com` |
| Origin path | Optional prefix added to every request | `/site` |
| Origin access control | How CloudFront signs requests to S3 | `site-oac` |
Use the bucket's regional domain name, not a bare global name. The regional name includes the Region, so requests go straight to the bucket's Region. In CloudFormation, !GetAtt SiteBucket.RegionalDomainName returns it.
The origin path is a prefix. With an origin path of /site, a viewer request for /index.html becomes a request for the key site/index.html. This lets one bucket hold several sites, with one distribution per prefix.
A minimal origin in CloudFormation
This excerpt shows the origin portion. The bucket resource SiteBucket is assumed to be defined elsewhere, and the full distribution appears in a later chapter.
Origins:
- Id: s3-origin
DomainName: !GetAtt SiteBucket.RegionalDomainName
OriginAccessControlId: !GetAtt SiteOac.Id
S3OriginConfig:
OriginAccessIdentity: ''The empty OriginAccessIdentity is required by the schema when you use an S3 origin with Origin Access Control. The older Origin Access Identity feature is replaced by Origin Access Control, which the next chapters cover.
Common errors
- 403 Access Denied for every object. The bucket policy does not allow CloudFront, or the Origin Access Control is not attached to the origin.
- 403 for a key that does not exist. Without
s3:ListBucketpermission, S3 returns 403 instead of 404 for missing keys. This often looks like a permission bug when the key is simply wrong. - 404 for a file you uploaded. Check the origin path. A prefix of
/sitemeans the object must be stored atsite/.... Keys are also case sensitive. - Redirects to the bucket's own address. A newly created bucket can answer with a redirect while its regional DNS name propagates. Using the regional domain name avoids this.
- Stale content after an upload. CloudFront serves its cached copy until the TTL expires. This is caching behavior, not an origin fault, and later chapters cover it.
Check the origin directly
Confirm that the object exists before you debug CloudFront.
aws s3api head-object --bucket amzn-s3-demo-bucket --key img/logo.pngIf this returns metadata, the key is correct. If it fails with 404, fix the upload or the key name first.
Exercise
A distribution has the origin path /site. A viewer requests /css/app.css. Which S3 key does CloudFront request, and why might the viewer get a 403 or 404 if the file was uploaded as css/app.css?
Show answer
CloudFront requests the key site/css/app.css, because the origin path is added as a prefix. The file uploaded as css/app.css is not at that key, so S3 returns 404, or 403 if the caller lacks s3:ListBucket permission. Fix it by uploading to site/css/app.css or by removing the origin path.