Table of Contents
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)
1. Application Flow
The following diagram illustrates the flow of the application:
- A user uploads an object to the source bucket in Amazon S3
- S3 detects the object create an event
- Amazon S3 publishes the object-created event to AWS Lambda by invoking the Lambda function
- AWS Lambda executes the Lambda function
- 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:
Click “Create bucket”:
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
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
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”:
Click Create a function
Then Click Author from Scratch
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.
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
Scroll to the bottom and click “Add”
Click “create thumbnail” at the top of the diagram
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
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
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.
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”
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.
Congratulations!
You have learned how to resize an image using AWS Lambda and S3