Ambidextrous
Ambidextrous

Reputation: 872

How to redirect URL at AWS CloudFront?

I have a client website that serves the images through AWS CloudFront and S3. The images are in PNG format and the URL on the webpage is of the CloudFront domain rather than the custom domain. In order to reduce costs, we are planning to compress them to JPG format. However, we have found that the image URL's are hardcoded in the database.

The problem is when we compress the image to JPG, the file extension changes and thus the old URL will not work as it is. The client does not wants to make changes to the database right now.

Is there a way we can serve the compressed files (with a different extension) for the same requests coming from the webpage?

I was looking into a solution to route requests using Lambda Edge - https://aws.amazon.com/blogs/networking-and-content-delivery/dynamically-route-viewer-requests-to-any-origin-using-lambdaedge/

Is there any other optimal solution for this which you could suggest? Is there a way CloudFront to check for both types of file (jpg and png) in S3 and serve one of them? Example: request came for images/car.png -- > CloudFront to check for both images/car.png and images/car.jpg. If jpg exists serve this one.

Upvotes: 2

Views: 11248

Answers (1)

Chris Williams
Chris Williams

Reputation: 35258

As per your suggestion you will need to use a Lambda@Edge to modify the request.

You will need to deploy it for the Origin Request event before it is forwarded to the Origin.

To make the change you will update the request['uri'] value to use the origins URL rather than the one forwarded from the user.

Regarding compression, remember that CloudFront supports automatic gzip compression if you enable the option.

Upvotes: 4

Related Questions