Skip to content

AWS Lambda@Edge: How to control cache TTL?

Published: at 12:00 AM

Hello friends! Today we will see how we can control the cache generated by the AWS CloudFront using the AWS Lambda@Edge functions.

Table of Contents

Open Table of Contents

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.


Previous Post
How to Use AWS CloudFront for Image Redirection to Default Images
Next Post
AWS CloudFormation: How to create a CodeCommit repository?