How to create website thumbnails using AWS Lambda function?

How to create website thumbnails using AWS Lambda function?

August 14, 2020 / Eternal Team

Do you know that you can create thumbnails for images stored in S3 dynamically? If you didn’t know this and we looking to do so, then we will show you how we can create thumbnails using a simple AWS Lambda function.

Step 1.

We have created two buckets in which one bucket is the source bucket and the second one is the destination bucket.

how-to-create-website-thumbnails-using-aws-lambda-function

Above is bucket one is named “test2571961” (the source bucket).

how-to-create-website-thumbnails-using-aws-lambda-function

Bucket number two’s name is “test2571961-resized” (the destination bucket)

And we just added an image in bucket number one. (test2571961”)

Step 2.

Now create the Lambda function,

how-to-create-website-thumbnails-using-aws-lambda-function

how-to-create-website-thumbnails-using-aws-lambda-function

  • Function name: create-thumbnail
  • Runtime: Python 3.7
  • Expand: Choose or create an execution role
  • Execution role: Use an existing role
  • Existing role: lambda-execution-role (To create lambda-execution-role click here)

The role gives permission to lambda to access S3 to read and write the images.

how-to-create-website-thumbnails-using-aws-lambda-function

Now click on “ create function”

Step 3.

Configure an Amazon S3 Bucket as a Lambda Event Source.

First Click “Add trigger” and then configure

Select a trigger: S3

Bucket: Select your bucket (test2571961)

Event type: All object create events

how-to-create-website-thumbnails-using-aws-lambda-function

how-to-create-website-thumbnails-using-aws-lambda-function

how-to-create-website-thumbnails-using-aws-lambda-function

Scroll to the bottom and click “Add” Click on “create thumbnail”

how-to-create-website-thumbnails-using-aws-lambda-function

Now it’s time to configure the Lambda function.

Scroll down to the Function code and configure the following settings

Code entry type: Upload a file from Amazon S3

Runtime: Python 3.7

Handler: CreateThumbnail.handler

how-to-create-website-thumbnails-using-aws-lambda-function

Click Save at the top of the window Your Lambda function has been configured now,

Step 4.

Trigger a Lambda function by uploading an image to Amazon S3

how-to-create-website-thumbnails-using-aws-lambda-function

See the below template will be displayed that shows the event data sent to a Lambda function when it is triggered by upload into Amazon S3.

how-to-create-website-thumbnails-using-aws-lambda-function

now time for the final stage, Click “Create” and then click “Test”

how-to-create-website-thumbnails-using-aws-lambda-function

Now add some images on the source bucket and you will see logs on above. Once you get the success log, go to the S3 test2571961-resized bucket and you can see the resized image in it.

Source bucket image,

how-to-create-website-thumbnails-using-aws-lambda-function

Resized or target bucket image,

how-to-create-website-thumbnails-using-aws-lambda-function

Finally, we are ready with our dynamically resized image. For more AWS related technical information, bookmark our blog.

AWS-Consulting-Partner

Talk to AWS Certified Consultant

Want to start a project?

It’s simple.

Contact us