serverless trigger for existing S3 fails with overlapping suffixes

Viewed 1564

Following S3 Simple event definition I have created serverless deployment

functions:
  convert:
    handler: handler.convert
    events:
      - s3:
          bucket: dev.domain.com
          event: s3:ObjectCreated:*
          rules:
            - prefix: assets/images/
            - suffix: .jpg
          existing: true
      - s3:
          bucket: dev.domain.com
          event: s3:ObjectCreated:*
          rules:
            - prefix: assets/images/
            - suffix: .png
          existing: true

But at the time of deployment I am getting following error

An error occurred: ConvertCustomS31 - Failed to create resource. Configuration is ambiguously defined. Cannot have overlapping suffixes in two rules if the prefixes are overlapping for the same event type. See details in CloudWatch Log: 2020/08/25/[$LATEST]47c58153b46f4aaf9ec08eacabbac2b2.

any idea what I am missing here ?

Also what I supposed to use here event: s3:ObjectCreated:* If I want triggers only for S3 PUT events. I tried event: s3:ObjectCreatedByPut:* which gave me

An error occurred: ConvertCustomS31 - Failed to create resource. The event is not supported for notifications See details in CloudWatch Log: 2020/08/25/[$LATEST]f50070907c60405085c3b03ab0183514.
1 Answers

The problem is that S3 does not allow event notifications to have overlapping prefixes or suffixes. From the S3 documentation about notifications:

Notification configurations that use Filter cannot define filtering rules with overlapping prefixes, overlapping suffixes, or prefix and suffix overlapping.

This means you either need to adjust your prefix in one of the filters (e.g. use assets/images/jpg and assets/images/png as prefixes) or, if you don't want to have different prefixes, then you can just use one notification config for prefix assets/images and filter for the suffix within your Lambda function. This can be done by checking the key of the S3 object data. Of course the latter option has the disadvantage of always calling your Lambda function, regardless of the suffix. This can have negative consequences for your costs.

I advise you to read through the various notification examples in the S3 documentation, maybe you get inspired how you can deal with this limitation in your case.

For the event name, you have to use s3:ObjectCreated:Put if you want to filter for PUT events. You can have a look at the overview section of the S3 notification documentation where it is explained:

New object created events — Amazon S3 supports multiple APIs to create objects. You can request notification when only a specific API is used (for example, s3:ObjectCreated:Put), or you can use a wildcard (for example, s3:ObjectCreated:*) to request notification when an object is created regardless of the API used.

Related