How to create website thumbnails using AWS Lambda function?

How to create website thumbnails using AWS Lambda function?

August 14, 2020 / Nirav Shah

How to create website thumbnails using AWS Lambda function?

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: Create S3 buckets and upload a sample object

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).

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

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

Step 2: Create the Lambda function

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 Amazon S3 Bucket

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: Upload image to Amazon S3 And Trigger Lambda Function

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.

Talk to AWS Certified Consultant

    Spread Love By Sharing:

    Let Us Talk About Your AWS Development Requirements

    Have queries about your AWS project ideas and concepts? Please drop in your project details to discuss with our AWS experts, professionals and consultants.

    • Swift Hiring and Onboarding
    • Experienced and Trained AWS Team
    • Quality Consulting and Programming
    Let’s Connect and Discuss Your Project