【问题标题】:Serverless - Schedule event not creating CloudWatch Events无服务器 - 计划事件未创建 CloudWatch 事件
【发布时间】:2020-01-07 01:20:03
【问题描述】:

Serverless 不会创建 CloudWatch Events 作为 lambda 的触发器。没有警告或错误。

functions:
  aggregate:
    handler: statistics.handler
    events:
      - schedule:
          rate: rate(10 minutes)

【问题讨论】:

    标签: serverless amazon-cloudwatch-events


    【解决方案1】:

    Serverless' 示例没有展示缩进的关键性质。 https://serverless.com/framework/docs/providers/aws/events/schedule/#schedule

    functions:
      aggregate:
        handler: statistics.handler
        events:
        # "- schedule:" has to start at the same indentation as the "events:" above it.
        - schedule:
            # The CloudWatch Events Rules have to be exactly 4 spaces indented below the "- schedule:"
            rate: rate(10 minutes)
            # ... other fields
    

    关键:

    • - schedule: 与上方的events: 对齐。
    • 对齐下一行​​,例如rate: rate(6 minutes)- schedule: 缩进 4 个空格

    示例代码:

    service: my-service
    provider:
      name: aws
      region: us-west-2
      runtime: nodejs10.x
    functions:
      hello:
        handler: handler.hello
        events:
        - schedule:
            rate: cron(*/5 * * * ? *)
            enabled: true
    

    module.exports.hello = (event, context, callback) => {
      console.log("Hello, world!");
      callback(null);
    };
    

    简单缩进 - 像我预期的那样安排 2 个空格不会在 AWS 中创建 cloudwatch 事件。 2 个空格的单次更改决定了是否创建了 cloudwatch 事件规则。

    注意:两个缩进之间不会引发错误,但它会创建 6 对 8 个 AWS 资源(缺少 2 个不创建 cloudwatch 事件规则)。

    【讨论】:

    • 是否有任何文档支持这一点?我们使用您链接的文档显示的相同间距进行部署,我们没有问题。似乎重要的部分是计划行和属性行(速率、启用等)之间有 4 个空格。但是,文档确实显示了 4 个空格。它可能看起来不像,但如果你将它复制到编辑器中,你可以数出4个空格。因此,您的缩进和文档对事件和时间表之间的缩进的例外确实没有区别(据我所知,这并不重要)。
    • 我不知道。这个博客medium.com/blogfoster-engineering/… 具有对我有用的相同缩进,如果我更改该缩进,它将不会创建 cloudwatch 事件。不确定原因是什么,但对我来说是一致的(在示例和我自己的自定义无服务器调度触发 lambda 中)。注意:它不会给你错误,只是创建 6 个没有适当缩进的资源或 8 个 - scheduleevents: 对齐。
    猜你喜欢
    • 2018-11-23
    • 2021-12-05
    • 2020-04-19
    • 2019-01-28
    • 2016-07-11
    • 2023-03-21
    • 1970-01-01
    • 2020-10-21
    • 2019-11-18
    相关资源
    最近更新 更多