SAA-C03Chapter 127 of 189Objective 3.4

Lambda@Edge vs CloudFront Functions

This chapter covers two powerful AWS edge compute services: CloudFront Functions and Lambda@Edge. Both allow you to run code in response to CloudFront events, but they differ significantly in runtime, capabilities, performance, and use cases. For the SAA-C03 exam, understanding these differences is critical as questions often test your ability to select the correct service based on latency requirements, compute needs, and event types. Expect 1-2 questions on this topic, typically asking which service to use for a specific scenario involving header manipulation, URL rewrites, or authentication at the edge.

25 min read
Intermediate
Updated May 31, 2026

Fast Food Drive-Thru vs. Full Kitchen Renovation

CloudFront Functions is like a fast-food drive-thru speaker system. When you place an order, the employee at the speaker takes it, confirms the total, and tells you to pull forward. This interaction is extremely fast because it's limited to simple operations: taking the order, checking the menu, and providing a quick response. The employee cannot cook the food or handle complex modifications; they just process the order and pass it to the kitchen. Similarly, CloudFront Functions operates at the edge with minimal compute, executing lightweight JavaScript functions on viewer requests/responses in under 5 milliseconds. They are ideal for simple header manipulations, URL rewrites, or cache key normalization, all done at the edge with virtually no latency.

Lambda@Edge, on the other hand, is like a full kitchen renovation. You can remodel the entire kitchen — change the layout, install new appliances, and even cook complex meals. But this takes more time and resources; you need to plan, get permits, and execute the work. Lambda@Edge runs in a full Node.js, Python, or other runtime environment, allowing network calls, file system access, and heavy computation. However, it has higher latency (up to 30 seconds for origin responses) and can be invoked for origin-facing events, not just viewer-facing ones. It's like having a complete team of chefs and contractors available at the edge, but the process is slower and more expensive. Choosing between them depends on whether you need a simple, ultra-fast tweak (CloudFront Functions) or a complex, compute-intensive transformation (Lambda@Edge).

How It Actually Works

What Are CloudFront Functions and Lambda@Edge?

CloudFront Functions and Lambda@Edge are both edge compute services that allow you to run code in response to CloudFront events, processing requests and responses at AWS edge locations before they reach the origin or the viewer. However, they are built on fundamentally different architectures and are intended for different use cases.

CloudFront Functions is a lightweight, low-latency option that runs JavaScript code (ECMAScript 5.1 compliant) at CloudFront edge locations. It is designed for high-scale, submillisecond operations such as URL rewrites, HTTP header manipulation, cache key normalization, and simple authentication or authorization checks. CloudFront Functions execute in a dedicated, isolated environment with no network access and no access to the file system. They are ideal for operations that must happen quickly and at massive scale, with thousands of requests per second per distribution.

Lambda@Edge, on the other hand, is an extension of AWS Lambda that runs functions at CloudFront edge locations. It supports multiple runtimes (Node.js, Python, Java, Go, .NET, Ruby) and provides full access to the Lambda execution environment, including network access, environment variables, and up to 10 GB of ephemeral storage. Lambda@Edge is designed for more complex operations that require significant compute, such as user authentication, database lookups, dynamic content generation, or integration with other AWS services. However, this flexibility comes with higher latency, longer execution timeouts, and higher cost.

How They Work Internally

CloudFront Functions are executed in a V8-based JavaScript engine that is tightly integrated with CloudFront's request processing pipeline. When a viewer request arrives at an edge location, CloudFront evaluates any associated function based on the event type (viewer request or viewer response). The function runs synchronously, blocking the request/response flow until it completes. The execution must finish within 1 millisecond for viewer events and 5 milliseconds for viewer response events (hard limit). The function has no access to the network, file system, or environment variables; it can only manipulate the request/response object (e.g., headers, cookies, query strings, URI) and return it. Because the runtime is minimal, CloudFront Functions can handle millions of requests per second with very low latency (submillisecond).

Lambda@Edge functions are triggered by CloudFront events and run in a standard Lambda execution environment. When a CloudFront event occurs (e.g., viewer request, origin request, origin response, viewer response), CloudFront sends the event data to the Lambda function, which runs asynchronously (but the CloudFront processing waits for the response). The function can make network calls to external services, read from S3, query DynamoDB, or invoke other Lambda functions. The maximum execution time is 5 seconds for viewer events and 30 seconds for origin events. Lambda@Edge functions have access to up to 10 GB of memory and 512 MB of ephemeral storage. However, because they are standard Lambda functions, they have cold start latency, especially when using larger runtimes or VPC configurations. CloudFront caches the function's response for the same event type and distribution, but the first invocation can be slow.

Key Components, Values, and Defaults

CloudFront Functions: - Runtime: JavaScript (ECMAScript 5.1) - Event types: Viewer request, viewer response - Maximum execution time: 1 ms (viewer request), 5 ms (viewer response) - Memory: 128 MB (fixed, cannot be changed) - Network access: None - File system access: None - Environment variables: None - Maximum function size: 10 KB (code only, no dependencies) - Pricing: $0.10 per million invocations (first 2 million free per month) - Concurrency: No limit beyond CloudFront distribution capacity

Lambda@Edge: - Runtimes: Node.js 18, Python 3.11, Java 11, Go 1.x, .NET 8, Ruby 3.2 - Event types: Viewer request, viewer response, origin request, origin response - Maximum execution time: 5 seconds (viewer events), 30 seconds (origin events) - Memory: 128 MB to 10 GB (in 1 MB increments) - Network access: Yes (can access VPC resources if configured) - File system access: Yes (ephemeral storage up to 512 MB) - Environment variables: Yes (up to 4 KB) - Maximum function size: 1 MB (console editor), 50 MB (zipped, including layers) - Pricing: $0.60 per million invocations (first 1 million free per month) plus compute time - Concurrency: Limited by Lambda account limits (default 1000 concurrent executions per region)

Configuration and Verification

To create a CloudFront Function, you use the AWS Management Console, CLI, or SDK. In the console, navigate to CloudFront > Functions > Create function. Write your JavaScript code directly in the editor (max 10 KB). After testing with sample events, publish a version and associate it with a CloudFront distribution behavior for a specific event type (viewer request or viewer response). You can also use the AWS CLI:

aws cloudfront create-function --name MyFunction --function-config Comment="My function",Runtime=cloudfront-js-1.0 --function-code "function handler(event) { var request = event.request; request.headers['x-custom'] = {value: 'test'}; return request; }"

To create a Lambda@Edge function, you first create a Lambda function in the Lambda console (same region as your distribution, typically us-east-1) with the appropriate IAM role (must include a trust policy for lambda.amazonaws.com and edgelambda.amazonaws.com). After creating the function, you publish a version and add a CloudFront trigger from the Lambda console or CloudFront console. The trigger specifies the CloudFront distribution, behavior, and event type. Verification can be done by checking CloudFront logs or using curl with custom headers.

Interaction with Related Technologies

Both services integrate with CloudFront's cache behavior. CloudFront Functions can modify the cache key (by altering headers, cookies, or query strings) to influence caching behavior. Lambda@Edge can also modify the cache key and can perform more complex logic like checking authentication tokens before allowing cached content to be served. Additionally, Lambda@Edge can interact with other AWS services like DynamoDB, S3, or even external APIs to dynamically generate responses or redirect users. CloudFront Functions cannot make any external calls, so they rely solely on the request/response object.

Use Case Scenarios

CloudFront Functions: - URL redirects (e.g., redirecting from HTTP to HTTPS) - Adding security headers (e.g., HSTS, CSP) - Normalizing query strings for cache optimization - A/B testing by setting cookies - Simple authentication using pre-shared keys in headers

Lambda@Edge: - User authentication against a database (e.g., DynamoDB) - Image resizing on-the-fly using the origin response - Generating dynamic content based on user location or device - Integrating with third-party APIs for personalization - Complex URL rewrites that require external data

Performance and Cost Considerations

CloudFront Functions are extremely fast and cheap. They are ideal for high-volume, low-latency operations. Lambda@Edge is slower (due to cold starts and network I/O) and more expensive, but it offers far greater flexibility. For the SAA-C03 exam, the key differentiator is latency: if the operation must complete in under 1 ms, use CloudFront Functions. If you need network access or longer execution times, use Lambda@Edge.

Walk-Through

1

Identify the Event Type

Determine which CloudFront event triggers your code. There are four event types: viewer request (when CloudFront receives a request from the viewer), origin request (when CloudFront forwards the request to the origin), origin response (when CloudFront receives a response from the origin), and viewer response (when CloudFront sends the response to the viewer). CloudFront Functions only support viewer events (request and response). Lambda@Edge supports all four. Choosing the wrong event type can lead to incorrect behavior or increased latency.

2

Choose the Right Service

Based on the event type and complexity, select CloudFront Functions or Lambda@Edge. If the logic is simple (header manipulation, URL rewrite, cache key normalization) and must complete in under 1 ms, use CloudFront Functions. If you need network access, longer execution time (up to 30 seconds), or complex computation (authentication, dynamic content generation), use Lambda@Edge. The exam often tests this decision: for example, 'You need to redirect users to a mobile version of your site based on the User-Agent header. The redirect must happen with minimal latency.' The answer is CloudFront Functions because it's a simple header check and redirect.

3

Write and Test the Code

For CloudFront Functions, write JavaScript code in the CloudFront console or CLI. The function must export a handler that receives an event object containing request or response. The function must return the request or response object. Test using the built-in testing tool with sample events. For Lambda@Edge, write code in the Lambda console using your preferred runtime. The handler receives an event object with Records containing CloudFront event data. Test using sample events provided by AWS. Both services require you to publish a version before associating with a distribution.

4

Associate with CloudFront Distribution

In CloudFront console, go to the Behaviors tab of your distribution. Choose a behavior (e.g., default cache behavior) and edit it. Under 'Function associations', you can add a CloudFront Function or Lambda@Edge function for each event type. For CloudFront Functions, select the function and version. For Lambda@Edge, select the Lambda function ARN (including version) and the event type. CloudFront will then execute the function at the appropriate point in the request/response flow. Note that changes may take a few minutes to propagate to all edge locations.

5

Monitor and Troubleshoot

Monitor CloudFront Functions using CloudFront metrics (e.g., function invocations, errors, latency) and logs (if you enable real-time logs or access logs). For Lambda@Edge, use CloudWatch Logs and metrics. Common issues include: CloudFront Functions timing out (if code exceeds 1 ms), Lambda@Edge cold starts causing high latency, incorrect IAM permissions (Lambda@Edge needs trust policy for edgelambda.amazonaws.com), and function errors causing 502 responses. Use CloudWatch to view error logs and adjust code accordingly.

What This Looks Like on the Job

Enterprise Scenario 1: Global E-commerce Platform with A/B Testing

A global e-commerce company wants to run A/B tests on their product pages by showing different variants to users based on a cookie. They need to add a cookie to a subset of users (e.g., 10% of traffic) and then use that cookie to serve different content from the origin. The operation must not add significant latency because even 100 ms delay can reduce conversion rates. They choose CloudFront Functions for the viewer request event to set the cookie if it doesn't exist, using a simple random number generator. The function runs in under 1 ms and scales to millions of requests per second. Misconfiguration: if they used Lambda@Edge, the cold start would add 200-500 ms latency to every request, degrading user experience. Additionally, the cost would be significantly higher. The correct approach is CloudFront Functions for cookie injection, combined with origin-side logic to read the cookie and serve the appropriate variant.

Enterprise Scenario 2: Media Streaming Service with User Authentication

A media streaming service needs to authenticate users before allowing them to access video content. The authentication involves checking a JWT token against a DynamoDB table to verify the user's subscription status. This requires network access to DynamoDB and potentially complex token validation logic. They choose Lambda@Edge for the viewer request event. The Lambda function extracts the JWT from the Authorization header, decodes it, queries DynamoDB, and either allows the request (by passing it through) or returns a 401 response. The function has a 5-second timeout, which is sufficient for the database query. If they used CloudFront Functions, they could not access DynamoDB, so authentication would be impossible. However, they must consider cold start latency: for a global audience, they should keep the Lambda function warm by invoking it periodically (e.g., using CloudWatch Events). Misconfiguration: not setting the correct IAM role for Lambda@Edge (must include edgelambda.amazonaws.com in the trust policy) will cause the function to fail with a 502 error.

Scenario 3: News Website with Image Resizing

A news website serves millions of images daily at various resolutions (thumbnails, mobile, desktop). They want to resize images on-the-fly when a new resolution is requested, rather than storing all variants. They use Lambda@Edge for the origin response event: when an image is fetched from the origin (S3), the Lambda function checks the requested dimensions from the query string, resizes the image using a library like Sharp, and returns the resized image. The function runs in under 5 seconds (within the origin response timeout). This avoids storing multiple copies and reduces S3 storage costs. CloudFront Functions cannot perform image processing because they lack network access and compute power. Misconfiguration: if the Lambda function takes too long (e.g., >30 seconds for very large images), CloudFront will time out and return a 504 error. In production, they would set a maximum execution time and use CloudFront's error handling to fall back to a default image.

How SAA-C03 Actually Tests This

SAA-C03 Objective Coverage

This topic falls under Domain 3 (High Performance) and objective 3.4: 'Determine an edge computing strategy based on application requirements.' The exam specifically tests your ability to choose between CloudFront Functions and Lambda@Edge for a given scenario. Key differentiators: latency requirements, compute complexity, event types, and network access.

Common Wrong Answers and Why

1.

'Use Lambda@Edge for all edge processing because it's more powerful.' Candidates choose this because they think 'more powerful' is always better. However, the exam emphasizes cost and latency. For simple tasks like header manipulation, CloudFront Functions is the correct answer because it's faster and cheaper.

2.

'CloudFront Functions can access DynamoDB for authentication.' This is wrong because CloudFront Functions have no network access. Candidates confuse CloudFront Functions with Lambda@Edge or think that CloudFront Functions are similar to Lambda. The exam will test this by describing a scenario that requires a database lookup.

3.

'Lambda@Edge can only be used for viewer request events.' Lambda@Edge supports all four event types, while CloudFront Functions only support viewer events. Candidates might think Lambda@Edge is limited to viewer events because CloudFront Functions are, but that's incorrect.

4.

'CloudFront Functions can run for up to 5 seconds.' The maximum execution time for CloudFront Functions is 1 ms for viewer request and 5 ms for viewer response. Candidates often misremember the limits. The exam may give a scenario requiring a 2-second operation, which rules out CloudFront Functions.

Specific Numbers and Terms

CloudFront Functions: max 1 ms (viewer request), 5 ms (viewer response), 10 KB code size, $0.10/million invocations.

Lambda@Edge: max 5 seconds (viewer events), 30 seconds (origin events), 50 MB zip size, $0.60/million invocations plus compute.

Event types: viewer request, viewer response, origin request, origin response.

CloudFront Functions runtime: JavaScript (ECMAScript 5.1).

Lambda@Edge runtimes: Node.js, Python, Java, Go, .NET, Ruby.

Edge Cases and Exceptions

CloudFront Functions cannot use environment variables. If the scenario requires passing configuration without hardcoding, Lambda@Edge is needed.

Lambda@Edge functions must be in us-east-1 region. This is a common exam detail: you create the Lambda function in us-east-1 even if your CloudFront distribution is elsewhere.

Lambda@Edge functions cannot be associated with a CloudFront distribution that uses a custom origin with an SSL certificate from a different account? Actually, they can, but the IAM role must be properly configured.

CloudFront Functions are not billed for compute time, only invocations. Lambda@Edge bills for both invocations and compute duration.

How to Eliminate Wrong Answers

When you see a scenario question, first ask: 'Does the code need network access or file system access?' If yes, eliminate CloudFront Functions. Next, ask: 'What is the maximum allowed latency?' If the operation must complete in under 1 ms, eliminate Lambda@Edge. Then, consider the event type: if it's an origin event, only Lambda@Edge works. Finally, check the complexity: if the code is simple (e.g., add a header, redirect), CloudFront Functions is likely the answer. If it involves database calls or heavy computation, choose Lambda@Edge.

Key Takeaways

CloudFront Functions are for ultra-low-latency operations (under 1 ms) on viewer events only.

Lambda@Edge supports all four CloudFront event types: viewer request/response and origin request/response.

CloudFront Functions cannot make network calls; Lambda@Edge can access any AWS service or external endpoint.

Lambda@Edge functions must be created in the us-east-1 region.

CloudFront Functions have a maximum code size of 10 KB; Lambda@Edge allows up to 50 MB.

For simple header manipulation or URL rewrites, always choose CloudFront Functions for best performance and cost.

For authentication, database queries, or dynamic content generation, use Lambda@Edge.

Lambda@Edge viewer events have a 5-second timeout; origin events have a 30-second timeout.

Easy to Mix Up

These come up on the exam all the time. Here's how to tell them apart.

CloudFront Functions

Runs JavaScript only (ECMAScript 5.1)

Max execution time: 1 ms (viewer request), 5 ms (viewer response)

No network access, no file system, no environment variables

Max function size: 10 KB

Cost: $0.10 per million invocations

Lambda@Edge

Supports Node.js, Python, Java, Go, .NET, Ruby

Max execution time: 5 seconds (viewer events), 30 seconds (origin events)

Full network access, up to 512 MB ephemeral storage, environment variables

Max function size: 50 MB (zipped, including layers)

Cost: $0.60 per million invocations plus compute time

Watch Out for These

Mistake

CloudFront Functions can make network calls to AWS services.

Correct

CloudFront Functions have no network access. They cannot call DynamoDB, S3, or any external service. Lambda@Edge is required for any network I/O.

Mistake

Lambda@Edge functions can run for up to 30 seconds for viewer events.

Correct

For viewer request and viewer response events, the maximum execution time is 5 seconds. The 30-second timeout applies only to origin request and origin response events.

Mistake

CloudFront Functions are just a subset of Lambda@Edge with limited runtimes.

Correct

CloudFront Functions are a completely separate service built on a different architecture (V8 engine) optimized for ultra-low latency. They are not Lambda functions.

Mistake

You can use any Lambda runtime for Lambda@Edge.

Correct

Lambda@Edge supports only specific runtimes: Node.js, Python, Java, Go, .NET, and Ruby. Custom runtimes are not supported.

Mistake

CloudFront Functions can be triggered by origin request events.

Correct

CloudFront Functions only support viewer request and viewer response events. Origin request and origin response events are only available for Lambda@Edge.

Do You Actually Know This?

Reveal each answer, then mark whether you got it right. Score 60%+ to unlock the next chapter.

Frequently Asked Questions

What is the difference between CloudFront Functions and Lambda@Edge?

CloudFront Functions are lightweight JavaScript functions that run in under 1 ms on viewer events, with no network access. Lambda@Edge are full Lambda functions with network access, supporting multiple runtimes, and can run on all four CloudFront event types with up to 30-second timeouts. Choose CloudFront Functions for simple, high-speed operations; Lambda@Edge for complex, compute-intensive tasks.

Can CloudFront Functions access DynamoDB?

No, CloudFront Functions have no network access. They cannot call DynamoDB, S3, or any external service. Use Lambda@Edge if you need to query a database or make any network call.

What are the execution time limits for Lambda@Edge?

For viewer request and viewer response events, the maximum is 5 seconds. For origin request and origin response events, the maximum is 30 seconds. CloudFront Functions have a 1 ms limit for viewer request and 5 ms for viewer response.

Which AWS region must Lambda@Edge functions be created in?

Lambda@Edge functions must be created in the US East (N. Virginia) region (us-east-1). This is a requirement for associating them with CloudFront distributions.

Can I use Python for CloudFront Functions?

No, CloudFront Functions only support JavaScript (ECMAScript 5.1). For Python, you must use Lambda@Edge.

What is the cost difference between CloudFront Functions and Lambda@Edge?

CloudFront Functions cost $0.10 per million invocations (first 2 million free). Lambda@Edge costs $0.60 per million invocations plus compute time (per GB-second). For high-volume simple operations, CloudFront Functions are significantly cheaper.

Can Lambda@Edge functions be used for origin request events?

Yes, Lambda@Edge supports origin request events. CloudFront Functions do not support origin events.

Terms Worth Knowing

Ready to put this to the test?

You've just covered Lambda@Edge vs CloudFront Functions — now see how well it sticks with free SAA-C03 practice questions. Full explanations included, no account needed.

Done with this chapter?