This code creates multiple thumbnails with AWS lambda feature using node.js (v0.10.42) and Imagemagick library.
This example doesn't use any external npm packages (even imagemagicwrapper) as recommended here http://docs.aws.amazon.com/lambda/latest/dg/with-s3-example-deployment-pkg.html
When you or your app uploads image on S3 bucket, this code downloads image, makes thumbnails with command like this:
convert FILENAME \( +clone -quality 80 -interlace Plane -resize 264x177\> -write jpeg:- +delete \)
\( +clone -quality 80 -interlace Plane -resize 576x387\> -write jpeg:- +delete \)
\( +clone -quality 80 -interlace Plane -resize 1242x834\> -write jpeg:- +delete \)
\( +clone -quality 80 -interlace Plane -resize 5000x5000\> -write jpeg:- +delete \)
null:
then grabs binary stdout buffer, splits buffer into images, puts images back to S3, notifies you backend via Amazon SNS and optionaly removes the original file
- Create lambda function https://console.aws.amazon.com/lambda/home?region=us-east-1#/functions
- Enter any name you want
- Select runtime Node.js 0.10 (4.3 needs some refactor)
- Copy code from resizer.js to inline textarea or upload package (see https://docs.aws.amazon.com/console/lambda/nodejsdeploy)
- Set handler value
exports.resizer
- Select Role for lambda execution (or create new Basic execution role)
- If you want to upload or delete files with this lambda add policy to execution role:
{
"Action": [
"s3:DeleteObject",
"s3:PutObject",
"s3:PutObjectAcl"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::*"
}
- I recommend to change memory limit to 512 MB and timeout to 15 seconds (depends on input/output image sizes)
- Add event handler for your bucket for necessary input (e.g. input/images) and/or extensions (*.jpg, *.png)
You can modify code for you needs. Adjust thumbnail sizes, prefixes, SNS ARN topics (getTopicArn function) if you use SNS service