【问题标题】:Conditionally enabling an event schedule in AWS sam template file有条件地在 AWS sam 模板文件中启用事件计划
【发布时间】:2021-07-10 19:29:34
【问题描述】:

这是我的(缩短的)template.yml 文件:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: CF template for foobar

Parameters:
  EnvType:
    Type: String
    Description: The environment to deploy to.
    AllowedValues:
      - dev1
      - qc1
      - uat1
    Default: dev1

  VeryImportantFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: src/very_important/
      Handler: app.very_important
      Runtime: python3.8
      {unrelated policies and environment variables}
      Events:
        VeryImportantSchedule:
          Type: Schedule
          Properties:
            Schedule: 'rate(2 hours)'
            Enabled: True

我要做的只是在 EnvType 参数设置为 qc1 时启用该计划。问题是,我无法让Equals conditionIf condition 工作。我已经尝试了以下所有方法:

Enabled: !Equals [!Ref EnvType, qc1]
Enabled: !Equals [!Ref EnvType, "qc1"]

Conditions:
  IsQcEnv: !Equals [!Ref EnvType, qc1]
  {and}
  IsQcEnv: !Equals [!Ref EnvType, "qc1"]

....
....
....
      Events:
        VeryImportantSchedule:
          Type: Schedule
          Properties:
            Schedule: 'rate(2 hours)'
            Enabled: !If [IsQcEnv, True, False]

在测试所有这四种情况之间,我删除了我的整个堆栈,而不是尝试更新它们,作为this post suggested. 中的评论,我还三次检查传入的参数不是 qc1,并且还设置了启用标志为 False 以确保它实际上会禁用计划,它确实这样做了。在这一点上我很困惑,有没有人知道我做错了什么或者我可以尝试什么?

【问题讨论】:

  • 为什么它不起作用?有任何错误信息吗? Parring 错误?语法错误?堆栈回滚?
  • @Marcin:我遇到了同样的问题。 AWS 没有抱怨,但计划始终处于启用状态。

标签: amazon-web-services amazon-cloudformation aws-event-bridge


【解决方案1】:

我遇到了同样的问题。显然Enabled 属性不适用于!If!Equals。我在Schedule 属性中进行了修改:

 Events:
   InvocationLevel:
     Type: Schedule
     Properties:
        Schedule: !If [IsQcEnv, "rate(2 hours)", "cron(0 0 31 2 ? *)"]

如果 envType 不是 qc1,将使用 cron 表达式。此表达式将在 2 月 31 日执行您的函数。这意味着它永远不会执行。

我在这些答案的帮助下找到了这个解决方案:

https://stackoverflow.com/a/65214717/517173

https://stackoverflow.com/a/13938155/517173

【讨论】:

  • 我和我的团队刚刚开始尝试这个,它成功了!非常感谢:)
猜你喜欢
  • 1970-01-01
  • 2019-02-14
  • 2022-06-14
  • 2019-07-29
  • 2020-07-20
  • 2016-09-12
  • 1970-01-01
  • 2019-04-24
  • 1970-01-01
相关资源
最近更新 更多