AWS Lambda@Edge: How to control cache TTL?
Table of contents
Hello friends! Today we will see how we can control the cache generated by the AWS CloudFront using the AWS Lambda@Edge functions.
As we all know, AWS CloudFront is a Content Delivery service which helps us in boosting our content delivery. AWS CloudFront achieves this by maintaining cache on its edge locations. Since our content is already cached, our content is delivered to the user with the lowest latency.
AWS CloudFront allows us to manage the cache built on the edge location by defining cache policies based on the requirement.
These cache policies have different parameters like what should be the default time for which the content should be caches, along with the minimum and maximum time.
But if we can use cache policies then why do we need to mimic the same using the lambda@edge function?
Actually, in ideal cases there is no need. Cache policies are capable enough to get the job done. This approach/technique is more focused towards someone who needs some customizations.
Problem
To understand the use case let's take an example. Assume that you are having a blog site just like CloudGags which is getting served using the AWS CloudFront.
There is one behaviour configures "/blog/*" which serves all your blog content. Now a situation arises that for a specific URL you need a separate TTL configured.
The above problem can be solved by creating a URL specific behaviour, but what if you are running out of behaviours.
Thats right, in AWS CloudFront there is a limit on the no. of behaviour you can create. You can create only 250 behaviours at max. Now if you have consumed all of the behaviour limit then how you can configure that one separate caching?
Solution
As a solution to the above problem, you can use Lambda@Edge functions. Lambda@Edge allows us to do some customizations on the fly while your content is getting served via the AWS CloudFront.
To control the TTL of our content we will be using the Cache-Control header. The Cache-Control header allows us to control the TTL(Time-To-Live) of the cache available on the AWS Edge locations.
#ATTACH THIS FUNCTION ON THE ORIGIN RESPONSE
import json
# URL to cache for 20 seconds
CUSTOM_CACHE_URL = ["/some/url"]
def lambda_handler(event, context):
# Extracting request and response objects from the Lambda event
request = event['Records'][0]['cf']['request']
response = event['Records'][0]['cf']['response']
# Checking for url
if request['uri'] in CUSTOM_CACHE_URL:
# Setting cache value
response['headers']['cache-control'] = [{"key": "Cache-Control","value": "s-maxage=20"}]
# Returning response
return response
Explanation
Note: We will be applying the above code on the Origin-Response event of the CloudFront.
The python code contains a list "CUSTOM_CACHE_URL" which holds all the URLS which needs to be cached for 20 seconds.
Then the function checks weather the URI falls in the list available for custom caching. If the condition is satisfied, the cache control header is set in the response and the response is returned.
The "s-maxage" value is used when we need to manipulate the cache on the edge locations. You can also use "max-age" in place of "s-maxage" if you want to manipulate the cache TTL on edge locations as well as on the user browser too.
Conclusion
In conclusion, you can dynamically manage cache settings, ensuring that your content is served quickly and efficiently. As the digital landscape continues to evolve, mastering these techniques will keep you ahead of the curve, providing your users with fast, responsive, and reliable access to your content.
Embrace AWS Lambda@Edge for effective cache management and take your website's performance to the next level.
Subscribe to my newsletter
Read articles from CloudGags directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by