Resize an Image in AWS S3 Using a Lambda Function

Resize an Image in AWS S3 Using a Lambda Function

Overview

AWS Lambda is a compute service that runs your code in response to events and automatically manages the compute resources for you, making it easy to build applications that respond quickly to new information.

AWS Lambda starts running your code within milliseconds of an event such as an image upload, in-app activity, website click, or output from a connected device. You can also use AWS Lambda to create new back-end services where compute resources are automatically triggered based on custom requests.

Objective

  • Application Flow
  • Create an Amazon S3 Bucket
  • Create an AWS Lambda function
  • Configure an Amazon S3 bucket as a Lambda Event Source
  • Trigger a Lambda function by uploading an image to Amazon S3

Prerequisites

1. Application Flow

The following diagram illustrates the flow of the application:

Image for post

Source: Nidhinkumar — Draw.io
  1. A user uploads an object to the source bucket in Amazon S3
  2. S3 detects the object create an event
  3. Amazon S3 publishes the object-created event to AWS Lambda by invoking the Lambda function
  4. AWS Lambda executes the Lambda function
  5. From the event data it receives, the Lambda function knows the source bucket name and object key name. The Lambda function reads the objects, creates a thumbnail using graphic libraries, then saves the thumbnail image to the target bucket.

2.Create an Amazon S3 Bucket

We will create two S3 buckets one for input and another for output. Amazon S3 buckets require unique names so we will add a random number to the bucket name.

In the AWS Management Console, on the service menu click S3:

Image for post

Click “Create bucket”:

Image for post

Then add the details below:

  • Bucket Name: images-{NUMBER}
  • Replace {NUMBER} with a random number or some characters to make it unique
  • Copy the name of the bucket to a text editor
  • The region should be US West (Oregon)
  • Click Create

Image for post

Every bucket in Amazon S3 requires a unique name such as images-123565555. If you receive an error stating bucket name is not available to change the bucket name and try again.

Click “Create Bucket” and then configure the below details

  • Bucket Name: Paste the name of the previous bucket you have created and append -resized at the end of the name
  • Click Create
  • Do not change the region

Image for post

Now you have the buckets named similar to

  • test2571961
  • test2571961-resized

You can now upload a picture to the bucket test2571961

upload an image named TestImage.jpg in test2571961 bucket

3.Create an AWS Lambda function

We will create an AWS Lambda function that reads an image from the test2571961 bucket, resizes the image, and then store it in test2571961-resized

On the service, menu click “Lambda”:

Image for post

Click Create a function

Image for post

Then Click Author from Scratch

Image for post

Add the below details

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

This role grants permission to the AWS Lambda function to access Amazon S3 to read and write the images.

Image for post

Click Create Function

4. Configure an Amazon S3 Bucket as a Lambda Event Source

Click Add trigger and then configure

  • Select a trigger: S3
  • Bucket: Select your bucket (test2571961)
  • Event type: All object create events

Image for post

Image for post

Image for post

Scroll to the bottom and click “Add”

Click “create thumbnail” at the top of the diagram

Image for post

We will now 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

Make sure you set the Handler field to the above value, otherwise the Lambda function will not be found

Image for post

Copy the below Amazon S3 link URL and paste the link

https://s3-us-west-2.amazonaws.com/us-west-2-aws-training/awsu-spl/spl-88/2.3.prod/scripts/CreateThumbnail.zip

Click Save at the top of the window

Your Lambda function has been configured now

5. Trigger a Lambda function by uploading an image to Amazon S3

At the top of the screen click Test then configure

  • Event Template: Amazon S3 Put
  • Event name: Upload

Image for post

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

Image for post

Before clicking create make sure you have an image named TestImage.jpg in your test2571961 bucket or if you are having some other image in the bucket copy that name and replace the TestImage.jpg name with your image name in the below code block

"Object": {
   key:  TestImage.jpg or your image name 
}

Click “Create” and then click “Test”

Image for post

You will get the success logs like above. Once you get the success log, go to the S3 test2571961-resized bucket you can see the resized image in it.

If not, upload an image to test2571961 bucket and then check test2571961-resized bucket now you should see the resized image.

Image for post

Source Bucket

Image for post

Target Bucket

Congratulations!

You have learned how to resize an image using AWS Lambda and S3

Source