Giter Site home page Giter Site logo

emaesen / aws-lambda-resize-images Goto Github PK

View Code? Open in Web Editor NEW
6.0 3.0 3.0 208 KB

AWS Lambda function to generate a set of resized images (large, medium, small)

License: MIT License

JavaScript 100.00%
javascript aws aws-lambda image-processing resized-images lambda

aws-lambda-resize-images's Introduction

AWS Lambda function for image resizing

This function will generate three (or more, or less) resized images for responsive websites: large, medium and small.

The functionality is based on the thumbnail generator described in http://docs.aws.amazon.com/lambda/latest/dg/walkthrough-s3-events-adminuser.html (this AWS documentation describes in detail how to setup the proper IAM user and roles).

Setup

Install npm packes async and gm (GraphicsMagick) in the root folder:

npm install async
npm install gm

Configure

Modify the code (resize_images.js) to change the configuration. If left unmodified, three sized images will be created.

// configuration as code - add, modify, remove array elements as desired
var imgVariants = [
  {
    "SIZE": "Large",
    "POSTFIX": "-l",
    "MAX_WIDTH": 1200,
    "MAX_HEIGHT": 1000,
    "SIZING_QUALITY": 70,
    "INTERLACE": "Line"
  },
  {
    "SIZE": "Medium",
    "POSTFIX": "-m",
    "MAX_WIDTH": 800,
    "MAX_HEIGHT": 800,
    "SIZING_QUALITY": 60,
    "INTERLACE": "Line"
  },
  {
    "SIZE": "Small",
    "POSTFIX": "-s",
    "MAX_WIDTH": 300,
    "MAX_HEIGHT": 400,
    "SIZING_QUALITY": 50,
    "INTERLACE": "Line"
  }
];
// The name of the destination S3 bucket is derived by
// adding this postfix to the name of the source S3 bucket:
var DST_BUCKET_POSTFIX = "-resized";

The source and destination AWS S3 buckets must exist.

Interface with AWS

The following assumes/requires the existence within your AWS account of:

  1. an "adminuser" user with administrative permission
  2. a "awsLambdaExecute" role with execute permission

Test whether lambda can be reached with the adminuser

aws lambda list-functions --profile adminuser

Create a zip file

Bundle both resize_images.js and the node_modules folder in a zip file

Upload the zip file

(run in the folder where the zip file is located):

aws lambda create-function \
--region us-west-2 \
--function-name resize_images \
--zip-file fileb://resize_images.zip \
--role arn:aws:iam::172166755826:role/awsLambdaExecute \
--handler resize_images.handler \
--runtime nodejs \
--profile adminuser \
--timeout 10 \
--memory-size 1024

Execute a test file input.json

Modify the input.json file: enter your S3 bucket name and your uploaded image name in the indicated locations. (The bucket must exist and you must have uploaded a jpg or png image for this test to succeed).

aws lambda invoke \
--invocation-type Event \
--function-name resize_images \
--region us-west-2 \
--payload file://input.json \
--profile adminuser \
outputfile.log

View execution logs

(assuming you are in region 'us-west-2')

https://us-west-2.console.aws.amazon.com/cloudwatch/home?region=us-west-2#logs:

Upload an updated zip file

In case any code changes are made, (re)zip the resize_images.js file and node_modules folder into "resize_images.zip" and re-upload.

(run in the folder where the zip file is located):

aws lambda update-function-code \
--region us-west-2 \
--function-name resize_images \
--zip-file fileb://resize_images.zip \
--profile adminuser

Execution through AWS console

Trigger the Lambda function in the source S3 bucket by adding a 'Put' Notification (send to your Lamda function) in the Events section of the bucket's Properties.

Once this is in place, uploading an image file to the source S3 bucket will automatically generate the resized images in the destination S3 bucket.

aws-lambda-resize-images's People

Contributors

emaesen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

aws-lambda-resize-images's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.