Galle, LK--:--5/6 sites live

Storage & Distribution: S3 + CloudFront CDN

Serving static HTML, CSS, JavaScript bundles, and images directly from an application server (like Node.js or Python) consumes valuable CPU cycles and results in sluggish load times for overseas users. The…

23 Jun 20263 min readSelf Guide · Stage 7#Nginx#DNS#W3DevOps

Lesson 23 of 26 · Module: Production web

22 min read · Source notes — Storage & Distribution + CDNs

Serving static HTML, CSS, JavaScript bundles, and images directly from an application server (like Node.js or Python) consumes valuable CPU cycles and results in sluggish load times for overseas users. The industry standard pattern is decoupling static assets to S3 and distributing them via a CDN.

Screenshot: Storage & Global Distribution Architecture

Instructor diagramming how static frontend bundles live in object storage while dynamic APIs run on compute instances.

1. What is a Content Delivery Network (CDN)?

A CDN is a geographically distributed network of proxy edge servers that cache content close to end users. If your S3 bucket is in N. Virginia (us-east-1) and a user in Tokyo opens your site:

  • Without CDN: Every image and JS chunk travels 14,000 km across the Pacific Ocean, introducing 250ms - 400ms latency per request.
  • With CDN (CloudFront): The request hits the Tokyo Edge Location in 5ms! If cached (Cache Hit), CloudFront responds instantly without ever touching your origin S3 bucket.

Screenshot: What are CDNs & Edge Locations?

Explaining edge caching, origin servers, and cache invalidation strategies.

Screenshot: Connecting CloudFront to S3 Bucket

Configuring Origin Domain, Origin Access Control (OAC), and Viewer Protocol Policy (Redirect HTTP to HTTPS).

2. Handling SPA Routing (Setting Custom Error Pages)

In client-side Single Page Applications (React, Next.js static exports, Vue, Angular), client-side routing handles paths like /dashboard or /login.

Because there is no physical file called /dashboard on S3, S3 returns a 403 Forbidden or 404 Not Found. To solve this, configure CloudFront's Custom Error Responses:

Screenshot: Setting CloudFront Custom Error Pages

Redirecting HTTP 403/404 errors to /index.html with HTTP 200 OK status code.

SettingConfigured ValueReason
HTTP Error Code403: Forbidden or 404: Not FoundTriggered when S3 cannot find a matching key object.
Customize Error ResponseYesOverrides default raw XML error page.
Response Page Path/index.htmlLoads the React/Vue bundle so client router takes over.
HTTP Response Code200: OKEnsures the browser treats the page load as successful.

Keep going: ← Lesson 22 · Lesson 24 → · Module overview · Track it in the Self Guide