# 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

* AWS Account (If you don’t have an account [click here to create an account](https://portal.aws.amazon.com/billing/signup?sc_icampaign=consolesignout_generic_createaccount_cta&sc_ichannel=ha&sc_icontent=awssm-1111&sc_iplace=cta&trk=ha_awssm-1111&redirect_url=https%3A%2F%2Faws.amazon.com%2Fregistration-confirmation#/start))
    

### 1\. Application Flow

The following diagram illustrates the flow of the application:

![Source: Nidhinkumar — Draw.io](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056732056/df66cfd7-b92b-4bfc-a70f-914c2ce26799.png align="left")

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:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056733544/a18a1c3f-edd3-4250-ad33-179b91aead51.jpeg align="left")

Click “Create bucket”:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056734816/ead4577f-99fb-41bf-9ffc-1c3ace0525e9.jpeg align="left")

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
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056736172/af74cee8-7a9a-4456-9fa8-40345f315ac5.jpeg align="left")

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
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056737600/071be160-f6d1-4c50-9988-b34eb1104a6d.jpeg align="left")

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”:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056739213/b7072579-c42e-4d20-ac13-666d6bf3cc3a.jpeg align="left")

Click **Create a function**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056740433/5262f743-39cb-4b63-9e2c-af27c805f7e1.jpeg align="left")

Then Click **Author from Scratch**

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056742060/60b9a9a1-20a4-4a43-843d-508552b14bcb.jpeg align="left")

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](https://docs.aws.amazon.com/lambda/latest/dg/lambda-intro-execution-role.html))
    

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056743425/2e8ab273-7644-40ca-b7c0-287d9a15c35d.jpeg align="left")

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 objects create events
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056744717/c42356aa-a30e-4332-bcef-30ba5ae370e8.jpeg align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056746454/032acb94-1854-4163-8c39-a8334181516b.jpeg align="left")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056748039/8640d548-eaaf-40a6-a0fd-bafb513e210f.jpeg align="left")

Scroll to the bottom and click “Add”

Click “Create thumbnail” at the top of the diagram

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056749318/826855d6-ffda-41ae-8924-5b750fae6c6b.jpeg align="left")

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056750662/2a423bdd-7109-4a6f-9999-789cad3dba6e.jpeg align="left")

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
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056751991/09bfe5fa-a164-4cf7-b674-04c54ca6e5af.jpeg align="left")

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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056753547/870c6371-a003-45bb-bf1f-5d8b72194f01.jpeg align="left")

> 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”

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056754879/3ee7da86-b327-4bf0-8c44-091fc4e6f5bc.jpeg align="left")

You will get the success logs like the 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.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056756366/7c59c2cc-4685-444c-853a-dcf44c39b7c5.jpeg align="left")

Source Bucket

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1698056758436/467944a5-d265-4022-9623-c9a9cc7f493d.jpeg align="left")

Target Bucket

<iframe src="https://www.youtube.com/embed/EyeduLX17Mc?feature=oembed" width="700" height="393"></iframe>

### Congratulations!

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

#### Happy Learning!!!!
