Handle large images gracefully
Closed this issue · 5 comments
Hi!
First eval thanks for this great repository :)
I've noticed a small thing that might not have been accounted for, the 6MB Lambda response limit. If a large image is requested and optimized it still might be larger than 6MB and thus Lambda will throw an error and nothing will be served to the user.
To fix this my initial idea was to add the original bucket as the second fallback to the CloudFront Distribution, this way if an image is not yet optimized and cannot be optimized by Lambda it can still be served from the original S3 bucket as is. But it seems like a single origin group only allows 2 members in it.. So maybe the Lambda could send a 307 redirect to the original bucket?
Hello, thanks for raising this.
There are some options:
- Use Streaming capability in Lambda (requires some code change), allowing for 20MB files.https://aws.amazon.com/blogs/compute/introducing-aws-lambda-response-streaming/
- Change the Lambda code to detect large image outputs, and when it does, it does not return it, but rather send a non cachable redirect to the the same URL. the next request will be served through CloudFront from the bucket.
I will consider adding the second option to this solution natively
Do you have any updated code for how the second solution would work?
ok I did a change, can you try the changed solution in this branch? https://github.com/aws-samples/image-optimization/tree/graceful-failover
merged into main