bytescale / aws-lambda-image-magick-resize-example

AWS Lambda image resize example (using ImageMagick to perform the image processing).

Home Page:https://www.bytescale.com/blog/aws-lambda-image-resize/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AWS Lambda Image Resize Example (with ImageMagick)

This repository deploys an AWS Lambda Function that resizes images using ImageMagick.

Blog post: https://www.bytescale.com/blog/aws-lambda-image-resize/

Quick Start

Important:

  • Replace my-lambda-function-code and my-images with unique S3 bucket names!

Deploying the Lambda Function

  1. Checkout the repository:

    git clone git@github.com:bytescale/aws-lambda-image-magick-resize-example.git
    
    cd aws-lambda-image-magick-resize-example
  2. Install NPM dependencies:

    npm install
  3. Create the S3 bucket stack (remember: change the bucket names in ParameterValue below):

    aws cloudformation create-stack \
      --stack-name my-bucket-stack \
      --parameters ParameterKey=LambdaCodeBucketName,ParameterValue=my-lambda-function-code \
                   ParameterKey=ImageBucketName,ParameterValue=my-images \
      --template-body file://buckets-cloudformation.yml
  4. Update function.js > imageBucketName to your image bucket's name.

  5. Upload the Lambda Function's code:

    zip -ry function-dist.zip .
    aws s3 cp function-dist.zip s3://my-lambda-function-code/AwsLambdaImageResizeExample.zip
  6. Deploy the Lambda Function (remember: change the bucket names in ParameterValue below):

    aws cloudformation create-stack \
      --stack-name my-image-resize-lambda \
      --parameters ParameterKey=LambdaCodeBucketName,ParameterValue=my-lambda-function-code \
                   ParameterKey=ImageBucketName,ParameterValue=my-images \
      --template-body file://function-cloudformation.yml \
      --capabilities CAPABILITY_NAMED_IAM
  7. Wait for the Lambda Function's stack to complete:

    aws cloudformation describe-stack-events \
      --stack-name my-image-resize-lambda

    Note: the latest event should read "ResourceStatus": "CREATE_COMPLETE".

Invoking the function

  1. Upload the image to resize:

    aws s3 cp test-image.jpg s3://my-images/original-image.jpg
  2. Invoke the function:

    aws lambda invoke \
      --function-name AwsLambdaImageResizeExample \
      function-result.json
  3. "See" the result:

    cat function-result.json

    Outputs:

    {
      "isBase64Encoded": true,
      "statusCode": 200,
      "headers": {
        "content-type": "image/jpg"
      },
      "body": "/9j/4AAQSkZJRg...9k="
    }

    Note: AWS Lambda only supports JSON responses. body contains the resized image as a base64-encoded string: to return the raw image, you'll need to put API Gateway in front of the Lambda function.

About

AWS Lambda image resize example (using ImageMagick to perform the image processing).

https://www.bytescale.com/blog/aws-lambda-image-resize/

License:MIT License


Languages

Language:C 99.1%Language:Shell 0.6%Language:JavaScript 0.3%