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

1

5

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?

Macule answered 24/2, 2020 at 23:11 Comment(0)
M
7

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
Macule answered 25/2, 2020 at 16:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.