Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't add s3 notification for lambda using boto3

I want to use boto3 to configure an s3 bucket to invoke an AWS lambda every time an object is created in that bucket. Here is my code:

s3 = ..boto3 resource
bucket_notification = s3.BucketNotification(bucket_name)
lambda_arn = .. arn for lambda

response = bucket_notification.put(
   NotificationConfiguration={'LambdaFunctionConfigurations': [
      {
                'LambdaFunctionArn': lambda_arn,
                'Events': [
                    's3:ObjectCreated:*'
                ],

      },
]})

I get the error:

botocore.exceptions.ClientError: An error occurred (InvalidArgument) when calling the PutBucketNotificationConfiguration operation: Unable to validate the following destination configurations

like image 272
RAbraham Avatar asked May 01 '16 23:05

RAbraham


People also ask

Can S3 invoke 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.


1 Answers

One has to add permissions on the lambda end to allow S3 to invoke the lambda function. Beware, if you manually create the event source mapping using the AWS Lambda GUI and then delete the event source mapping, the permission still exists! So you won't get the above error.

However, if you start from scratch and then try to add the notification, the above error will occur.

Permissions are added by:

 client = ...boto3 lambda client
 response = client.add_permission(
     FunctionName=lambda_name,
     StatementId='1',
     Action='lambda:InvokeFunction',
     Principal='s3.amazonaws.com',
     SourceArn=s3_arn,
     SourceAccount='66666666666'
 )
like image 144
RAbraham Avatar answered Sep 24 '22 06:09

RAbraham