Lambda Function with S3 Event Input

Previously, we created a synchronous Lambda function that takes the string as input and returns the string as output. Now, we are going to create an asynchronous Lambda function that can be invoked through S3 events, and test the function with a dummy event that can upload a file to S3 and see if the function is triggered by the create-object event.

Open the project that we have created. Let's add a new Lambda function handler to the project. Click on the little orange box and select New AWS Lambda Function. We will create a new function handler name, S3FunctionHandler; use the default input type that is already selected, S3 Event; and as leave the output type as object:

Click on Finish. This will create some boilerplate code with the Lambda function handler that takes an S3 event as input:

Let's deploy the new function. Right click on demo and go to Amazon Web Services | Upload function to AWS Lambda.

You will notice that the region is still EU Central in Frankfurt, but, instead, we create a new Lambda function, MyS3JavaFunction. Click on Next. There will be two Lambda function handlers. Since we don't want to deploy the same Lambda function as before, let's deploy the new function handler that we've just created. Select S3FunctionHandler and select an IAM Role:

Click on Finish to upload the code.

Once the code has been uploaded, we switch over to the AWS Management Console to test our Lambda function with a dummy S3 event. You will notice that, on the Management Console, we have selected MyS3JavaFunction. We need to test it, so to do this let's configure a test event. Select the S3 Put event, which you find by clicking on the Actions tab. This simulates somebody uploading a new object to an S3 bucket. Click on Save and Test. The Lambda function returns null and the log output here is the S3 event, as follows:

When you go back to the Eclipse IDE, you will see that the input event is not very verbose. We need to give out some more information about our S3 event. Make the console output a little bit more verbose by replacing the code with the following:

We need to iterate over the records, which are of the type S3EventNotificationRecord. Then we'll output some information, such as the event name, the event source, and the object that has triggered the event. Now let's update our code by choosing the same function as before, by clicking Next and Finish to upload the code. Once the function has been updated, we will head back to our AWS Management Console.

Go to the AWS Lambda dashboard and click on the Test button again.

As you can see, it uses the same dummy event from before and the log statements can be seen; for example, the event name, which is the ObjectCreated event of type put. The Event Source is S3 and the object key is HappyFace.jpg.

So now we know that the Lambda function can process S3 events. How about testing it with a real S3 event?

Click on the Triggers tab and add a new trigger to our Lambda function. You can select from a number of triggers, but we want to choose the S3 trigger. Let's use the S3 bucket that has been created previously. If you don't have an S3 bucket yet, create a bucket and then we'll test our Lambda function with events in the bucket. We can also specify which type of events we want to trigger our Lambda function. We will go for Object Created events and click on Submit. This will create a trigger for the Lambda function, as shown here:

Let's go to the S3 dashboard, upload a file into our S3 bucket, and then check if we can see the metadata of the file that we have uploaded in the log statements of our Lambda function.

Go to the S3 dashboard and upload a file into the bucket that has the created trigger. Once the file has been uploaded, take a look at the Lambda log. Go back to the dashboard of the Lambda function, and click on Monitoring and View logs in CloudWatch. As you can see in the following logs, we have our dummy event, the HappyFace.jpg file, but we also have the Smiling-Cat.jpg that we have just uploaded:

As you can imagine, you can do a lot of things with the Lambda function using triggers. For example, we could process the JPEG file, transform it into a thumbnail, store that thumbnail into another S3 bucket, and then load it onto your Web page to load faster. As you can imagine, you could do a lot of things with this Lambda function.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.23.130.191