A Hands-on Introduction to AWS Lambda using a Serverless Image Thumbnail Application
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.
In this lab, you will perform the following:
Create an AWS Lambda function
Configure an Amazon S3 bucket as a Lambda Event Source
Trigger a Lambda function by uploading an object to Amazon S3
Monitor AWS Lambda S3 functions through Amazon CloudWatch Log
This lab demonstrates AWS Lambda by creating a serverless image thumbnail application.
This is how the application works:
1 A user uploads an object to the source bucket in Amazon S3 (object-created event).
2 Amazon S3 detects the object-created event.
3 Amazon S3 publishes the object-created event to AWS Lambda by invoking the Lambda function and passing event data as a function parameter.
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 object and creates a thumbnail using graphics libraries, then saves the thumbnail to the target bucket.
Task 1: Create the S3 Buckets
In this task, you will create two Amazon S3 buckets – one for input and one for output. Note that S3 buckets require unique names, so you will add a random number to the bucket name such as images-33522332.
Input Bucket: images-<random-number>
Output Bucket: images-<random-number>-resized
You will now upload a picture for testing purposes.
Right-click this link and download the picture to your computer: HappyFace.jpg
Open the image on your computer.
It is a large picture, with dimensions of 1280 x 853.
Upload the image into your input bucket (Not the -resized bucket)
Task 2: Create an AWS Lambda Function
In this task, you will create an AWS Lambda function that reads an image from Amazon S3, resizes the image and then stores the new image in Amazon S3.
Blueprints are code templates for writing Lambda functions. Blueprints are provided for standard Lambda triggers such as creating Alexa skills and processing Amazon Kinesis Firehose streams. This lab provides you with a pre-written Lambda function, so you will Author from scratch.
Choose Author from scratch
In the Create function window, configure:
Function name: Create-Thumbnail
Runtime: Python 3.7
Expand Change default execution role
Execution role: Select Use an existing role
Existing role: Choose lambda-execution-role
Make sure to select Python 3.7 under Other supported runtime. If you select Python 3.8 from Latest supported list, the code will fail.
This role grants permission to the Lambda function to access Amazon S3 to read and write the images.
The following is the JSON of the lambda-execution-policy attached to the lambda-execution-role
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"logs:*"
],
"Resource": "arn:aws:logs:*:*:*",
"Effect": "Allow"
},
{
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": "arn:aws:s3:::*",
"Effect": "Allow"
}
]
}
AWS Lambda functions can be triggered automatically by activities such as data being received by Amazon Kinesis or data being updated in an Amazon DynamoDB database. For this lab, you will trigger the Lambda function whenever a new object is created in your Amazon S3 bucket.
Click Add trigger and under Under Trigger configuration configure:
Select a source: S3
Bucket: Select your images- bucket (e.g. images-123456789)
Event type: All object create events
For Recursive invocation, Select I acknowledge that …
Scroll to the bottom of the screen, then click Add
You will now configure the Lambda function.
Click Code as shown below:
Configure the following settings (and ignore any settings that aren’t listed):
Click Upload from menu and select Amazon S3 location
Amazon S3 link URL: Copy and paste this URL into the field:
https://us-west-2-aws-training.s3.us-west-2.amazonaws.com/courses/spl-88/v2.3.23.prod-668e8b10/scripts/CreateThumbnail.zip
The Code from the ZIP is performing the following steps:
Receives an Event, which contains the name of the incoming object (Bucket, Key)
Downloads the image to local storage
Resizes the image using the Pillow library
Uploads the resized image to the -resized bucket
In the Runtime settings section, click Edit
Handler enter:
CreateThumbnail.handler
Click Save
Click Configuration as shown below:
- Click General configuration, click Edit
- Description enter:
Create a thumbnail-sized image
You will leave the other settings as default, but here is a brief explanation of these settings:
Memory defines the resources that will be allocated to your function. Increasing memory also increases CPU allocated to the function.
Timeout sets the maximum duration for function execution.
Click Save
Your Lambda function has now been configured.
Task 3: Test Your Function
In this task, you will test your Lambda function. This is done by simulating an event with the same information normally sent from Amazon S3 when a new object is uploaded.
Click Test as shown below:
In the Test event section , click Create new event and then configure:
Event name: Upload
Template - optional: s3-put
A sample template will be displayed that shows the event data sent to a Lambda function when it is triggered by an upload into Amazon S3. You will need to edit the bucket name so that it uses the bucket you created earlier.
Replace example-bucket with the name of your images bucket (e.g. images-123456789) that you copied to your text editor.
Be sure to replace example-bucket in both locations.
Replace test/key with the name of the picture that you uploaded. This should be HappyFace.jpg
Click Test
AWS Lambda will now trigger your function, using HappyFace.jpg as the input image.
Towards the top of the page you should see the message: Execution result: succeeded
Result returned by your function will show as null.
If your test did not succeed, the error message will explain the cause of failure.
For example, a Forbidden message means that the image was not found possibly due to an incorrect bucket name. Review the previous steps to confirm that you have configured the function correctly.
Click Details to expand it (towards the top of the screen).
You will be shown information including:
Execution duration
Resources configured
Maximum memory used
Log output
You can now view the resized image that was stored in Amazon S3 in the -resized bucket that we created earlier.
The image should now be a smaller thumbnail of the original image.
You are welcome to upload your own images to the images- bucket and then check for thumbnails in the -resized bucket.
Task 4: Monitoring and Logging
You can monitor AWS Lambda functions to identify problems and view log files to assist in debugging.
Click your Create-Thumbnail function.
Click the Monitor tab as shown below:
The console displays graphs showing:
Invocations: The number of times that the function was invoked.
Duration: The average, minimum, and maximum execution times.
Error count and success rate (%): The number of errors and the percentage of executions that completed without error.
Throttles: When too many functions are invoked simultaneously, they will be throttled. The default is 1000 concurrent executions.
Async delivery failures: The number of errors that occurred when Lambda attempted to write to a destination or dead-letter queue.
Iterator Age: Measures the age of the last record processed from streaming triggers (Amazon Kinesis and Amazon DynamoDB Streams).
Concurrent executions: The number of function instances that are processing events.
Log messages from Lambda functions are retained in Amazon CloudWatch Logs.
Click View CloudWatch logs
Click the Log Stream that appears.
Expand each message to view the log message details.
The Event Data includes the Request Id, the duration (in milliseconds), the billed duration (rounded up to the nearest 100 ms, the Memory Size of the function and the Maximum Memory that the function used. In addition, any logging messages or print statements from the functions are displayed in the logs. This assists in debugging Lambda functions.
Conclusion
Congratulations! You have successfully gained a fundamental understanding of AWS Lambda after having:
Created an AWS Lambda function
Configured an Amazon S3 bucket as a Lambda Event Source
Triggered a Lambda function by uploading an object to Amazon S3
Monitored AWS Lambda S3 functions through Amazon CloudWatch Logs
Clean Up Resources
In case you tried this on your own AWS Account, don't forget to delete the resources associated with this hands-on to avoid unnecessary costs.
Further Reading Resources
Subscribe to my newsletter
Read articles from Kevin Tuei directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Kevin Tuei
Kevin Tuei
ALX Gold Fellow, Cloud consultant and a Certified educator with 7 years of experience. With experience as a Cloud Consultant, I assist in migrating on-premise workloads to the cloud. I am passionate about Data, AI, and Blockchain, and expertise in the Cloud Adoption Framework and AWS Well-Architected Framework. I specialize in training on industry certifications from major technology providers and excels in co-creation, data analytics, user experience, and problem-solving. I am on a career mission to grow into Technical Program/Project Management and use these skills to lead 500 successful projects targeting the SDGs on Education and Gender Equality.