问题描述
这是我的(缩短的)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 condition 或 If 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,而且只是将 Enabled 标志设置为 False 以确保它实际上会禁用计划,它确实做到了。在这一点上我很难过,有没有人知道我做错了什么或我可以尝试什么?
解决方法
我遇到了同样的问题。显然,Enabled
属性不适用于 !If
或 !Equals
。我通过 Schedule
属性中的 hack 修复了它:
Events:
InvocationLevel:
Type: Schedule
Properties:
Schedule: !If [IsQcEnv,"rate(2 hours)","cron(0 0 31 2 ? *)"]
如果 envType 不是 qc1,将使用 cron 表达式。此表达式将在 2 月 31 日执行您的函数。这意味着它永远不会执行。
我在这些答案的帮助下找到了这个解决方案: