- Newest
- Most votes
- Most comments
Take a look at this blog post which discusses how you might use AWS WAF to achieve this without having to inspect the user-agent header.
Hi, thanks for detailed explanation! Unfortunately, overriding the origin won't work on the viewer-request event. For that, you'll need to use the origin-request event. To make best use of caching, you will actually need to use both of these triggers for different purposes. Here's how you can approach it holistically:
-
use a function attached to the viewer-request event to detect if the request was made by a bot. The code that you wrote for that is great. Please add a custom header (for example
x-bot
with valuetrue
) to the request and return it for further processing by CloudFront. Tip: because you're only manipulating headers, you can stay with Lambda@Edge to implement that, or use CloudFront Functions instead. -
make use of the newly added header in a custom Cache Policy. You can do that by creating a new custom cache policy and setting that header to be included in the cache key. This way, requests that are made by bots will be served a different version of the page than regular users. The SPA itself (files like index.html, JS, CSS and other files accessed without that magic header) will then stay cached independently from prerendered objects/pages for bots (accessed from the CloudFront cache with the magic header set earlier). Tip: in theory you could just add the
user-agent
header to the cache policy, but because there are so many different values of this field, it will most probably decrease the performance of the cache substantially. With just two versions of the bot flag, you're able to send two different versions of your objects (for bots and non-bots) while still ensuring a high cache hit ratio. -
finally, use the origin-request Lambda@Edge function to set a correct origin (override the origin for requests made by bots). The origin property exists only on origin-request events - you can't set this property in the viewer-request function. That's why you need to use the second function association for that.
You can see all properties exposed by CloudFront events at Lambda event structure. You can see there that origin prop can be read and written in origin events only.
Last thing: the domainName
you're using is 'current-prerendered.s3.amazonaws.com', but the example on the event structure page is using a regional s3 endpoint, such as awsexamplebucket.s3.eu-west-1.amazonaws.com
. Please update the domainName field to include the region too.
Let us know if it worked for you, or if additional guidance is needed!
Relevant content
- asked 8 months ago
- How can I use a Lambda function to automatically start an AWS Glue job when a crawler run completes?AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago