1. 14
    Create an S3 event notification to trigger a lambda function on file upload
    2m 39s
⚠️ This lesson is retired and might contain outdated information.

Create an S3 event notification to trigger a lambda function on file upload

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

Social Share Links

Send Tweet
Published 4 years ago
Updated 5 months ago

A huge part of building serverless applications with AWS is being able to connect certain cloud resources and have them react to certain events.

Currently our HelloLambda function can be triggered by a GET request event sent to the API Gateway, but that's not the only use case when a lambda function can be triggered.

Suppose we'd like to trigger the lambda function whenever a file is uploaded to an S3 bucket, for instance, to generate a thumbnail. Once a thumbnail is generated, lambda function can call another function etc., this behaviour is up to us to define.

In order to call a lambda function when a file is uploaded to an S3 bucket, we need to use an s3Notifications construct - and that's exactly what we're going to do in this quick lesson.

Instructor: [0:00] A huge part of building serverless applications with AWS is being able to connect cloud resources and have them react to certain events.

[0:07] Currently, our Hello Lambda function can be triggered whenever there's a request to this Lambda REST API. This is not the only case when a Lambda function can get triggered. Suppose we would like to be able to trigger a Lambda function whenever a file is uploaded to an S3 bucket. For instance, to generate a thumbnail.

[0:21] In order to do that, we need to use the S3 notifications construct. To do that, open up the terminal and run npm install --save @aws-cdk/aws-s3-notifications and hit Enter. Once this is done, go back to your code and import * as s3-notifications from 's3-notifications'.

[0:38] Next up, we are going to attach a new notification for this logo-bucket. I'm going to do logo-bucket @event-notification. This is going to add a bucket notification event destination. It can either be a Lambda function or for instance, it can also be an SNS topic or an SQS Queue. In this lesson we are going to use the Lambda function.

[0:55] The first argument is the event that is going to trigger the notification. Our event is going to be s3:ObjectCreated, so whenever a file gets uploaded to this bucket. The second part is the destination. I'm going to do new s3-notifications.LambdaDestination("Hello Lambda").

[1:11] To recap, this is going to attach an event notification to our logo bucket. Whenever an object gets created inside of this bucket, so whenever a file gets uploaded, is going to send a notification with a destination of a Lambda function. The Lambda function that is going to get triggered is our Hello Lambda function over here. We've managed to do it with only four lines of code.

[1:29] Let's go ahead and deploy it, so run cdk deploy in our terminal. Right now, it's going to ask us whether we would like to allow this S3 bucket to be able to invoke this Lambda function. Yes, we would like to do that. I'm going to hit Yes and hit Enter.

[1:41] Now it's down, so let's go ahead and test it. Go to our bucket, hit Upload and I'm going to add a file which I have on my desktop. This is the AWS logo. I'm going to hit Upload. Now our file has been uploaded, so let's go ahead and check whether our Lambda function has been triggered.

[1:54] Let's go to the Lambda Console. Click on Services, search for Lambda and click over here. Here in the Lambda Console, we can see two functions that were updated recently. The first one is our Hello Lambda function that we wanted to trigger whenever a file was uploaded to the S3 bucket.

[2:07] The second one was created automatically for us. This function is taking care of sending the S3 bucket notifications. Again, this is yet another thing that we didn't have to configure that was created for us by CDK.

[2:18] Next, go to Hello Lambda function, click on Monitoring and click on View logs in CloudWatch. Here we can see the logs for this Lambda function. Let's click on the newest one.

[2:26] If I expand the event that was sent to this function, we are going to be able to see that this function has been triggered because we have uploaded the AWS logo file, we can see the size of this file over here, and also a bunch of information about the bucket that we have uploaded the file to.

Roland Pangu
Roland Pangu
~ 8 months ago

Starting a few months ago, all new S3 buckets will have BlockAll enabled by default and all ACLs disabled.

So the code that made it work for me was:

    const logoBucket = new Bucket(this, 'LogoBucket', {
      blockPublicAccess: BlockPublicAccess.BLOCK_ACLS,
      accessControl: BucketAccessControl.BUCKET_OWNER_FULL_CONTROL,
    });

Roland Pangu
Roland Pangu
~ 8 months ago

Also, the way things are done to handle notifications is through event sources.

    helloLambda.addEventSource(
      new S3EventSource(logoBucket, { events: [EventType.OBJECT_CREATED] })
    );

Markdown supported.
Become a member to join the discussionEnroll Today