Search code examples
amazon-web-servicesamazon-s3aws-lambdawebp

AWS cron job converting images in S3 bucket to WebP


I have a client who wants the image files for their website (stored in an S3 bucket) converted to WebP. Since their customers often upload images (and don't use WebP), they wanted some script that would run periodically to search through the bucket for un-converted image files and convert them to WebP. The script would detect whether or not there was an extant WebP version of a file and, if not, create one; then, some server-side code would preferentially load the WebP version of each image if it were available.

From my research, it seems like AWS scheduled/cron jobs are done with Lambda functions via EventBridge.

Can I do this using a BASH script, or am I limited to the supported languages for Lambda functions (.NET, Go, Java, Node.js, Python, or Ruby)?

Do I have to install something in AWS in order to do the WebP processing (and how do I do that)?

Also, Lambda functions can only run for 15 minutes maximum. This probably won't be an issue once everything's set up, but the initial conversion of the images already in the bucket (easily over 1000) might well take over 15 minutes. What's a good way to convert the images that are already there?


Solution

  • What's a good way to convert the images that are already there?

    For the images which are already there, you can use AWS batch with spot instances to be cost-effective.

    Can I do this using a BASH script? you can, but for that you need to use custom runtime https://aws.amazon.com/about-aws/whats-new/2018/11/aws-lambda-now-supports-custom-runtimes-and-layers/ or different approach will be to upload your bash script as a zip inside lambda and ask lambda to execute it. https://stackoverflow.com/a/38337076/13126651

    And now the question when should lambda run? If the client wants to run periodically go for event bridge + cron + lambda architecture otherwise go for s3 upload trigger to invoke lambda https://docs.aws.amazon.com/lambda/latest/dg/with-s3-example.html