Serverless Image Handler - How to set subfolder as root

Viewed 494

Hi i got the Serverless Image Handler up and running (using this template: https://docs.aws.amazon.com/solutions/latest/serverless-image-handler/deployment.html). Deployment worked fine, all good.

I pointed it to my already existing bucket "MyBucket", and i can do image rescaling and stuff when placing images into that bucket. However we have all our images in a subfolder to that bucket, called "cloudfront_assets".

So after assigning my CNAME to the new cloudfront distribution, i am stuck with having to reference my images like this:

https://subdomain.mydomain.com/cloudfront_assets/image.jpg

instead of

https://subdomain.mydomain.com/image.jpg

I tried editing the cloudfront disitrbutions origin settings, and set "Origin Path" from /image to things like /cloudfront_assets or /image/cloudfront_assets.

It fixed the path issue, so i didnt have to write the "/cloudfront_assets/" before the image, but regardless of what i set, the image rescaling stopped working.

What is the correct way to do this?

Please help, currently stuck at the moment Set the log level to debug in the lambda function in order to see whats happening, but it only says its getting "access denied" as far as i can tell

1 Answers

The handler supports a rewrite functionality that allows you to modify the url, that is likely to be the simplest way to achieve it: https://docs.aws.amazon.com/solutions/latest/serverless-image-handler/appendix-b.html

Basically, you can rewrite all url's to always append /cloudfront_assets/, similar to how the example rewrites to add /fit-in/

Rewriting something like .* should catch pretty much everything. As the code is python based, you should use python regexp syntax.

The underlying code for the function can be found in the github repos: https://github.com/awslabs/serverless-image-handler/blob/master/source/image-handler/lambda_rewrite.py

Related