Study guide
Technical reference and lesson notes
Purpose of This Lesson
Lambda@Edge extends Amazon CloudFront by allowing Lambda functions to process requests and responses at specific points in the CloudFront lifecycle. This supports low-latency customization close to users, without requiring every request to travel directly to the origin for processing.
For the SAP-C02 exam, the most important skill is identifying which CloudFront event is appropriate for a required transformation or control.
Key Concepts
What Lambda@Edge Provides
Lambda@Edge allows Node.js and Python Lambda functions to customize content delivered through CloudFront. Functions execute within the CloudFront request path and can process data at four event points:
- Viewer request – after CloudFront receives a request from the viewer.
- Origin request – immediately before CloudFront forwards a request to the origin.
- Origin response – after CloudFront receives a response from the origin.
- Viewer response – immediately before CloudFront returns a response to the viewer.
The event names describe the side of CloudFront being handled and the direction of the request or response.
CloudFront Request Flow
The simplified lifecycle is:
Viewer
|
| Viewer request
v
CloudFront cache
|
| Origin request, when CloudFront needs the origin
v
Origin
|
| Origin response
v
CloudFront cache
|
| Viewer response
v
Viewer
An origin request and origin response occur when CloudFront communicates with the origin. Viewer request and viewer response events relate directly to the viewer-facing side of the distribution.
Why Placement Matters
Choosing the correct event determines when the function can influence processing:
- Use viewer request processing for logic that should run as soon as CloudFront receives the request.
- Use origin request processing when the request is about to be sent to the origin and origin-facing changes are required.
- Use origin response processing to inspect or transform the response received from the origin.
- Use viewer response processing for final changes immediately before the response is delivered to the viewer.
Exam-Relevant Takeaways
- Lambda@Edge runs Lambda functions in the CloudFront delivery path, closer to viewers than centralized application processing.
- The four CloudFront event types are viewer request, origin request, origin response, and viewer response.
- The event names are easy to confuse; memorize both the direction and the boundary involved.
- Lambda@Edge supports Node.js and Python runtimes in the context covered by this lesson.
- The event selected should match the point at which the request or response must be changed.
- Lambda@Edge is associated with CloudFront, not Route 53 DNS query processing.
Architecture Decision Guide
| Requirement | Appropriate Lambda@Edge event | Reason |
|---|---|---|
| Process a request immediately after CloudFront receives it | Viewer request | Runs on the viewer-facing request path |
| Modify a request before it is sent to the origin | Origin request | Runs immediately before origin forwarding |
| Inspect or change data returned by the origin | Origin response | Runs after the origin response reaches CloudFront |
| Make a final viewer-facing change before delivery | Viewer response | Runs immediately before CloudFront sends the response |
Common Exam Traps
- Confusing viewer request with origin request: Viewer request occurs when CloudFront receives the request. Origin request occurs later, immediately before CloudFront contacts the origin.
- Confusing origin response with viewer response: Origin response is received from the origin. Viewer response is the final processing stage before delivery to the user.
- Assuming every event contacts the origin: Viewer-side processing can occur even when CloudFront serves an object from cache. Origin events are associated with communication between CloudFront and the origin.
- Choosing Lambda@Edge merely because Lambda is required: The scenario must involve CloudFront request or response processing. Otherwise, a regional Lambda function or another service may be more appropriate.
- Ignoring execution placement: The main architectural value is processing in the CloudFront delivery path, closer to viewers.
Real-World Engineer Notes
- Document the exact event used by each function. A function that works at viewer request may not behave correctly if moved to origin response because the available request or response context differs.
- Keep edge logic focused. Request routing, lightweight transformations, and delivery customization are easier to reason about than placing broad application workflows in the CDN path.
- Analyze cache behavior when introducing request manipulation. Changes to request attributes can affect which cached object CloudFront selects and whether requests are forwarded to the origin.
- Treat edge functions as part of the production request path. Test cache hits, cache misses, origin failures, and both successful and error responses.
- Select Node.js or Python according to the implementation and operational standards of the organization, while ensuring the runtime is supported for the chosen Lambda@Edge deployment model.
Quick Reference Summary
- Viewer request: CloudFront has received the viewer request.
- Origin request: CloudFront is about to send the request to the origin.
- Origin response: CloudFront has received the origin response.
- Viewer response: CloudFront is about to send the response to the viewer.
- Primary purpose: Customize CloudFront-delivered content and request/response handling close to users.
- Runtimes covered: Node.js and Python.
Flashcards
- Q: What is Lambda@Edge used for?
A: Running Lambda functions in the CloudFront delivery path to customize content and request or response processing closer to viewers.
- Q: What are the four Lambda@Edge CloudFront events?
A: Viewer request, origin request, origin response, and viewer response.
- Q: When does the viewer request event occur?
A: After CloudFront receives a request from the viewer.
- Q: When does the origin request event occur?
A: Immediately before CloudFront forwards a request to the origin.
- Q: When does the origin response event occur?
A: After CloudFront receives a response from the origin.
- Q: When does the viewer response event occur?
A: Immediately before CloudFront forwards the response to the viewer.
- Q: Which event is associated with modifying a request before origin forwarding?
A: Origin request.
- Q: Which event is associated with processing data returned by the origin?
A: Origin response.
- Q: Which event is the final processing point before a response reaches the viewer?
A: Viewer response.
- Q: Which programming runtimes are covered for Lambda@Edge in this lesson?
A: Node.js and Python.
- Q: What is the main performance advantage of Lambda@Edge?
A: Processing occurs in the CloudFront delivery path closer to users, reducing the need to send every customization request to a centralized location.
Practice Questions
Question 1
A company uses CloudFront and needs to modify an incoming request before CloudFront sends it to an S3 origin. Which Lambda@Edge event should the architect select?
- A. Viewer request
- B. Origin request
- C. Origin response
- D. Viewer response
Correct answer: B. Origin request
Explanation: The origin request event runs immediately before CloudFront forwards a request to the origin. Viewer request occurs earlier, when CloudFront first receives the request.
Question 2
An application must inspect the response received from its origin before CloudFront continues processing it. Which event is appropriate?
- A. Viewer request
- B. Origin request
- C. Origin response
- D. Viewer response
Correct answer: C. Origin response
Explanation: Origin response processing occurs after CloudFront receives the response from the origin and before the response is delivered to the viewer.
Question 3
A solutions architect must implement a final response customization immediately before CloudFront sends content to a user. Which event should be used?
- A. Viewer request
- B. Origin request
- C. Origin response
- D. Viewer response
Correct answer: D. Viewer response
Explanation: Viewer response is the final CloudFront event before the response is forwarded to the viewer.
Question 4
A team wants to run lightweight request-processing logic in the CloudFront path, close to globally distributed users. Which AWS capability best matches this requirement?
- A. Route 53 health checks
- B. Lambda@Edge
- C. An Amazon EC2 instance in one Region
- D. An Amazon S3 lifecycle rule
Correct answer: B. Lambda@Edge
Explanation: Lambda@Edge allows Node.js and Python Lambda functions to customize CloudFront requests and responses at viewer- and origin-related events.
Question 5
A developer claims that the origin response event runs before CloudFront sends a request to the origin. Which correction is accurate?
- A. Origin response runs when CloudFront first receives the viewer request.
- B. Origin response runs immediately before the request is sent to the origin.
- C. Origin response runs after CloudFront receives the response from the origin.
- D. Origin response runs only after the response has already reached the viewer.
Correct answer: C. Origin response runs after CloudFront receives the response from the origin.
Explanation: The origin request event precedes communication with the origin. The origin response event follows the origin’s response and precedes viewer delivery.