Amazon CloudFront is a fast content delivery network (CDN) service that securely delivers data, videos, applications, and APIs to customers globally with low latency, high transfer speeds, all within a developer-friendly environment.
When building performant and globally distributed applications, Amazon CloudFront is a go-to CDN solution. One of the common use cases for CDN customization is URL redirection - think locale-based redirects, mobile vs desktop redirection, redirect/rewrite to a specific path, etc.
AWS now offers a more lightweight and cost-effective alternative: CloudFront Functions.
In this post, we’ll walk through:
- What CloudFront Functions are
- How to implement redirection logic using them
- Why you should choose CloudFront Functions over Lambda@Edge for certain use cases
- When to choose Lambda@Edge
What Are CloudFront Functions?
CloudFront Functions are lightweight, JavaScript-based functions that run at the CloudFront edge locations. They’re ideal for simple, high-performance tasks that need to execute very quickly on viewer requests — such as header manipulation, redirects, and URL rewrites.
Unlike Lambda@Edge, which uses full-fledged AWS Lambda functions, CloudFront Functions:
- Have ultra-low latency (microsecond-level)
- They are cheaper and faster to deploy
- Can scale to millions of requests per second
- Run only during the viewer request/response phases, not at origin request/response
Cloudfront Functions can be attached at 2 ends :
- Viewer Request - Runs before request reaches Cloudfront (edge cache)
- Viewer Response - Runs before Cloudfront forwards request to the client (edge cache)

If you need some of the capabilities of Lambda@Edge that are not available with CloudFront Functions, such as network access or a longer execution time, you can still use Lambda@Edge before and after content is cached by CloudFront.

Use Case: URL Redirection
Let’s say if the user visits:https://example.com/test1.html, then they'll be redirected to https://example.com/test2.html
How to Deploy
- Go to the CloudFront Console
- Click on Functions → Create function
- Give a name: Cloudfront-redirection
- Paste the below code into the editor

- Click Save and Publish the function
- Click Add Association:
- Select the CloudFront Distribution
- Choose the Viewer Request event
- Attach it to the cache behavior that covers /test1.html
Why you should choose CloudFront Functions over Lambda@Edge for certain use cases

Benefits of Cloudfront Functions over Lambda@Edge :
Ultra-Low Latency (Sub-Millisecond Execution)
CloudFront Functions are optimized for speed and run in microseconds. If you want seamless, nearly-instant redirection (e.g., /test1 → /test2.html), CloudFront Functions are measurably faster.
Significantly Lower Cost
CloudFront Functions are much cheaper than Lambda@Edge, especially at scale. With Lambda@Edge, you pay $0.60 per 1 million requests plus the execution time. With CloudFront Functions, you pay $0.10 per 1 million Invocations and nothing for execution time.
Instant Deployments (No Regional Propagation Delay)
Changes to CloudFront Functions propagate in seconds globally. With Lambda@Edge, changes must be replicated to regional edge locations, which takes more time than CloudFront functions
When to use Lambda@Edge:
- For more complex logic that requires access to external services (e.g., other AWS services).
- When you need to interact with the origin (e.g., for content generation or origin failover).
- When you need to support origin requests or response triggers.
- When you need more compute power or longer execution time
- Use of non-JS languages (like Python)
Conclusion
CloudFront Functions offer a powerful, lightweight, and cost-effective way to implement edge-level logic like URL redirection with minimal latency and near-instant deployments. For use cases that demand ultra-fast performance and simplicity—such as header manipulation, basic routing, or conditional redirects—they're an excellent alternative to Lambda@Edge.
While Lambda@Edge remains essential for more complex scenarios that involve origin access, extended execution time, or integrations with other AWS services, CloudFront Functions shine when we need blazing-fast performance at a fraction of the cost.
By leveraging the right implementation, we can build smarter, more efficient, and globally responsive applications with AWS. Whether we are optimizing for performance, cost, or both, CloudFront Functions will always be the best fit.