Lesson 3 of 3 Route 53, Application Recovery Controller and CloudFront

Delivering Resilient Content with Amazon CloudFront

Amazon CloudFront is a content delivery network with hundreds of edge locations. It improves performance by caching content near users and improves resilience by absorbing traffic spikes, shielding origins and failing over between origins. This lesson explains distributions, origins and cache behaviours, caching controls and invalidation, origin groups for failover, Origin Shield, error handling and edge code.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe CloudFront distributions, origins, cache behaviours and origin access control.
  • Explain how cache policies, TTLs, versioned object names and invalidations control freshness.
  • Explain how origin groups provide origin failover and their limitations.
  • Describe how Origin Shield, custom error responses and edge functions improve resilience and scalability.

Distributions, Origins and Behaviours

A CloudFront distribution has a domain name, optional alternate domain names with an ACM certificate from us-east-1, and one or more origins: S3 buckets, load balancers, API Gateway endpoints, Lambda function URLs or any custom HTTP origin. Cache behaviours map path patterns, such as /api/* and /static/*, to origins with their own caching, allowed methods, viewer protocol policy and functions. A default behaviour handles all other paths.

For S3 origins, origin access control (OAC) lets CloudFront sign requests to the bucket, and the bucket policy allows only the distribution, so users cannot bypass CloudFront. For custom origins, a secret header added by CloudFront and checked by the origin, or the CloudFront managed prefix list in the origin's security group, restricts access to CloudFront. VPC origins let CloudFront reach load balancers and instances in private subnets directly.

Caching and Freshness

A cache policy defines the cache key, meaning which headers, cookies and query strings distinguish cached objects, and the minimum, default and maximum TTLs. An origin request policy defines which values are forwarded to the origin without being part of the cache key. Keeping the cache key small raises the cache hit ratio, which reduces origin load and latency; including unnecessary headers or cookies fragments the cache. The origin's Cache-Control headers set object TTLs within the policy's bounds.

When content changes, there are two ways to make users see the new version. Invalidations remove objects from edge caches by path, including wildcards; the first 1,000 paths per month are free, and invalidation takes a short time to propagate. Versioned object names, such as app.3f9c2.js referenced from an HTML page with a short TTL, make each release use new names, so caches never serve stale assets, rollbacks are instant and no invalidation is needed. Versioned names are the recommended approach for static assets deployed by pipelines.

Origin Failover with Origin Groups

An origin group contains a primary and a secondary origin. When CloudFront receives one of the configured failover status codes from the primary (chosen from 500, 502, 503, 504, 403 and 404), or cannot connect to it or times out, it retries the request against the secondary origin. A typical design pairs an S3 bucket in one Region with its replica bucket in another, or a primary Regional load balancer with a recovery-Region load balancer.

Origin failover has limitations. It applies only to GET, HEAD and OPTIONS requests; writes are not failed over. It happens per request, so each request to an unavailable primary first waits for the failure, which can add latency unless connection timeouts and attempts are reduced. Recovery is automatic, because each request tries the primary first. For write traffic or whole-application failover, Route 53 or ARC is still needed.

SettingEffect on failover
Failover criteria status codesWhich origin responses trigger a retry to the secondary
Connection attempts (1 to 3)How many times CloudFront tries to connect to an origin
Connection timeout (1 to 10 seconds)How long each connection attempt waits
Response timeoutHow long CloudFront waits for a response from the origin

The following diagram shows how a request fails over to the secondary origin.

Origin failover with a CloudFront origin group. Each request goes to the primary origin first. If the primary returns a configured status code (from 500, 502, 503, 504, 403 and 404) or cannot be reached within the connection attempts and timeout, CloudFront retries the request against the secondary origin. Failover applies only to GET, HEAD and OPTIONS requests, and it happens per request, so writes and whole-application failover still need Route 53 or Application Recovery Controller.
Origin failover works per request and only for reads.
True or false

Resilient CloudFront

Exam skills 3.3.3 · 3.2.4

Check four facts about CloudFront origin failover and origin protection.

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

Origin Shield, Error Handling and Edge Code

Origin Shield adds a centralised caching layer in a chosen Region between the edge locations and the origin. All edge caches fetch through it, so each object is requested from the origin only once, which greatly reduces origin load and protects origins during traffic spikes or cache flushes.

Custom error responses map origin errors to custom pages and set an error caching minimum TTL, which controls how long CloudFront caches an error before retrying the origin. A short error TTL lets recovery be noticed quickly; serving a friendly static page from S3 for 5xx errors keeps users informed during an outage. CloudFront can also serve stale cached content when the origin is unavailable, if the cache policy and headers such as stale-if-error allow it.

Code at the edge adds flexibility. CloudFront Functions run lightweight JavaScript at viewer request and response events for header manipulation, redirects and URL rewrites at very high scale and low cost. Lambda@Edge runs Node.js or Python functions at viewer and origin events with network access, for example to select an origin dynamically based on health information or the user's attributes. AWS WAF web ACLs and AWS Shield protect distributions from attacks, as described in the security modules.

CloudFront Functions
EventsViewer request and response
RuntimeJavaScript, sub-millisecond
Network accessNo
Choose when Simple header, URL and redirect logic at scale.
Lambda@Edge
EventsViewer and origin request and response
RuntimeNode.js and Python, up to seconds
Network accessYes
Choose when Logic needing network calls or origin selection.
Worked example

A Resilient Static Website

A marketing site is served from an S3 bucket in us-east-1 through CloudFront. The company wants the site to stay available if the bucket's Region is impaired, releases to be visible immediately without serving stale assets, and the origin to be protected from spikes during campaigns.

  1. Replica

    Replicate the bucket to us-west-2 with CRR and Replication Time Control.

  2. Failover

    Create an origin group with both buckets, using OAC for each, with failover on 500, 502, 503, 504 and 403 and a connection timeout of 3 seconds.

  3. Releases

    Deploy assets with content-hashed names and a long TTL, and HTML with a short TTL, so releases need no invalidation.

  4. Shielding

    Enable Origin Shield in us-east-1.

Check yourself

A CloudFront origin group fails over GET requests correctly, but a form that POSTs data fails when the primary origin is down. Why?

Show answer

Origin failover applies only to GET, HEAD and OPTIONS requests. Write requests need application-level or DNS-level failover, such as Route 53 failover records.

Exam Tips

Remember for the exam

  • Use origin access control so S3 origins accept requests only from the distribution.
  • Prefer versioned object names to invalidations for pipeline deployments; small cache keys raise the hit ratio.
  • Origin groups fail over GET, HEAD and OPTIONS requests on chosen status codes, connection failures or timeouts; tune connection timeouts and attempts.
  • Origin Shield adds a central cache layer that reduces origin load.
  • Custom error responses with short error caching TTLs and stale-if-error improve behaviour during outages.