A Thumbnail Creator Serverless application

yinlinchen

Yinlin Chen

Posted on May 9, 2021

A Thumbnail Creator Serverless application

Thumbnail Creator

A Thumbnail Creator Serverless application. This application automatically creates a thumbnail for image, PDF, and video files. Once this application is deployed, you only need to upload files into an S3 bucket and all the thumbnails will be automatically created and saved in the result S3 bucket.

When a file is uploaded to an S3 bucket, the application will detect the file type, create a thumbnail, and save it to another S3 bucket.

Architecture

Overview

  1. Upload image, video, or pdf file to the S3 upload bucket
  2. Thumbnail files will be created and stored in the S3 result bucket

Lambda Function

  • app.py: Create a thumbnail for the image, pdf, and video file that uploaded to an S3 bucket
  • sharedutils: a custom Lambda Layer filetype - Python package to infer binary file types

Note: This application uses the below three custom Lambda Layers and you must deploy these Lambda Layers before deploying this application.

  1. ghostscript
  2. imagemagic
  3. ffmpeg

ps. Since it uses Lambda temporary storage with a fixed size of 512MB. So you may need to consider using Amazon EFS for Lambda in order to process large-size files. Learn more

Deployments

You can deploy this application directly from the AWS Serverless Application repository, on-click button CloudFormation deploy, or using SAM CLI.

  • For more information, check out the Thumbnail-Creator application in the Serverless App Repository.

  • For manual deployments and custom builds through CloudFormation or SAM, see below sections.

Deploy Thumbnail Creator application using CloudFormation stack

Step 1: Launch CloudFormation stack

Launch Stack

Click Next to continue

Step 2: Specify stack details

Name Description Example value
Stack name any valid stack name filethumbnail
ImageMagickLayer ImageMagick Lambda Layer ARN arn:aws:lambda:us-east-1::layer:image-magick:1
GhostscriptLayer Ghostscript Lambda Layer ARN arn:aws:lambda:us-east-1::layer:image-magick:1
FfmpegLayer Ffmpeg Lambda Layer ARN arn:aws:lambda:us-east-1::layer:image-magick:1
ConversionFileType a valid file type jpg
ConversionMimeType a valid mime type image/jpeg
ThumbnailWidth a valid number 150

Step 3: Configure stack options

Leave it as is and click Next

Step 4: Review

Make sure all checkboxes under Capabilities section are CHECKED

Click Create stack

Deploy Thumbnail Creator application using SAM CLI

To use the SAM CLI, you need the following tools.

To build and deploy your application for the first time, run the following in your shell:

sam build --use-container
Enter fullscreen mode Exit fullscreen mode

Above command will build the source of the application. The SAM CLI installs dependencies defined in requirements.txt, creates a deployment package, and saves it in the .aws-sam/build folder.

To package the application, run the following in your shell:

sam package --output-template-file packaged.yaml --s3-bucket BUCKETNAME --region us-east-1
Enter fullscreen mode Exit fullscreen mode

Above command will package the application and upload it to the S3 bucket you specified.

Run the following in your shell to deploy the application to AWS:

sam deploy --template-file packaged.yaml --stack-name STACKNAME --s3-bucket BUCKETNAME \
 --parameter-overrides 'ImageMagickLayer=layerARN GhostscriptLayer=layerARN FfmpegLayer=layerARN ThumbnailWidth=150' \
 --capabilities CAPABILITY_IAM --region us-east-1
Enter fullscreen mode Exit fullscreen mode

You should see the cloudformation resource like below
Resources

Usage

  1. Upload files to the UploadBucket
  2. Thumbnail files will be created and stored in the ResultsBucket

Cleanup

To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following:

aws cloudformation delete-stack --stack-name stackname
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
yinlinchen
Yinlin Chen

Posted on May 9, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related