leonfancy / image-lambda

An AWS Lambda function that AWS S3 can invoke to create thumbnails or reduce file size for png and jpg images.
79 stars 11 forks source link
aws-lambda image-lambda resized-images

image-lambda

Build Status

An AWS Lambda function that AWS S3 can invoke to create thumbnails or reduce file size for png and jpg images.

Features:

Dependencies

How it works

Once you deployed image-lambda package to AWS Lambda and configured it. When an image is uploaded to AWS S3 bucket, S3 sends an notification to AWS Lambda and invokes the image-lambda function. image-lambda reduce/resize the image based on configuration and then put the processed images to target bucket/directory.

AWS Lambda

image-lambda use GraphicsMagick to resize image, and imagemin to reduce image file size.

Installation

git clone https://github.com/SlimFancy/image-lambda.git
cd image-lambda
npm install

Configuration

image-lambda supports configuration for reduce/resize image. There is config.json.sample in project root directory as example. You can copy to use it.

$ cp config.json.example config.json

Here is an example of configuration:

{
  "reduce": {
    "sourceDir": "images/uploads",
    "targetBucket": "example",
    "targetDir": "images/reduce",
    "ACL": "public-read"
  },
  "resizes": [
    {
      "width": 100,
      "sourceDir": "images/uploads",
      "targetBucket": "example",
      "targetDir": "images/100w",
      "ACL": "public-read"
    },
    {
      "height": 200,
      "sourceDir": "images/uploads",
      "targetBucket": "example",
      "targetDir": "images/200h",
      "ACL": "public-read"
    },
    {
      "height": 200,
      "width": 200,
      "resizeOption": "^",
      "sourceDir": "images/uploads",
      "targetBucket": "example",
      "targetDir": "images/200",
      "format": "jpg",
      "ACL": "public-read"
    }
  ]
}

Create AWS Lambda deployment package

As AWS Lambda function actually running on Amazon Linux, if you compile node modules on you local machine which is not Linux system, some binaries (such as imagemin used in this project) might not work after you deploy function to AWS Lambda.

In order to solve this problem, Vagrant is used to provision a Linux machine locally, and the Lambda deployment package is built in this vagrant machine.

A npm script is created to provision the Linux machine and create the deployment zip package.

$ npm run vagrant:build

This command takes a few ten minutes the first time you run it, because it needs to download the Linux image and install node environment in the virtual machine. After this command finished, you can find the zip package in build/ folder with name image-lambda.zip.

If your local operating system is already Linux (such as Ubuntu, CentOS), you can just run below command to build the package:

$ npm run build

Contributing

If you'd like to contribute to the project, feel free to submit a PR.

Run tests:

$ npm test

test/fixture directory contains jpg and png images for testing. After you run tests, the reduced and resized images are stored in test/out.

License

MIT