You can return to the original look by selecting English in the language selector
above.

Tutorial: Using AWS Lambda with Amazon S3

Suppose you want to create a thumbnail for each image file that is uploaded to a bucket.
You can create a Lambda
function (CreateThumbnail) that Amazon S3 can invoke when objects are created. Then, the Lambda function can
read the image object from the source bucket and create a thumbnail image target bucket.

Upon completing this tutorial, you will have the following Amazon S3, Lambda, and
IAM resources in your account:

An execution role that grants permissions that your Lambda function needs through
the permissions policy
associated with this role.

Amazon S3 Resources

A source bucket with a notification configuration that invokes the Lambda function.

A target bucket where the function saves resized images.

Prerequisites

This tutorial assumes that you have some knowledge of basic Lambda operations and
the Lambda console. If you
haven't already, follow the instructions in Getting Started with AWS Lambda to create your first Lambda function.

To follow the procedures in this guide, you will need a command line terminal or shell
to run commands. Commands are shown in
listings preceded by a
prompt symbol ($) and the name of the current directory, when appropriate:

~/lambda-project$ this is a command
this is output

For long commands, an escape character (\) is used to split a command over multiple lines.

Create Buckets and Upload a Sample Object

Create two buckets. The target bucket name must be source followed by
resized, where source is the name of the bucket you want
to use for the source. For example, mybucket and mybucketresized.

In the source bucket, upload a .jpg object, HappyFace.jpg.

When you invoke the Lambda function manually before you connect to Amazon S3, you
pass sample event data to the
function that specifies the source bucket and HappyFace.jpg as the newly created object
so you need to create this sample object first.

Create the Function

The following example code receives an Amazon S3 event input and processes the message
that it contains. It resizes
an image in the source bucket and saves the output to the target bucket.

The function knows the source bucket name and the key name of the object from the
event data it receives
as parameters. If the object is a .jpg, the code creates a thumbnail and saves it
to the target bucket.

The code assumes that the destination bucket exists and its name is a concatenation
of the source bucket
name followed by the string resized. For example, if the source bucket identified in the event
data is examplebucket, the code assumes you have an examplebucketresized destination
bucket.

For the thumbnail it creates, the code derives its key name as the concatenation of
the string
resized- followed by the source object key name. For example, if the source object key is
sample.jpg, the code creates a thumbnail object that has the key
resized-sample.jpg.

The preceding command specifies a 10-second timeout value as the function configuration.
Depending on the size
of objects you upload, you might need to increase the timeout value using the following
AWS CLI command.

Run the following Lambda CLI invoke command to invoke the function. Note that the command
requests asynchronous execution. You can optionally invoke it synchronously by specifying
RequestResponse as the invocation-type parameter value.

Configure Amazon S3 to Publish Events

In this step, you add the remaining configuration so that Amazon S3 can publish object-created
events to AWS Lambda
and invoke your Lambda function. You do the following in this step:

Add permissions to the Lambda function access policy to allow Amazon S3 to invoke
the function.

Add notification configuration to your source bucket. In the notification configuration,
you provide the
following:

Event type for which you want Amazon S3 to publish events. For this tutorial, you
specify the
s3:ObjectCreated:* event type so that Amazon S3 publishes events when objects are
created.

Lambda function to invoke.

To add permissions to the function policy

Run the following Lambda CLI add-permission command to grant Amazon S3 service principal
(s3.amazonaws.com) permissions to perform the lambda:InvokeFunction action. Note
that permission is granted to Amazon S3 to invoke the function only if the following
conditions are met:

An object-created event is detected on a specific bucket.

The bucket is owned by your account. If you delete a bucket, it is possible for another
account to
create a bucket with the same ARN.