0
votes

I've just done this, makes images stored on s3 being resized on request and it works as charm: https://aws.amazon.com/tw/blogs/compute/resize-images-on-the-fly-with-amazon-s3-aws-lambda-and-amazon-api-gateway/

And I've just started reading articles of Lambda Edge: http://docs.aws.amazon.com/lambda/latest/dg/lambda-edge.html

But I'm still not sure what I should do with events: Viewer Request, Origin Request, Origin Response and Viewer Response.

The lambda function with gateway trigger I use for now is https://github.com/awslabs/serverless-image-resizing

3

3 Answers

1
votes

Lambda@Edge isn't quite a fit for image resizing, at this point. It is intended for more lightweight tasks, like header manipulation and small text-based response generation.

In Lambda@Edge, the deployment zip file is limited to 1 MiB, the container memory is limited to 128 MiB, the runtime is limited to 1 sec on the viewer side and 3 sec on the origin side, and although the available CPU capacity is not explicitly published it is some fraction of the capacity of 1 modern 2.3-2.4 GHz Xeon core. It's sufficient for sub-millisecond header rewrites, but not image resizing.

Now that Lambda@Edge has the ability to inspect the query string, it's conceivable that you could use it, instead of API Gateway, to invoke the second Lambda function that actually does the resize (invoke Lambda from Lambda) then convert the response from the API Gateway format into the CloudFront format, so the cost per invocation would potentially be lower (since a Lambda@Edge request costs less than an API Gateway request)... but it's not a direct substitute for what API Gateway + Lambda are doing in the established example. You would do this with an Origin Request trigger.

Lambda@Edge also can't return generated binary content (only UTF-8 character content), though that is not directly relevant to the resize example as implemented, since redirects are used to send the browser to the new target object.

It's very useful but not fully equivalent to API Gateway + Lambda.

1
votes

As of December 2017, AWS Lambda Limits are memory allocation maximum of 3008 MB and maximum execution duration per request of 300 seconds. In fact Step 2.1: Create a Deployment Package even uses the example of image resizing.

AWS Lambda@Edge has the best description of the events including a diagram. I think you want to use the Origin Request event, so your response can be cached. Also, watch AWS re:invent 2017: Building Serverless Websites with Lambda@Edge (CTD309) - YouTube for a full explanation.

0
votes

Now, Lambda@edge already supports zip packages of dozens of MB. So we can pack the sharp library into it to support our regular processing of images.

I wrote a best practice. https://github.com/foamzou/aws-lambda-edge-image-process

Using it, users only need to add a few parameters to get the processed picture. For example, convert a jpg picture into a webp picture with width of 500px and quality of 50%: https://your-cdn/image.jpg@500w_50q.webp