AWS - Cloudfront

CloudFront & Edge Functions

  • Edge Functions are code that you write and attach to Cloudfront Distributions
  • Runs close to your users to minimize latency
  • Cloudfront provides 2 types of Edge Functions - CloudFront Functions & Lamdba@Edge
  • Allows us to customize the CDN content

General Use Cases - CloudFront Functions & Lamdba@Edge

  1. Dynamic Web Application at the Edge
  2. Search Engine Optimization (SEO)
  3. Intelligently Route Acoess Origins and Data Centers
  4. Bot Mitigation at the Edge
  5. Real-time Image Transformation
  6. A/B Testing
  7. User Authentication & Authorization
  8. User Prioritization
  9. User Tracking and Analytics

Cloudfront Functions

  • Lightweight Functions written in Javascript (only support Javascript)
  • For high-scale, latency-sensitive CDN customizations
  • Sub-ms startup times, millions of requests/second
  • Is a native feature of CloudFront
  • Can manage code entirely within CloudFront
  • Used to change Viewer requests and response:
    • Viewer Request: after CloudFront receives a request from a Viewer
    • Viewer Response: before CloudFront forwards the response to a Viewer
  • Free tier available
  • 1/6 the price of Lambda Edge
  • No Network Access or File System Access
  • No access to request body
  • Total package size of 10 KB
  • Max memory of 2 MB
  • Max execution time < 1ms
  • Main use cases:
    1. Cache key normalization
    2. HTTP Header manipulation
    3. URL rewrites or redirect
    4. Request Authentication & Authorization

Lamdba@Edge

  • are Lambda functions written in NodeJs & Python
  • scales to thousands of request/second
  • used to change CloudFront requests and responses:
    • Viewer Request: after CloudFront receives a request from a Viewer
    • Origin Request: before CloudFront forwards the request to the Origin
    • Origin Response: after CloudFront receives a response from a Origin
    • Viewer Response: before CloudFront forwards the response to a Viewer
  • author your functions in one AWS regions (us-east-1), then CloudFront replicates to its locations
  • Has Network Access and File System Access
  • Has access to request body
  • No Free tier, charged per request & duration
  • Total package size of 1 MB - 50 MB
  • Max memory of 128 MB, up to 10 GB
  • Max execution time of 5 - 10 seconds

Use case - To resolve NextJs static export routing