【问题标题】:Can I create a CloudWatch scheduled event in CloudFormation template?我可以在 CloudFormation 模板中创建 CloudWatch 计划事件吗?
【发布时间】:2019-01-28 21:32:48
【问题描述】:

我知道我可以通过 AWS 控制台创建计划的 Cloud Watch 事件:

https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/Create-CloudWatch-Events-Scheduled-Rule.html

有没有办法在 Cloud Formation 模板中声明类似事件?

【问题讨论】:

    标签: amazon-web-services amazon-cloudformation amazon-cloudwatch


    【解决方案1】:

    以下是在 cloudwatch 中创建计划事件的示例,它创建一个规则,每 10 分钟调用一次指定的 Lambda 函数。 PermissionForEventsToInvokeLambda 资源授予 EventBridge 调用关联函数的权限。

    "ScheduledRule": {
      "Type": "AWS::Events::Rule",
      "Properties": {
        "Description": "ScheduledRule",
        "ScheduleExpression": "rate(10 minutes)",
        "State": "ENABLED",
        "Targets": [{
          "Arn": { "Fn::GetAtt": ["LambdaFunction", "Arn"] },
          "Id": "TargetFunctionV1"
        }]
      }
    },
    "PermissionForEventsToInvokeLambda": {
      "Type": "AWS::Lambda::Permission",
      "Properties": {
        "FunctionName": { "Ref": "LambdaFunction" },
        "Action": "lambda:InvokeFunction",
        "Principal": "events.amazonaws.com",
        "SourceArn": { "Fn::GetAtt": ["ScheduledRule", "Arn"] }
      }
    }
    

    示例引用自AWS官方文档。

    【讨论】:

      【解决方案2】:

      是的,这是可能的。

      AWS::Events::Rule 资源创建一个规则来匹配传入的 Amazon CloudWatch 事件 (CloudWatch Events) 事件并将它们路由到一个或多个目标进行处理。

      这是示例 CloudFormation 代码段:

      Type: AWS::Events::Rule
      Properties: 
        Description: String
        EventPattern: JSON object
        Name: String
        ScheduleExpression: String
        State: String
        Targets:
          - Target
      

      这里是official documentation,如果您还有其他问题。

      【讨论】:

        【解决方案3】:

        是的,@bhalothia 可以分享。请找到一篇能让您深入了解它的文章。

        实际实施:

        http://marcelog.github.io/articles/serverless_cloudwatch_event_cloudformation_template.html

        详细记录:

        https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-events-rule-target.html

        希望对你有帮助。

        【讨论】:

          猜你喜欢
          • 2016-06-13
          • 2019-01-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-11-03
          • 2020-11-27
          • 2020-02-14
          • 1970-01-01
          相关资源
          最近更新 更多