This is a demo that uses Python Pillow within AWS Lambda to resize an image. The Pillow library is built and bundled via Serephem.
- An AWS account
- Docker
-
Once inside this example's directory, install Serephem.
$ npm i
-
Configure the
serverless.ymlfile with your own settings (i.e.region,stage,profile). Defaults are:- region:
us-east-1 - stage:
dev - profile: Ommited, thus points to
default
- region:
An S3 bucket named srphm-image-processor is created on deployment. Here you will upload your images to process.
You must upload them to the original folder. Either you can create it or will be created for you when running the test Lambda (see below).
This example features 2 Lambdas:
processor: This processor takes an image uploaded to S3 and resizes it to 128, 192 and 320 pixels, keeping the width/height ratio. It then uploads each to a different folder in the S3 bucket.test: Uploads a default provided image (tokyo.jpg) to theoriginaldirectory (creating it if it doesn't exist). This, as a result, triggers theprocessorLambda.
$ sls deploy -vThe first time you run this, you will see how Docker builds the image-processor.zip file. This file contains the Pillow library.
On subsequent deployments, the zip file will be pulled from local cache unless it is deleted or you decide to rebuild via the nocache option.
Remember to run
sls removewhen done with the demo.
-
Log in to the AWS console
-
Here you can choose 2 paths:
-
Upload an image manually
a. Go to AWS S3 and open the
srphm-image-processorbucketb. Create the
originalfolder if it doesn't existc. Open the folder and upload an image
-
Use the
testLambdaa. Go to the
testLambda in the AWS consoleb. Click the Test button to run it. Use
{}as test event.
-
-
In the bucket, you will notice 3 new folders:
w128,w192andw320. Each will contain resized versions of the uploaded images.