aws-samples / image-optimization

Simple, performant and cost efficient solution for optimizing images using Amazon CloudFront, Amazon S3 and AWS Lambda
MIT No Attribution
172 stars 106 forks source link

Unable to bypass and use the default behavior of cloudfront #16

Closed msonowal closed 7 months ago

msonowal commented 8 months ago

Lets say I have a image and based on certain conditions I dont want to process further and return the original behavior of cloudfront. or say the file is a video or a very big size as the lamda has limitation when I try to request those files it gives 413 error so I dont want the function to even handle that if possible that too?

AMBDA_RUNTIME Failed to post handler success response. Http response code: 413.

I tried these none worked, it just returns blank response

in functions/image-processing/index.js

return event.request;

and

return event.Records[0].cf.request;

am I doing something wrong?

achrafsouk commented 8 months ago

Hello,

Check this branch to solve your size limit problem: https://github.com/aws-samples/image-optimization/issues/1

Basically I changed the Lambda code to detect large image outputs, and when it does, it only transform it and uploads it ro S3, but does not return it to avoid errors. Then it sends a non cachable 3xx redirect to the the same URL. The redirected image request will be served through CloudFront from the S3 bucket directly.

The changes you are making are expected to return errors because this is not a Lambda@Edge function, but a Lambda function that acts as origin for CloudFront.

msonowal commented 8 months ago

but as per your code I dont see you are giving the original location,

say I have video file which is .mp4 obviously sharp cant handle in that case in your code you are not returning the actual origin file correct me if I am wrong

if (!contentType.startsWith('image/')) {
        // If not an image, return the original file without processing
        return {
    }
achrafsouk commented 8 months ago

Ah ok. In this case, you'd want just to create a new Cache Behavior in CloudFront, that catches the file extensions that do not need any processing and forward it to the origin.

Check this issue https://github.com/aws-samples/image-optimization/issues/12

msonowal commented 8 months ago

Yeah I already did that but for all the other extensions I had to add the behaviours so was not so happy about it but, thanks for your reply anyway

achrafsouk commented 8 months ago

Yes, it's a better option that triggering the Lambda and pay for it while you do not need it. However, you can possibly optimize the cache behavior settings. For example, consider using the image optimization solution only on specific file extensions (jpg, etc..), and keep the default behavior for fetching content from your origin without any transformation.