如何在SAM模板中为lambda函数定义多个触发器?

时间:2020-02-24 23:11:05

标签: aws-lambda aws-sam amazon-cloudwatch-events

我从SAM模板创建了一个lambda函数,并定义了多个触发器,但是这些触发器中只有一个是在cloudformation中创建的。这是我的sam模板:

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

此模板正确部署,但仅创建一个规则,并且在aws控制台中,在sam模板上显示:

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

有什么想法如何在sam模板中为lambda定义多个触发器?不可能吗?

1 个答案:

答案 0 :(得分:3)

“事件”字段是一个词典,因此您必须为触发器指定不同的名称,如:

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