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
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.
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'
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With