Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boto3 - Create S3 'object created' notification to trigger a lambda function

How do I use boto3 to simulate the Add Event Source action on the AWS GUI Console in the Event Sources tab.

I want to programatically create a trigger such that if an object is created in MyBucket, it will call MyLambda function(qualified with an alias).

The relevant api call that I see in the Boto3 documentation is create_event_source_mapping but it states explicitly that it is only for AWS Pull Model while I think that S3 belongs to the Push Model. Anyways, I tried using it but it didn't work.

Scenarios:

  • Passing a prefix filter would be nice too.
like image 737
RAbraham Avatar asked Apr 28 '16 20:04

RAbraham


People also ask

Can S3 trigger a Lambda?

Amazon S3 can send an event to a Lambda function when an object is created or deleted. You configure notification settings on a bucket, and grant Amazon S3 permission to invoke a function on the function's resource-based permissions policy.

How can you be notified when there's an object uploaded to your S3 bucket?

In the Buckets list, choose the name of the bucket that you want to enable events for. Choose Properties. Navigate to the Event Notifications section and choose Create event notification.


1 Answers

I was looking at the wrong side. This is configured on S3

    s3 = boto3.resource('s3')
    bucket_name = 'mybucket'
    bucket_notification = s3.BucketNotification(bucket_name)
    response = bucket_notification.put(
        NotificationConfiguration={'LambdaFunctionConfigurations': [
            {
                'LambdaFunctionArn': 'arn:aws:lambda:us-east-1:033333333:function:mylambda:staging',
                'Events': [
                    's3:ObjectCreated:*'
                ],

            },
        ]})
like image 123
RAbraham Avatar answered Oct 15 '22 00:10

RAbraham