AWS::S3::Bucket LambdaConfiguration in multiple AWS Lambdas

Viewed 95

I have 4 AWS Lambdas that should read S3 bucket when some file is created (S3 Event), but in cloudformation I just can use 1 lambda ARN, see inside AWS::S3::Bucket LambdaConfiguration:

enter image description here

How can I trigger more than 1 Lambda in Bucket Lambda Configuration ?

3 Answers

S3 does not offer this kind of fan-out out-of-the-box but only through e.g. SNS.
You need to push the notification into a SNS topic instead of a lambda and then either

  • subscribe four lambdas to that topic or
  • subscribe four queues to the topic and let each lambda "subscribe" to one queue

AWS recently announced S3 Event Notifications with Amazon EventBridge. Consequently, you can enable EventBridge notification on your bucket and then have one (or more) Lambda function(s) triggered by those events.

Example implementation using AWS SAM:

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: 'S3 EventBridge Example'

Parameters:
  BucketName:
    Type: String
    Description: 'Name of the bucket to be created'

Resources:

  S3Bucket:
    Type: AWS::S3::Bucket
    Properties:
      BucketName: !Ref BucketName
      NotificationConfiguration:
        EventBridgeConfiguration:
          EventBridgeEnabled: true

  S3EventProcessor:
    Type: AWS::Serverless::Function
    Properties:
      FunctionName: S3EventListener
      Architectures:
        - arm64
      Runtime: nodejs14.x
      Handler: index.handler
      InlineCode: |
        exports.handler = (event, context) => {
          console.log('event:', JSON.stringify(event));
        }
      Events:
        S3EventBridgeRule:
          Type: EventBridgeRule
          Properties:
            Pattern:
              source:
                - aws.s3
              detail:
                bucket:
                  name:
                    - !Ref BucketName
Related