AWS 云表单任务定义条件命令

AWS cloud form task definition conditional command

假设我有一个应用程序能够在干 运行 模式下 运行,由命令行上的标志设置; myapp --dryrun,其任务定义的 CloudForm 是:

MyTaskDefinition
  Type: AWS::ECS::TaskDefinition
  Properties:
    ContainerDefinitions:
    - Name: myApp
      Image: user/myapp:latest
      Command:
      - ./myapp
      - --dryrun
      Environment:
      - Name: SOME_ENV_VAR
        Value: !Ref SomeEnvVar

我正在尝试为可在开发和生产环境中使用的任务定义创建一个 CloudForm 模板,其中干 运行 标志仅为开发环境设置。

有没有什么方法可以设置条件命令,或者我要求助于我传入的骇人听闻的字符串:

      Command:
      - ./myapp
      - !Ref DryRun

最简洁的解决方案是使用 If 函数,当为真时设置标志,如果为假则使用 AWS::NoValue

AWS::NoValue 将完全删除 属性,这意味着 true 的命令是 ["./myapp", "--dryrun"] 而 false 的命令是 ["./myapp"].

不幸的是,CloudForm 参数没有 Bool 类型,因此您必须将其作为 String 传递,然后使用 ConditionStringBool.

MyTaskDefinition
  Type: AWS::ECS::TaskDefinition
  Properties:
    ContainerDefinitions:
    - Name: myApp
      Image: user/myapp:latest
      Command:
      - ./myapp
      - Fn::If:
        - UseDryRun
        - --dryrun
        - Ref: AWS::NoValue

Parameters:
  DryRun:
    Type: String
    AllowedValues: # No bool parameter in CFN
    - true
    - false

Conditions:
  UseDryRun: !Equals [ !Ref DryRun, true ]