Edge Function: A code that runs close to users to minimize latency. You run it on CloudFront. You have these 2 options

CloudFront Functions

  • Lightweight functions written in JavaScript
  • For high-scale, latency-sensitive CDN customizations
  • Sub-ms startup times, millions of request/second
  • Used to change Viewer requests and responses in CDN

Lambda@Edge

  • Lambda functions written in NodeJS or Python
  • Scales to 1000s of request/second
  • Can change both viewer and origin requests and responses in CDN

Comparison

FeatureCloudFront FunctionsLambda@Edge
Runtime SupportJavaScriptNode.js, Python
# of RequestsMillions of requests per secondThousands of requests per second
CloudFront TriggersViewer Request/ResponseViewer Request/Response, Origin Request/Response
Max. Execution Time< 1 ms5 – 10 seconds
Max. Memory2 MB128 MB up to 10 GB
Total Package Size10 KB1 MB – 50 MB
Network/File System AccessNoYes
Access to Request BodyNoYes
PricingFree tier available, 1/6th price of @EdgeNo free tier, charged per request & duration

Use Cases

CloudFront Functions

  • Cache key normalization
    • Transform request attributes (headers, cookies, query strings, URL) to create an optimal Cache Key
  • Header manipulation
    • Insert/modify/delete HTTP headers in the request or response
  • URL rewrites or redirects
  • Request authentication & authorization
    • Create and validate user-generated tokens (e.g., JWT) to allow/deny requests

Lambda@Edge

  • Longer execution time (several ms)
  • Adjustable CPU or memory
  • Your code depends on a 3rd libraries (e.g., AWS SDK to access other AWS services)
  • Network access to use external services for processing
  • File system access or access to the body of HTTP requests