Skip to content

This is a mix of several repos for usage of the sharp library via serverless on AWS Lambda

License

Notifications You must be signed in to change notification settings

einSelbst/serverless-image-resizer

 
 

Repository files navigation

Serverless-Image-Resizer

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:

Contents

  1. What is it?
  2. Installation
  3. Setup
  4. Testing
  5. Local Invocation
  6. Deployment
  7. Logs
  8. Documentation

What is it?

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

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.

Setup

Credentials

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>

Buckets

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.

Testing

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.

Local Invocation

To run a function on your local machine:

$ serverless invoke local --function resizeImage

To simulate API Gateway locally using serverless-offline

$ serverless offline start

Deployment

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

Logs

Prints the tailing logs for staging to the console:

$ yarn logs-dev

Prints the tailing logs for production to the console:

$ yarn logs

Documentation

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

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 93.7%
  • Dockerfile 6.3%