Mid last-year AWS introduced CloudFront Functions - designed for running extra-small (sub-millisecond) operations on web requests without the overhead of a full Lambda function.
This is perfect for the use cases I wrote Lambda@Edge functions for:
- Rewriting URLs, to append
index.html
which allows pretty no-extension URLs to work with an S3 website. - Adding security headers
In fact, AWS even provides those as examples in their documentation: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-example-code.html
CloudFront Functions are only available as viewer-request and viewer-response, so they will be called more often than the origin-request and origin-response Lambda@Edge functions. But:
- They are cheaper per invocation : $0.10 per 1M invocations vs $0.60 for Lambda@Edge.
- They don’t have a memory+execution-time based charge. Lambda@Edge costs $0.00005001 per GB/second used.
- There is a free tier for CloudFront Functions: 2 million invocations per month. There is no free tier for Lambda@Edge.
These considerations are pretty theoretical for my personal website, which costs around 90c a month. But CloudFront Functions are a very attractive option for large-traffic distributions.
Step 1: Removing the existing Lambda@Edge associations
Open up the “Behaviour” tab of the CloudFront distribution config. Down the bottom are the Lambda associations. Remove them:
Then we visit a sub-page to verify the URL-rewriter has been removed, so that /blog/
no longer fetches the resource /blog/index.html
:
And also that the security headers are no longer being added:
AWS removes lambda versions automatically after there are no distributions associated with them.
Step 2: Adding CloudFront Functions
You can create functions directly from the CloudFront console: You can test the function behaviour in a similar way to testing Lambda functions. Unfortunately you can’t save multiple test cases to retry.
Once the function is published, it can be associated with a cloudfront distribution:
Now we can check the functions in the wild:
Switching to CloudFront Functions was surprisingly easy to do! I’d recommend anyone to try it if you have some extra-simple Lambda@Edge functions lying around.