在CloudFormation中,在lambda计划中使用输入参数。

huangapple go评论116阅读模式
英文:

Use input parameter within lambda schedule in Cloudformation

问题

我正在尝试根据环境来启用/禁用 Lambda 定时事件,但运气不太好。如果环境是 prod,它应该启用计划,如果是 dev,它应该禁用计划。然而,计划状态并不遵守条件。目前在 dev 环境中它是启用的,但模板没有禁用它。如果我手动将 Enabled 属性设置为 false,计划状态确实会变为禁用。所以不确定我做错了什么(或者我尝试的是否可行)。非常感谢任何帮助!

Parameters:
  env:
    Type: String

Conditions:
  isProd:
    !Equals [!Ref env, prod]

Resources:
  func:
    Type: AWS::Serverless::Function
    Properties:
      Events:
        ScheduledEvent:
          Type: Schedule
          Properties:
            Schedule: rate(5 minutes)
            Enabled: !If [isProd, true, false]
英文:

I'm trying to enable/disable lambda scheduled event based on environment, and not having much luck. If the env is prod, it should enable the schedule, and if it's dev it should disable it. However, the schedule status doesn't obey the condition. It's currently enabled in dev, but the template doesn't disable it. If I set the Enabled property to false manually, the schedule status does change to disable. So not sure where I'm going wrong (or if what I'm trying to do is possible). Any help is greatly appreciated!

Parameters:
  env:
    Type: String

Conditions:
  isProd:
    !Equals [!Ref env, prod]

Resources:
  func:
    Type: AWS::Serverless::Function
    Properties:
      Events:
        ScheduledEvent:
          Type: Schedule
          Properties:
            Schedule: rate(5 minutes)
            Enabled: !If [isProd, true, false]

答案1

得分: 3

好的,以下是翻译好的内容:

好的,经过进一步搜索,看起来使用条件语句设置Enabled属性并不太好用。我采用的解决方法是在Schedule属性中使用条件语句。

ScheduledEvent:
  Type: Schedule
  Properties:
    Schedule: !If [isProd, "rate(5 minutes)", "cron(00 00 01 01 ? 1970)"]

感谢以下帖子指导我正确的方向:

https://stackoverflow.com/questions/67113915/conditionally-enabling-an-event-schedule-in-aws-sam-template-file

https://stackoverflow.com/questions/63779940/dynamically-change-event-properties-on-aws-cloudformation-templates

英文:

Okay, so after a bit more searching, it looks like setting the Enabled property with conditional statements doesn't work so nice. The workaround I've put in place is to use conditional statements in the Schedule property.

ScheduledEvent:
  Type: Schedule
  Properties:
    Schedule: !If [isProd, "rate(5 minutes)", "cron(00 00 01 01 ? 1970)"]

Thanks to these posts for point me in the right direction:

https://stackoverflow.com/questions/67113915/conditionally-enabling-an-event-schedule-in-aws-sam-template-file

https://stackoverflow.com/questions/63779940/dynamically-change-event-properties-on-aws-cloudformation-templates

huangapple
  • 本文由 发表于 2022年2月9日 14:51:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/71045130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定