Search code examples
serverlessserverless-frameworkaws-serverless

Serverless Framework | How to use wildcard (*) in prefix path


I'm using Serverless Framework to create an S3 Lambda trigger:

functions:
  profilePictureInput:
    handler: triggers/handler.profilePictureInput
    memorySize: 10240
    timeout: 25
    events:
      - s3:
          bucket: ${self:custom.mediaInputBucketName}
          existing: true
          event: s3:ObjectCreated:*
          rules:
            - prefix: private/*/profile-pictures

Question: Is it possible to use a wildcard in the prefix path?

I want the lambda to be triggered if object key is private/foo/profile-pictures or private/bar/profile-pictures.

The above example does not seem to work (lambda not triggered)


Solution

  • No, you can't.

    Because the wildcard asterisk character (*) is a valid character that can be used in object key names, Amazon S3 literally interprets the asterisk as a prefix or suffix filter. You can't use the wildcard character to represent multiple characters for the prefix or suffix object key name filter.

    You have to register all prefix value strings to the S3 event. For you case, it will look like this:

    functions:
      profilePictureInput:
        handler: triggers/handler.profilePictureInput
        memorySize: 10240
        timeout: 25
        events:
          - s3:
              bucket: ${self:custom.mediaInputBucketName}
              existing: true
              event: s3:ObjectCreated:*
              rules:
                - prefix: private/foo/profile-pictures
                - prefix: private/bar/profile-pictures