I am creating a lambda that should be called when objects are created in a certain bucket. I only want it to be called for certain key prefixes. I have seen examples of setting this up using a NotificationConfiguration on the bucket when the bucket is being defined:
NotificationConfiguration:
LambdaConfigurations:
- Function: !Ref SomeLambdaArn
Event: "s3:ObjectCreated:*"
Filter:
S3Key:
Rules:
- Name: prefix
Value: zip
However I would rather not modify the bucket's CloudFormation definition. With SAM, lambda event triggers can be defined on the lambda:
Events:
BucketEvent1:
Type: S3
Properties:
Bucket: Ref: Bucket1
Events:
- 's3:ObjectCreated:*'
But is there a way to restrict it to trigger only for certain prefixes? Alternatively, is there a way to add a NotificationConfiguration to an existing bucket?
If I understand you correctly you want to define the prefix in the lambda not the bucket.
This is how I did it, works like a charm as far as I can tell
Resources:
ConvertToParquet:
Type: AWS::Serverless::Function
Properties:
CodeUri: handler/convert_to_parquet
MemorySize: 3008
Timeout: 90
Policies:
- AmazonS3FullAccess
Events:
SummaryCSVCreated:
Type: S3
Properties:
Bucket: !Ref UploadBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: '.txt'
UploadBucket:
Type: AWS::S3::Bucket
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