1. 14
    Create an S3 event notification to trigger a lambda function on file upload
    2m 39s

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

Share this video with your friends

Send Tweet

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.

Roland Pangu
Roland Pangu
~ a year 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
~ a year ago

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

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