skip to Main Content

I’m trying to trigger a Lambda when a new file is uploaded to an S3 bucket. We’re configuring the bucket in CloudFormation using the NotificationConfiguration > LambdaConfigurations > ObjectCreated > s3:ObjectCreated:* with a filter on the suffix. We want the lambda to be triggered whenever a png file is added to the bucket. We want this to happen for any casing of filename suffix e.g. .png or .PNG. Here is the CF

"NotificationConfiguration": {
      "LambdaConfigurations": [
        {
          "Event": "s3:ObjectCreated:*",
          "Filter": {
            "S3Key": {
              "Rules": [
                {
                  "Name": "suffix",
                  "Value": "PNG"
                }
              ]
            }
          }

The above ONLY works and triggers the Lambda for files with an uppercase suffix of "PNG", the lambda is not triggered for files with a suffix of "png".

How is it possible to make a suffix filter or filters or alternative approach that will trigger the Lambda regardless of suffix casing?

There’s relevant info here but it didn’t seem to solve this particular issue and it seems like a problem that would be encountered fairly often – https://docs.aws.amazon.com/AmazonS3/latest/userguide/notification-how-to-filtering.html

Thanks,

Sam

2

Answers


  1. Amazon S3 file names (and the rules on the event notifications) are always case sensitive.

    If you can’t change your implementation, you need an intermediate Lambda Function that process every S3 Event Notification, filter it manually in a case-insensitive way, and then call the right function in the chain.

    Login or Signup to reply.
  2. I was able to create an S3 bucket NotificationConfiguration that triggers an existing Lambda function when objects with suffix png or with suffix PNG are dropped into the S3 bucket.

    Here’s the YAML template (you can convert to JSON easily):

    AWSTemplateFormatVersion: "2010-09-09"
    Description: Stack Overflow 75519656
    
    Parameters:
      BucketName:
        Type: String
        Default: mybucket
      FunctionName:
        Type: String
        Default: print-event
      FunctionArn:
        Type: String
        Default: arn:aws:lambda:us-east-1:111122223333:function:print-event
    
    Resources:
      LambdaInvokePermission:
        Type: AWS::Lambda::Permission
        Properties:
          FunctionName: !Ref FunctionName
          Action: lambda:InvokeFunction
          Principal: s3.amazonaws.com
          SourceAccount:
            Ref: AWS::AccountId
          SourceArn:
            Fn::Sub: arn:aws:s3:::${BucketName}
      Bucket:
        Type: AWS::S3::Bucket
        DependsOn: LambdaInvokePermission
        Properties:
          BucketName: !Ref BucketName
          NotificationConfiguration:
            LambdaConfigurations:
              - Event: s3:ObjectCreated:*
                Filter:
                  S3Key:
                    Rules:
                      - Name: suffix
                        Value: png
                Function: !Ref FunctionArn
              - Event: s3:ObjectCreated:*
                Filter:
                  S3Key:
                    Rules:
                      - Name: suffix
                        Value: PNG
                Function: !Ref FunctionArn
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search