Xoxoftware - XOXO Creative Studio | Web & Mobile App Development | Fred Cheung | Hong Kong
AWSNetworking

Amazon CloudFront

Global CDN — edge caching, origins, cache behaviours, Origin Access Control, Lambda@Edge, and CloudFront Functions.

Overview

Amazon CloudFront is a global Content Delivery Network (CDN) — it caches and delivers content from 400+ edge locations worldwide, reducing latency and offloading traffic from origin servers.

CloudFront supports static and dynamic content, live and on-demand video streaming, WebSocket connections, and API acceleration. It integrates natively with S3, ALB, EC2, API Gateway, and MediaStore as origins.


Core Concepts

ConceptDescription
DistributionA CloudFront configuration that defines origins, cache behaviours, and edge settings
OriginThe source of content — S3 bucket, ALB, EC2 instance, API Gateway, or any HTTP endpoint
Edge LocationA data centre in the CloudFront global network that caches content close to users
Regional Edge CacheA larger mid-tier cache between edge locations and the origin — reduces origin fetches
Cache BehaviourRules that match URL path patterns and define caching, headers, TTLs, and edge function triggers
Origin Access Control (OAC)Restricts S3 origin access so only CloudFront can read the bucket (replaces OAI)
Cache KeyThe combination of values (URL, headers, query strings, cookies) that uniquely identifies a cached object
InvalidationRemoves cached objects from edge locations before TTL expiry

How CloudFront Works

User Request → Nearest Edge Location

                  ├── Cache HIT → Return cached response (lowest latency)

                  └── Cache MISS


                  Regional Edge Cache

                        ├── Cache HIT → Return + cache at edge

                        └── Cache MISS


                        Origin (S3, ALB, etc.)


                        Response cached at Regional Edge + Edge Location

Origin Types

OriginConfigurationNotes
S3 BucketS3 origin with OACStatic content, websites, media
ALB / EC2Custom origin (HTTP/S)Dynamic content, APIs
API GatewayCustom originServerless APIs
MediaStore / MediaPackageSpecialised originsLive/VOD streaming
Custom HTTP serverAny publicly accessible HTTP/S endpointThird-party or on-premises origins
Origin GroupPrimary + failover origin (automatic failover on 5xx/4xx)High availability for origin errors

SAA/SAP Tip: Origin Groups enable automatic origin failover. Configure a primary and secondary origin — CloudFront switches to the secondary when the primary returns configurable error codes (e.g., 502, 503, 504). This is a common HA exam pattern.


Cache Behaviours

Each distribution can have multiple cache behaviours, matched by URL path pattern (e.g., /api/*, /images/*, *).

SettingDescription
Path PatternURL pattern this behaviour applies to (/api/*, *.jpg, default *)
OriginWhich origin to forward requests to
Cache PolicyControls TTL, headers, query strings, and cookies included in the cache key
Origin Request PolicyControls which headers, query strings, and cookies are forwarded to the origin
Viewer Protocol PolicyHTTP only, HTTPS only, or Redirect HTTP to HTTPS
Edge FunctionsLambda@Edge or CloudFront Functions triggers

Security

Origin Access Control (OAC)

  • Restricts S3 bucket access to CloudFront only (bucket policy references CloudFront distribution)
  • Replaces the legacy Origin Access Identity (OAI) — OAC supports SSE-KMS, all S3 features
  • Prevents direct S3 URL access, forcing all traffic through CloudFront

HTTPS / TLS

  • Viewer ↔ CloudFront: TLS with ACM certificate (must be in us-east-1 for CloudFront)
  • CloudFront ↔ Origin: TLS with origin's certificate; supports SNI
  • Field-Level Encryption: Encrypt specific POST form fields at the edge before forwarding to origin

Geo-Restriction

  • Allowlist or Blocklist countries
  • Uses a third-party geo-IP database to determine requester location

Exam Trap: ACM certificates used with CloudFront must be provisioned in us-east-1 regardless of where the origin resides. This is a frequently tested requirement.


Edge Functions

FeatureCloudFront FunctionsLambda@Edge
RuntimeJavaScript onlyNode.js, Python
Execution location400+ edge locations (PoPs)Regional edge caches
Max execution time< 1 ms5 s (viewer) / 30 s (origin)
Max memory2 MB128–10,240 MB
Network accessNoYes
TriggersViewer request / response onlyViewer request/response + Origin request/response
Use caseHeader manipulation, URL rewrites, cache key normalisationAuth, A/B testing, image transformation, origin selection
CostLower (per invocation)Higher (Lambda pricing)

Signed URLs and Signed Cookies

MethodUse CaseScope
Signed URLRestrict access to a single fileOne URL per resource
Signed CookieRestrict access to multiple files or an entire pathSet once, applies to all URLs

Both use either a trusted key group (recommended) or a CloudFront key pair (legacy, root account only) for signing.


Common Use Cases

  • Static website acceleration — Serve S3-hosted static sites globally with low latency and TLS.
  • API acceleration — Cache API responses at the edge; reduce load on ALB/API Gateway origins.
  • Video streaming — Deliver live and on-demand video using MediaStore/MediaPackage origins.
  • Security layer — Combine with AWS WAF for rate limiting and bot protection at the edge.
  • Dynamic content — Even uncacheable content benefits from persistent connections and TCP optimisation to the origin.
  • Geo-restricted content — Enforce geographic content licensing with allowlist/blocklist.

SAA/SAP Exam Tips

SAA/SAP Tip: CloudFront + S3 with OAC is the standard pattern for serving static content globally. The bucket can remain private — CloudFront handles all public access.

Exam Trap: CloudFront cache invalidations are charged per path. For frequent content updates, use versioned file names (e.g., app.v2.js) instead of invalidating — this is both faster and cheaper.

SAA/SAP Tip: For "reduce latency for global users" or "improve performance for static/dynamic content delivery," CloudFront is almost always the answer. It benefits even dynamic, uncacheable content through TCP connection reuse and route optimisation.


Cross-Cloud Equivalents

ProviderService / SolutionNotes
AWSAmazon CloudFrontBaseline
AzureAzure CDN / Azure Front DoorFront Door adds WAF, load balancing, and routing
GCPCloud CDNIntegrated with Google Cloud Load Balancing
On-PremisesCloudflare, Akamai, FastlyThird-party CDN providers

Pricing Model

DimensionUnitNotes
Data transfer out (edge)Per GBTiered pricing; varies by region
HTTP/S requestsPer 10,000Higher rate for HTTPS
Invalidation requestsPer pathFirst 1,000 paths/month free; $0.005 per path after
Edge functionsPer invocationCloudFront Functions cheaper than Lambda@Edge
Origin ShieldPer requestOptional additional caching layer
Dedicated IP SSLPer monthOnly if non-SNI clients must be supported

Built by Fred Cheung @CookedRicer · Powered by Fumadocs & Github Copilot

On this page