top of page

Create thumbnail images using AWS Lambda (CLI) and Amazon S3

Updated: Apr 7, 2022

Create AWS Lambda using AWS CLI and implement event-driven architecture.


Tutorial Objectives:

1. Learn to create Lambda using AWS CLI

2. Learn to implement event-driven architecture using S3 as event source to invoke Lambda function


Step 1: Create the execution role.

1. In AWS Console, top blue bar, from region drop down, select any region.

Here, Asia Pacific (Singapore) ap-southeast-1 is selected.

2. Open AWS Management Console and search for IAM.

Go to Roles and click on the Create Role.

  • Select type of trusted entity: AWS Service

  • Choose a use case: Lambda

  • Click Next: Permission

Then search for AmazonS3FullAccess and AWSLambdaBasicExecutionRole, proceed to Next Step and Review.

  • Role Name: Lambda-S3

Create role. Copy the Role ARN and paste it into the notepad we will use it in Step 4.


Step 2: Create S3 Buckets.

1. Open AWS Console in a new tab and search for S3 and create two buckets in the same region which is selected in Step 1.

  • Bucket Name: rm-thumbnail-test

Keep other settings default and create the bucket.

Follow the same procedure to create second bucket give it a name as a

  • Bucket Name: rm-thumbnail-test-resized


Step 3: Create Cloud9 environment.

1. Select the same region as Step 1 and Step 2.

Go to Cloud9 Service. Click on Create Environment.

Provide the following configuration:

  • Environment Name: Thumbnail Demo

  • Description: Cloud9 IDE for CloudPlusPlus Tutorial

Go to Next Step. Confirm the following list of default selected choices:

  • Environment type: Create a new EC2 instance for environment (direct access)

  • Instance type: t2.micro (1 GiB RAM + 1 vCPU)

  • Platform: Amazon Linux 2 (recommended)

Proceed to Next Step. Review and click on Create environment.


2. You will have the Cloud9 IDE ready in some time. A window as below will be visible.

In the bottom part of the screen, a Terminal Window is visible. If not go to the Window option in Menu Bar of Cloud9 IDE and click on New Terminal.


3. Right Click on the Thumbnail Demo and create a new folder, name it as aws-thumbnail.


4. Similarly create a new file in the AWS-thumbnail folder and name it lambda_function.py.


Double click on the lambda_function.py, open the file and paste the below code.

import boto3
import os
import sys
import uuid
from urllib.parse import unquote_plus
from PIL import Image
import PIL.Image

s3_client = boto3.client('s3')

def resize_image(image_path, resized_path):
 with Image.open(image_path) as image:
  image.thumbnail(tuple(x / 2 for x in image.size))
  image.save(resized_path)

def lambda_handler(event, context):
 for record in event['Records']:
  bucket = record['s3']['bucket']['name']
  key = unquote_plus(record['s3']['object']['key'])
  tmpkey = key.replace('/', '')
  download_path = '/tmp/{}{}'.format(uuid.uuid4(), tmpkey)
  upload_path = '/tmp/resized-{}'.format(tmpkey)
  s3_client.download_file(bucket, key, download_path)
  resize_image(download_path, upload_path)
  s3_client.upload_file(upload_path, '{}-resized'.format(bucket), key) 

Save the file by Ctrl+S.


Type the following command in the terminal to change the directory.

cd aws-thumbnail


Install pillow package using the following command.

pip install pillow


Use below command to get the location of pillow package.

pip show pillow


Copy the location and run the following command

cd /home/ec2-user/.local/lib/python3.7/site-packages


Create a zip file of pillow package and give it name as my-deployment-package, use following command.

zip -r my-deployment-package.zip .


Run ls command to check if the package is ready.

Move my-deployment-package to aws-thumbnail folder.

mv my-deployment-package.zip ~/environment/aws-thumbnail/


Use cd command to move to the root directory. Run the following command.

cd environment/aws-thumbnail/


Create a zip file of my-deployment-package and lambda_function.py

zip -g my-deployment-package.zip lambda_function.py


Step 4: Create the Lambda Function.

Create a Lambda function with the create-function command.

aws lambda create-function --function-name CreateThumbnail \

--zip-file fileb://my-deployment-package.zip --handler lambda_function.lambda_handler --runtime python3.7 \

--timeout 30 --memory-size 1024 \

--role arn:aws:iam::xxxxxxxxxxxx:role/Lambda-S3

For the role parameter, replace <arn:aws:iam::xxxxxxx:role/Lambda-S3> with your Role ARN which we copied in Step 1 and run the command.


Step 5: Add S3 trigger.

1. Go to the Lambda Service tab, Click on the CreateThumbnail Function.


2. Click Add trigger option.


3. Search for S3 and select it from the dropdown.


4. Select Source Bucket i.e rm-thumbnail-test.


5. Scroll down Acknowledge and Add trigger.


6. The S3 trigger will be added to the Lambda function.


Step 6: Upload an object to bucket.

1. Now go to S3 Management Console.

2. Click on rm-thumbnail-test bucket.


3. Click on Upload.


4. Click on Add files and upload any image



5. After Uploading check the rm-thumbnail-test-resized bucket.


Thus, we have successfully created a thumbnail image using Lambda function and S3.


Note: If you no longer need the resources, you may delete the Lambda Function, S3 Buckets and Cloud9 environment.


Was this document helpful? How can we make this document better? Please provide your insights. You can download PDF version for reference.

Create Image Thumbnail using Lambda Function and S3 FINAL
.pdf
Download PDF • 983KB

We provide best AWS trainings from Pune, India.

For aws certification contact us now.

310 views10 comments

10 Comments


Poonam Singh
Poonam Singh
Oct 16, 2023

Easily understandable

Like

Aditya Joshi
Aditya Joshi
Oct 04, 2023

Easy to understand

Like

Neha Kulkarni
Neha Kulkarni
Jun 22, 2023

Easy to understand

Like

Snehal Sutar
Snehal Sutar
Sep 16, 2022

Very Informative Blog!!

Like

Helpful Blog!

Like
bottom of page