Is it possible to set multiple eventbridge rules on a lambda?

I’d like to trigger a single lambda based on different rules (the source needs to be different). Can I set multiple event rules on a lambda? Something like the following:

mylambda:
    handler: lambdas/mylambda.handler
    description: Reusable lambda 
    iamRoleStatements:
      - Effect: Allow
        Action:
          - events:PutEvents
        Resource:
          - ${ssm:/foo/bar/myEventBus}
    events:
      - eventBridge:
          eventBus: ${ssm:/foo/bar/myEventBusArn}
          pattern:
            source:
              - foo
            detail:
              myKey:
                - bar
      - eventBridge:
          eventBus: ${ssm:/foo/bar/myEventBusArn}
          pattern:
            source:
              - baz
            detail:
              myKey:
                - oof

I found this https://www.serverless.com/framework/docs/providers/aws/events/event-bridge/ which seems to indicate it’s possible, but I can’t seem to get it to work.

I get the following error:

Failed to create resource. The statement id (mylambda-mylambda-rule) provided already exists. Please provide a new statement id, or remove the existing statement.

Yes, It is possible with a little workaround.

  1. Create rules as resources.
  2. Add your lambda as target to the created rules.

Note: Make sure that you have latest version of serverless framework

functions:

  mylambda:

    handler: lambdas/mylambda.handler

    description: Reusable lambda 

    iamRoleStatements:

      - Effect: Allow

        Action:

          - events:PutEvents

        Resource:

          - ${ssm:/foo/bar/myEventBus}

resources:

  Resources:

    EventbridgeRule1:

      Type: AWS::Events::Rule

      Properties:

        EventBusName: ${ssm:/foo/bar/myEventBusArn}

        EventPattern:

          source:

            - foo

          detail:

            myKey:

              - bar

        Name: rule-1

        Targets:

          - Arn: !GetAtt 

              - MyLambdaLambdaFunction

              - Arn

            Id: MyLambda

    EventbridgeRule2:

      Type: AWS::Events::Rule

      Properties:

        EventBusName: ${ssm:/foo/bar/myEventBusArn}

        EventPattern:

          source:

              - baz

          detail:

            myKey:

              - oof

        Name: rule-2

        Targets:

          - Arn: !GetAtt 

              - MyLambdaLambdaFunction

              - Arn

            Id: MyLambda

Something like this worked for me:

mylambda:
    handler: lambdas/mylambda.handler
    description: Reusable lambda 
    iamRoleStatements:
      - Effect: Allow
        Action:
          - events:PutEvents
        Resource:
          - ${ssm:/foo/bar/myEventBus}
    events:
      - eventBridge:
          eventBus: ${ssm:/foo/bar/myEventBusArn}
          pattern:
            source:
              - foo
              - baz
            detail-type:
              - bar
              - oof