Introduction
CloudFront Functions is a feature of Amazon CloudFront that enables developers to deploy and run lightweight JavaScript functions at AWS’s global edge locations closer to the end-users. This functionality facilitates high scalability and low latency and simplifies operations by allowing developers to run code that customizes the content delivered through CloudFront.
When to Use CloudFront Functions?
CloudFront Functions is ideal for simple tasks requiring high request rates and low latency.
These tasks can include a range of activities.
- Modifying Cache-keys: CloudFront allows you to change certain parts of your HTTP request (like the URL, headers, cookies, and query strings) to create a unique cache key. This key helps CloudFront remember if it has seen this object before. For instance, you could remember content based on the type of device a user is using. This could help you show different content for mobile and desktop users. It can also help you get more hits on your cache by treating multiple similar requests as the same.
- URL Changes and Redirects: You can also use CloudFront to guide visitors to a different webpage. For example, if a user isn’t logged in, you could send them from a members-only page to a login form. This feature can also be used to do A/B testing.
- Playing with HTTP headers: CloudFront lets you see, add, change, or remove any parts of the request/response headers. For example, you could add security headers to your response or copy the user’s IP address into a new header so that it can be sent to the original server.
- Authorizing Access: CloudFront lets you control who can see your content. You do this by creating and checking user tokens, like HMAC tokens or JSON web tokens (JWT), to either allow or block requests.
CloudFront Functions vs. Lambda@Edge
While both CloudFront Functions and Lambda@Edge allow you to customize web content, they have different use cases and capabilities. Lambda@Edge is more versatile and powerful than CloudFront Functions. For example, you can use Lambda@Edge to perform on-the-fly image processing jobs, such as creating thumbnails of images or converting an image format to another. However, this power comes with more operational complexity and cost.
Compared to Lambda@Edge, CloudFront Functions start and run faster, but they have limited processing power (with a maximum memory usage of 2 MB). Their capabilities are also more constrained. For example, they can’t access a file system, so they can’t read or write files, interact with databases, or make network requests to external services. Additionally, you can only attach CloudFront Functions to viewer-facing events(Viewer Requests and Viewer Response)
Viewing CloudFront Function in the CloudFront Console
The CloudFront Console offers a built-in testing platform where you can try out your code under various conditions. For example, you can work with certain types of request data or change web headers for unique replies before deploying it live. Test your function in the ‘Development’ stage first, then move it to the ‘Live’ stage to handle real-time requests.
By clicking ‘Test’, you get a snapshot of the function’s compute utilization – how much of the maximum allowed time it’s using. Remember, CloudFront Functions are best for quick, simple tasks. If compute utilization is high, your function may be too complex or slow and could benefit from some adjustments.