Trigger an AWS Lambda function by uploading a file to an S3 bucket

Tomasz Łakomy
InstructorTomasz Łakomy
Share this video with your friends

Social Share Links

Send Tweet
Published 5 years ago
Updated 4 years ago

AWS Lambda functions can be triggered by many different sources, including HTTP calls and files being uploaded to S3 buckets.

This can be useful, for instance if we'd like to start processing a file using a lambda function whenever it gets uploaded to an S3 bucket.

In this lesson we're going to learn how to create a S3 event trigger for a lambda function, how to create an S3 sample event to test the function and how can we verify that the function was triggered when uploading a file by viewing CloudWatch logs.

Instructor: [0:00] Start by creating a new Lambda function. I'm going to call it myS3Function. We would like this function to be executed whenever someone uploads a file to an S3 bucket. In order to do that, we need to add a trigger. From the list, we're going to select S3, and afterwards, I'm going to select a bucket to be the egghead [inaudible] bucket, which I created a while ago.

[0:20] Basically, this function can be triggered based on certain events. It's either All ObjectCreated events, or only the Put, Post, or Copy events. We can also specify a prefix and a suffix. For instance, if only the JPEG files should trigger this Lambda function, we can specify that over here.

[0:37] Now I'm going to click on the Add button. We can see that the trigger, egghead [inaudible] bucket was successfully added to this function, myS3Function.

[0:45] Let's take a look at our Lambda function. I'm going to click over here, scroll down, and every single time we execute a Lambda function, there's an event passed into this function. I'm going to go ahead and console.log this event, so console.log('event', event), like this.

[1:02] If I save this function, we're going to create a sample event from the S3 bucket. I'm going to go over here, configure test events, and I'm going to select an S3 Put event template. This is what the event looks like whenever you upload a file to an S3 bucket. This sample event allows us to implement our function without having to upload something to S3 bucket in order to test it.

[1:25] I'm going to call my event name to be "My S3 event." Let me go ahead and create that. In order to run our function with the sample event, click on Test. Right now, we can see that the execution result is succeeded. Down here in the logs, we can see the sample event being passed into the function.

[1:43] Basically, over here we can see that this event is an object with our records field, which is an array containing objects describing the files uploaded to this S3 bucket.

[1:52] Let me modify our function a bit. I'm going to paste in a different console.log statement. Basically, the upload result is the first record, because I'm going to upload a single file, and the S3 is an object containing the data about this file.

[2:04] I'm going to save this function, and now I'm going to switch to S3. This is the egghead [inaudible] bucket which I've mentioned earlier. I'm going to go ahead and upload a file to this bucket. Click on that files, I'm going to upload this video, and click on Upload. This is going to take a second. It's done now, and we can see this file being uploaded to the bucket.

[2:24] Let's see if our function has triggered. In order to do that, go back to S3, click on the monitoring tab, and click on View Logs in CloudWatch. We're going to save the newest log stream. Over here, we can see the S3 upload result. We can see that this file was uploaded to a bucket named egghead [inaudible] bucket.

[2:42] This is the file name, and this is the size of this file. We established that this function was triggered by uploading a file to an S3 bucket.

egghead
egghead
~ a minute ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today