This is an Amazon AWS lambda function that automatically resizes images to smaller resolutions when they are uploaded to a S3 bucket on Amazon, so you can save some bandwidth, storage space and money.
All you will need is the index.zip file that sits in the root path of this project.
- Create a new lambda on Amazon AWS
- Put imageResizer as the name
- Leave Node.js 4.3 as the Runtime option
- In Lambda function code select "Upload .ZIP file"
- In Lambda function handler and role
- 5.1 leave index.imageResizer as the Handler
- 5.2 leave lambda_s3_exec_role as the Role
- Select 512 as memory (should be enough for most images)
- Use at least 1 minute as timeout (extra margin just to be safe)
- Click Next
- Click Create Function
- Go to Event Sources tab
- Select S3 as the event source type
- Select the target Bucket
- Select Object Created > Put as the Event type
- Optionally you can filter by the folder inside your bucket
- Optionally you can filter the type of files you want to target (the funciton will ignore anything that isn't a jpeg or png file).
If all the configuration is correct you should be able to upload an image to your target bucket (and folder if that is tha case) and see that it was resized after the upload.
You can check the logs in CloudWatch > LogGroups > aws/lambda/imageResizer
First run npm install
to install all the dependencies locally. You can change the target size in the config.js file.
/*jslint node: true */
"use strict";
/* The GM library will keep the aspect ratio */
var config = function() {
return {
width: 640,
height: 640
};
};
Then just compact the following files and folders into a zip file:
- lib
- node_modules
- index.js
- config.js
If you have the zip command line program installed you can simply run:
$ npm run build
This command will automatically compact all the necessary files into a index.zip file in the root folder.