AWS - Lambda
AWS - Lambda
application.
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.
Upon completing this tutorial, you will have the following resources in your account:
The steps in this lab will show you how to create the Amazon S3 buckets and the Lambda
function. You will then test the service by uploading images for resizing.
Task 1: Create the Amazon S3 Buckets
In this task, you will create two Amazon S3 buckets -- one for input and one for output.
Amazon S3 buckets require unique names, so you will add a random number to the
bucket name such as images-123456789.
● Bucket name:
If you receive an error stating The requested bucket name is not available, then click
the first Edit link, change the bucket name with a different random number and try
again until it works.
● Scroll to the bottom of the screen to click Create bucket leaving the rest of the
options as default.
7. You will now create another bucket for output. Click Create bucket with similar
steps as the previous bucket, now configure:
● Bucket name: Paste the name of your images bucket
● At the end of the bucket name, append
● Click Create bucket
images-123456789
images-123456789-resized
content_copy
8. You will now upload a picture for testing purposes.
● Right-click this link and download the picture to your computer: HappyFace.jpg
● Name the file HappyFace.jpg.
Firefox users: Make sure the saved filename is HappyFace.jpg (not .jpeg).
Later in this lab you will invoke the Lambda function manually by passing sample event
data to the function. The sample data will refer to this HappyFace.jpg image.
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.
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.
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.
23. Download the CreateThumbnail.zip function code file from the following URL:
https://s3-us-west-2.amazonaws.com/us-west-2-aws-training/awsu-spl/spl-88/2.3.11.prod/scripts/
CreateThumbnail.zip
24. Scroll down to the Function code section and configure the following settings
(and ignore any settings that aren't listed):
● Click Actions menu and select Upload a .zip file
● Select the file, Click Upload
● Click Save
Do not copy this code -- it is just showing you what is in the Zip file.
import boto3
import os
import sys
import uuid
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((128, 128))
image.save(resized_path)
Make sure you set the Handler field to the above value, otherwise the Lambda function
will not be found.
26. In the Basic settings section towards the bottom of the page, click Edit
● Description enter:
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
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.
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.
28. 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.
29. Replace test/key with the name of the picture that you uploaded. This should be
30. Click Create
31. 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
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.
32. Click Details to expand it (towards the top of the screen).
● Execution duration
● Resources configured
● Maximum memory used
● Log output
You can now view the resized image that was stored in Amazon S3.
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.
Log messages from Lambda functions are retained in Amazon CloudWatch Logs.
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.