aws-lambdaaws-samamazon-cloudwatch-events

How to define multiple triggers for lambda function in SAM template?


I created a lambda function from a SAM template, and defined multiple triggers, but only one of these triggers is being created in cloudformation. This is my sam template:

Myfunc:
    Type: AWS::Serverless::Function
    Properties:
        FunctionName: name
        CodeUri: /
        Handler: app.lambdaHandler
        Runtime: nodejs12.x
        Role: myrole            
        Events:
            Trigger:
                Type: CloudWatchEvent
                Properties:
                    EventBusName: mybus
                    Pattern:
                        source: 
                            - a
                        detail-type:
                            - b
            Trigger:
                Type: CloudWatchEvent
                Properties:
                    EventBusName: mybus
                    Pattern:
                        source:
                            - c

This template deploys correctly but creates only one rule, and in the aws console, on the sam template it shows:

Events:
    Trigger:
      Type: CloudWatchEvent
      Properties:
        EventBusName: mybus
        Pattern:
          source:
          - c

Any idea how to define multiple triggers for a lambda in the sam template? Is it impossible?


Solution

  • The Events fields is a dictionary, so you have to give different names to your triggers as in:

    Events:
        Trigger:
            Type: CloudWatchEvent
            Properties:
                EventBusName: mybus
                Pattern:
                    source: 
                        - a
                    detail-type:
                        - b
        TriggerForSourceC:
            Type: CloudWatchEvent
            Properties:
                EventBusName: mybus
                Pattern:
                    source:
                        - c