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

3.8k views Asked by At

I'd like to trigger a single lambda based on different rules. 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.
3

There are 3 answers

0
EnterPassword On

Does not look like EventBridge supports multiple rules on a lambda. Unless someone knows different, but so far I have not found anything that indicates that's it's possible.

0
shontauro On

This creates the rule in the event bus on EventBridge but it doesn't create the association between cloud-watch and lambda from the lambda perspective, so when you put an event in the event bus it never triggers the lambda. When you create the association between eventbridge and lambda using the default way:

 events:
   - eventBridge:
       eventBus: ${env:ORDERS_EVENT_BUS_ARN}
       pattern:
         source:
           - domain.orders

It creates the rule in EventBridge and the Cloudwatch event association in the lambda, you can check it in the lambda configuration TAB.

0
Kshitiz Agrawal On

Yes, It is possible with a little workaround.

Create rules as resources. 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