Overview
Functions and applications are deployed in a specific region, but with CloudFront edge locations distribute your content. Modern applications sometimes need to run logic at the edge before the request reaches the application. These are edge functions: code you write and attach to CloudFront distributions, run close to users to minimize latency.
CloudFront has two kinds: CloudFront Functions and Lambda@Edge.
Edge functions:
- No servers to manage, deployed globally.
- Pay for what you use, fully serverless.
- Use case: customize the CDN content coming out of CloudFront.
Use cases
- Website security and privacy
- Dynamic web applications at the edge
- Search engine optimization (SEO)
- Intelligent routing across origins and data centers
- Bot mitigation at the edge
- Real-time image transformation
- A/B testing
- User authentication and authorization
- User prioritization
- User tracking and analytics
CloudFront Request Flow
- Client request to CloudFront: viewer request.
- CloudFront request to the origin: origin request.
- Origin reply to CloudFront: origin response.
- CloudFront response to the client: viewer response.
Where edge functions run
CloudFront Functions
- Lightweight functions written in JavaScript.
- For high-scale, latency-sensitive CDN customizations.
- Sub-millisecond startup times, scale to millions of requests per second.
- Modify only the viewer request (after CloudFront receives the request from a viewer) and the viewer response (before CloudFront sends the response to the viewer).
- A native feature of CloudFront. The entire code is managed within CloudFront.
Lambda@Edge
- Functions written in Node.js or Python.
- Scale to thousands of requests per second.
- Can modify all four: viewer request, origin request (before CloudFront forwards to the origin), origin response (after CloudFront receives it from the origin), viewer response.
- You author the function in one region, us-east-1 (the region where you manage CloudFront distributions). CloudFront replicates it to all its locations.
Comparison
| CloudFront Functions | Lambda@Edge | |
|---|---|---|
| Runtime | JavaScript only | Node.js and Python |
| Scale | Millions of requests per second | Thousands of requests per second |
| Triggers | Viewer request/response only | Viewer and origin request/response |
| Max execution time | Less than 1 millisecond | 5 to 10 seconds (the transcript also says "up to 10 seconds") |
| CPU / memory | Not stated | Adjustable |
| Third-party libraries / SDK | Not stated | Yes (e.g. the SDK to reach other AWS services) |
| Network access to external services | Not stated | Yes |
| File system / HTTP body access | Not stated | Yes |
| Authored in | CloudFront | us-east-1, replicated by CloudFront |
Current Lambda@Edge quotas:
- Function timeout: 30 seconds.
- Memory: same as the normal Lambda quotas.
- Generated response size: up to 40 KB for viewer events and 1 MB for origin events.
- Throughput: 10,000 requests per second in each Region.
The course said 5 to 10 s. The exam may still use the course figure. AWS source
The instructor lists these as Lambda@Edge capabilities and does not state them for CloudFront Functions. He says "the rest, you can have a look" at the slide table, so the cells not spoken are not covered here.
CloudFront Functions use cases (under 1 ms)
- Cache key normalization: transform request attributes to create an optimal cache key.
- Header manipulation: insert, modify or delete HTTP headers in the request or response.
- URL rewrites or redirects.
- Request authorization: create and validate JWT tokens to allow or deny requests.
Lambda@Edge use cases
- Longer execution (up to 10 seconds).
The Lambda@Edge function timeout is now 30 seconds (the course said up to 10 seconds). AWS source
- Adjustable CPU and memory, so you can load many libraries and depend on third-party libraries.
- Network access to external services for data processing, for big integrations.
- File system access and access to the body of the HTTP request.
High performance, high scale, viewer request/response only, simple logic -> CloudFront Functions. Need origin triggers, longer runtime, external network, libraries or body access -> Lambda@Edge.
Questions & Answers
Q1: In which region do you author Lambda@Edge functions?
Answer:
us-east-1. CloudFront replicates them to all locations.
Q2: Which function type can change origin requests and responses?
Answer:
Lambda@Edge only. CloudFront Functions handle viewer request and response only.
Q3: Which runtimes does each support?
Answer:
CloudFront Functions: JavaScript. Lambda@Edge: Node.js and Python.
Q4: Which is best for cache key normalization, header changes, URL rewrites, JWT validation?
Answer:
CloudFront Functions (sub-millisecond, millions of requests per second).
Check yourself
Quiz
Flashcards
Click the card (or focus it), then use ← → to move and Space to flip.