Serverless Framework-based AWS Lambda function triggered by S3 events to resize images with the excellent Sharp module. By using the Sharp module (which uses the libvips library), image processing can be 3x-5x faster than using ImageMagick, thus reducing the time your function spends running, which can potentially dramatically decrease your lambda function's cost. The function's behaviour can be controlled entirely with configuration.
This is based on the following projects:
- https://github.com/Jaikant/lambda-image-sharp, which is based on
- https://github.com/adieuadieu/retinal, see adieuadieu/retinal#59
- https://github.com/danielstaleiny/serverless-example, which is based on
- https://github.com/keboola/developer-portal, see serverless-heaven/serverless-webpack#396
A tool to take images uploaded to an S3 bucket and produce one or more images of varying sizes, optimizations and other operations. It does this by creating an AWS Lambda function with the help of the Serverless Framework. It uses Docker to build the sharp library and then deploys the Lambda function from within docker.
The current configuration is to create the following versions for a supplied image:
- original width / 6, quality 80%
- original width / 4, quality 80%
- original width / 3, quality 80%
- original width / 2, quality 80%
- original width * 2 / 3, quality 80%
- original width, quality 80%
Installation can be achieved with the following commands:
git clone https://github.com/einselbst/serverless-image-resizer
cd serverless-image-resizer
yarn install
Or, if you have serverless
installed globally:
serverless install -u https://github.com/einselbst/serverless-image-resizer
Then, modify the config.js
and event.json
files, adapting them to your needs. More on configuration below.
You must configure your AWS credentials by defining AWS_ACCESS_KEY_ID
and AWS_SECRET_ACCESS_KEY
environmental variables.
export AWS_ACCESS_KEY_ID=<your-key-here>
export AWS_SECRET_ACCESS_KEY=<your-secret-key-here>
The buckets must exist. They need to be set in the config.json
file.
When the project is deployed, the S3 trigger needs to be manually set in the AWS Lambda console: Configuration -> Add triggers -> S3 -> Choose correct source bucket -> Add -> Save
. While there is a plugin for this it doesn't seem to be very mature, so it's not included yet.
Make sure the bucket in config.js
exists.
We use Jest to run our tests. You can read more about setting up your tests here.
yarn test
You can also try out the service by invoking it. First deploy it with yarn run deploy
and then you can invoke your function with yarn run invoke
. This will invoke the function with the test event in event.json
. You may need to tweak this file to match your setup.
To run a function on your local machine:
$ serverless invoke local --function resizeImage
To simulate API Gateway locally using serverless-offline
$ serverless offline start
Deploy project to staging:
$ yarn deploy-dev
This will call docker-compose run deploy-dev
.
Deploy project to production:
$ yarn deploy
This will call docker-compose run deploy
.
Deploy a single function:
$ serverless deploy function --function resizeImage
Delete project deployment (stack & lambda functions), pass the Stage which should be destroyed (dev/production).
$ serverless remove --STAGE dev
Prints the tailing logs for staging to the console:
$ yarn logs-dev
Prints the tailing logs for production to the console:
$ yarn logs
image.js
- receives the initial event from S3 in processItem
- returns an array of promises, each promise resolves to one processed file
- invokes the get from s3
- invokes the sharp API
- invokes the uploads to s3
s3.js
- has default source and destination buckets
- implements the get from s3
- implements the uploads to s3
sharp.js
- the sharp APIs