- Notifications
You must be signed in to change notification settings - Fork0
This is a mix of several repos for usage of the sharp library via serverless on AWS Lambda
License
einSelbst/serverless-image-resizer
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Serverless Framework-based AWS Lambda function triggered by S3 events to resize images with the excellentSharp 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, seeadieuadieu/retinal#59
- https://github.com/danielstaleiny/serverless-example, which is based on
- https://github.com/keboola/developer-portal, seeserverless-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 theServerless 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-resizercd serverless-image-resizeryarn installOr, if you haveserverless installed globally:
serverless install -u https://github.com/einselbst/serverless-image-resizer
Then, modify theconfig.js andevent.json files, adapting them to your needs. More on configurationbelow.
You must configure your AWS credentials by definingAWS_ACCESS_KEY_ID andAWS_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 theconfig.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 aplugin for this it doesn't seem to be very mature, so it's not included yet.
Make sure the bucket inconfig.js exists.
We use Jest to run our tests. You can read more about setting up your testshere.
yarntestYou can also try out the service by invoking it. First deploy it withyarn run deploy and then you can invoke your function withyarn run invoke. This will invoke the function with the test event inevent.json. You may need to tweak this file to match your setup.
To run a function on your local machine:
$ serverless invokelocal --function resizeImageTo simulate API Gateway locally usingserverless-offline
$ serverless offline start
Deploy project to staging:
$ yarn deploy-dev
This will calldocker-compose run deploy-dev.
Deploy project to production:
$ yarn deploy
This will calldocker-compose run deploy.
Deploy a single function:
$ serverless deployfunction--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
About
This is a mix of several repos for usage of the sharp library via serverless on AWS Lambda
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
Packages0
Languages
- JavaScript93.7%
- Dockerfile6.3%