Sub topic 4
Creating a CloudFront distribution
This chapter assumes you know S3 buckets, objects, and keys, can read HTTP status codes and JSON policies, and can run AWS CLI commands. It covers the distribution itself. The bucket policy, cache tuning, and custom domains come in later chapters.
What a distribution is
A distribution is the CloudFront resource that ties together three things: where content comes from (origins), how requests are handled (cache behaviors), and how viewers reach it (a domain name like d111111abcdef8.cloudfront.net). Every distribution has at least one origin and one default cache behavior.
The pieces you configure
| Setting | What it does | Value in this chapter |
|---|---|---|
| Origin | Names the S3 bucket CloudFront fetches from | Bucket's regional domain name |
| Origin access | Controls how CloudFront authenticates to S3 | Origin Access Control (OAC) |
| Default cache behavior | Handles every path no other behavior matches | Redirect HTTP to HTTPS, managed cache policy |
| Default root object | File returned for requests to `/` | `index.html` |
Use the bucket's regional domain name, such as amzn-s3-demo-bucket.s3.us-east-1.amazonaws.com. This is the REST endpoint, which works with OAC. The previous chapter covered how it differs from the website endpoint.
Defining the distribution in CloudFormation
This excerpt creates the OAC and the distribution. It assumes a bucket resource named SiteBucket is defined elsewhere in the template.
Resources:
SiteOac:
Type: AWS::CloudFront::OriginAccessControl
Properties:
OriginAccessControlConfig:
Name: site-oac
OriginAccessControlOriginType: s3
SigningBehavior: always
SigningProtocol: sigv4
SiteDistribution:
Type: AWS::CloudFront::Distribution
Properties:
DistributionConfig:
Enabled: true
DefaultRootObject: index.html
Origins:
- Id: s3-origin
DomainName: !GetAtt SiteBucket.RegionalDomainName
OriginAccessControlId: !GetAtt SiteOac.Id
S3OriginConfig:
OriginAccessIdentity: ''
DefaultCacheBehavior:
TargetOriginId: s3-origin
ViewerProtocolPolicy: redirect-to-https
CachePolicyId: 658327ea-f89d-4fab-a63d-7e88639e58f6Walk through the key lines:
SigningBehavior: alwaysmakes CloudFront sign every request to S3 with SigV4. S3 can then verify the caller is CloudFront.OriginAccessControlIdattaches the OAC to the origin. Without it, CloudFront sends anonymous requests and a private bucket returns 403.S3OriginConfigwith an emptyOriginAccessIdentitymarks the origin as an S3 bucket and tells CloudFront not to use the older Origin Access Identity (OAI) mechanism.TargetOriginIdmust match an originId. This is how the behavior knows where to forward cache misses.CachePolicyIdis the ID of the AWS managed policy CachingOptimized. Cache policies get their own chapter.
Deploying and checking it
A new distribution shows a status of InProgress while CloudFront pushes the configuration to edge locations. This usually takes a few minutes. Wait for Deployed, then request the domain name.
Common errors
- 403 from a fresh distribution: the bucket policy is missing or its
AWS:SourceArnnames a different distribution. - Wrong endpoint: using the website endpoint as the origin breaks OAC, because that endpoint does not accept signed requests.
- Origin ID mismatch: a
TargetOriginIdthat matches no origin fails validation at deploy time. - Stale configuration: a change is not live until the status returns to
Deployed.
Exercise
Your distribution is deployed, but requests to its domain name return 403 AccessDenied. The origin uses the bucket's regional domain name and has an OAC attached. What is the most likely missing piece?
Show answer
The bucket policy. OAC makes CloudFront sign requests, but S3 still needs a policy that allows the cloudfront.amazonaws.com service principal to call s3:GetObject, restricted to your distribution's ARN with an AWS:SourceArn condition.