3
3
Table of Contents

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 :

  1. Viewer Request - Runs before request reaches Cloudfront (edge cache)
  2. Viewer Response - Runs before Cloudfront forwards request to the client (edge cache)
Working of Cloudfront functions
Source: AWS

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.

Working of Cloudfront functions
Source: AWS

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

  1. Go to the CloudFront Console
  2. Click on Functions → Create function
  3. Give a name: Cloudfront-redirection
  4. Paste the below code into the editor
Code for deployment
  1. Click Save and Publish the function
  2. 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

Why you should choose CloudFront Functions over Lambda@Edge for certain use cases

Benefits of Cloudfront Functions over Lambda@Edge :

  1. 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.

  2. 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.

  3. 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.

12
Let's discuss your cloud challenges and see how CloudKeeper can solve them all!
Meet the Author
  • rohit
    Senior DevOps Engineer

    Rohit is passionate about designing and implementing scalable, secure, and efficient DevOps solutions including automation pipelines, cloud architectures, and infrastructure as code.

0 Comment
Leave a Comment

Speak with our advisors to learn how you can take control of your Cloud Cost