Lesson 1 of 4 Amazon CloudFront and AWS Global Accelerator

Configuring CloudFront Distributions and Origins

This lesson explains how Amazon CloudFront, the AWS content delivery network, serves content from points of presence close to users, and how distributions, origins and viewer settings are configured.

Learning outcomes

After completing this lesson, you will be able to:

  • Configure distributions and origins.
  • Secure origins with origin access control.
  • Configure viewer protocol and restrictions.

Distributions and origins

A distribution has a domain name (d111111abcdef8.cloudfront.net), optional alternate domain names (CNAMEs) with an ACM certificate from us-east-1, and one or more origins: S3 buckets, S3 website endpoints, ALBs, EC2 instances, API Gateway, Lambda function URLs, MediaPackage or any HTTP server. Origin groups pair a primary and secondary origin for origin failover on specified HTTP error codes or timeouts. Regional edge caches sit between edge locations and origins, and Origin Shield adds an optional central cache layer in one Region that collapses requests from all edges, raising the cache hit ratio and reducing origin load. Route 53 alias records point domain names at distributions.

Securing origins

Origins should accept traffic only from CloudFront so that edge controls such as WAF, geographic restrictions and signed URLs cannot be bypassed. For S3, origin access control (OAC) signs CloudFront's requests, and the bucket policy allows only the cloudfront.amazonaws.com principal with the distribution's ARN as aws:SourceArn; OAC replaces the legacy origin access identity and supports SSE-KMS-encrypted objects (the key policy must allow CloudFront). For ALBs and custom origins, use the CloudFront managed prefix list in the origin's security group and a secret custom header that the ALB checks in a listener rule, or VPC origins, which let CloudFront reach ALBs, NLBs and instances in private subnets directly.

{
  "Sid": "AllowCloudFrontServicePrincipalReadOnly",
  "Effect": "Allow",
  "Principal": {"Service": "cloudfront.amazonaws.com"},
  "Action": "s3:GetObject",
  "Resource": "arn:aws:s3:::acme-site/*",
  "Condition": {"StringEquals": {"AWS:SourceArn": "arn:aws:cloudfront::111122223333:distribution/EDFDVBD6EXAMPLE"}}
}

The following diagram shows the two policies that must allow CloudFront for a private S3 origin.

The two policies a private S3 origin needs with origin access control. The bucket policy must allow cloudfront.amazonaws.com with an AWS:SourceArn naming this distribution, and for SSE-KMS objects the key policy must allow CloudFront kms:Decrypt. If either is missing or names another distribution, CloudFront receives 403 Access Denied.
A 403 from a private origin almost always means a policy, not a missing object.
Scenario questions

Securing origins

Exam skills 5.2.3 · 4.2.3

Make sure users reach content only through CloudFront.

Log in to try this activity Interactive activities are free with an account.

Viewer settings and restrictions

Distribution and behaviour settings control what viewers can do. The viewer protocol policy allows HTTP and HTTPS, redirects HTTP to HTTPS, or requires HTTPS. Allowed HTTP methods must include POST, PUT and DELETE for dynamic APIs. Geographic restrictions allow or block countries. Signed URLs and signed cookies, created with a key group's private key, restrict private content to authorised users. A WAF web ACL can be associated, and CloudFront Functions and Lambda@Edge run code at the edge for header manipulation, redirects and authentication.

Match the pairs

Viewer settings

Exam skills 5.2.3

Match each requirement with the CloudFront setting that meets it.

Log in to try this activity Interactive activities are free with an account.
Worked example

An S3 origin returning 403

After migrating a static site from a public bucket to a private bucket behind CloudFront with OAC, every request returns 403 Access Denied. The bucket policy allows cloudfront.amazonaws.com but its AWS:SourceArn condition names the old distribution ID, and the objects are encrypted with a customer managed KMS key whose policy does not allow CloudFront. Correcting the distribution ARN and adding a key policy statement that allows cloudfront.amazonaws.com kms:Decrypt with the same source ARN condition fixes the site. A missing default root object would have produced a 403 only for the root URL.

Check yourself

Which feature provides failover between two origins?

Show answer

An origin group.

Exam Tips

Remember for the exam

  • Use OAC so private S3 buckets are reachable only through CloudFront.
  • Custom origins: CloudFront prefix list plus a secret header, or VPC origins.
  • Origin groups provide origin failover; Origin Shield raises cache hit ratio.
  • CloudFront alternate domain names need ACM certificates in us-east-1.
  • Signed URLs or cookies restrict private content.