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.