1 Answer
- Newest
- Most votes
- Most comments
1
Yes, this could be done via Lambda@Edge. There are a few different ways that you could approach this, but I think the most straightforward would be as follows:
- Add user-defined metadata to the S3 Objects for which you want to return the rel=canonical header. eg. x-amz-meta-canonical: https://example.com/friendly-URL
- Create a Lambda@Edge function on the Origin Response trigger. This means your function will only be invoked in the event of a cache miss, and the modified version of the object will be stored in cache for future requests.
- The function should look for the x-amz-meta-canonical header, read the value, then write a new rel="canonical" header in the correct format. You could choose to remove the x-amz-meta-canonical header if you wish
Documentation:
- Working with metadata in s3: https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingMetadata.html
- Updating HTTP responses with Lambda@Edge: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-updating-http-responses.html
- Lambda@Edge Response event structure: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-event-structure.html#lambda-event-structure-response
- Lambda@Edge example of manipulating response headers: https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/lambda-examples.html#lambda-examples-overriding-response-header
An alternative approach would be to keep a mapping of the source object and the value of the rel=canonical header (if any) separate to the objects themselves, and simply look this up in your Lambda@Edge function. There are various methods for doing this, as outlined in this blog post: https://aws.amazon.com/blogs/networking-and-content-delivery/leveraging-external-data-in-lambdaedge/
However, I think this adds unnecessary complexity, and it is probably easier to store the mapping in the object metadata.
Relevant content
- Accepted Answerasked 2 years ago
- asked 7 months ago
- asked a year ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years ago