【问题标题】:How can I specify in cloudformation yaml file to use parameter value otherwise a default via FindInMap如何在 cloudformation yaml 文件中指定使用参数值,否则通过 FindInMap 使用默认值
【发布时间】:2018-07-24 21:28:33
【问题描述】:

假设我有一个 ecs.yml 文件,该文件指定要在环境 JAVA_OPTIONS 值中设置的主机名的查找映射。查找键是用于其他设置的帐户 ID 值。但我希望能够在模板的某些用法中覆盖地图的使用。

  AccId:
     Type: String
  AccountMap:
     DomainName:
          "01" :"https://production.example.com",
          "02" :"https://test.example.com",
          "03" :"https://pref.example.com"

并且拥有

 TaskDefinition:
    Type: AWS::ECS::TaskDefinition
        Properties:
           ...<snip>...
           Environment: 
                 - Name: JAVA_OPTIONS
                   Value: !Sub
                     - "-DSERVER_HOST=${ServerHost} -DACC=${AccId}"
                     - !FindInMap [AccountMap, "ServerHost", !Ref "AccId"], AccId: !Ref AccId

但我想要另一个模板参数,即 SeverHostOveride,如果 SeverHostOveride 有值,它会覆盖使用 AccountMap 查找来设置 ServerHost。

  ServerHostOveride:
       Type:String 
       Default:''

【问题讨论】:

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


    【解决方案1】:

    我相信您可以使用Conditions 实现这一目标。我已经将它用于更简单的事情(教程中的代码示例)。这就是我的工作

    为DB设置一些条件

    Conditions:
      isLarge:
        !Equals [!Ref EnvironmentSize, "LARGE"]
      isntLarge:
        !Not [!Equals [!Ref EnvironmentSize, "LARGE"]]
      isRestore:
        !Not [!Equals [!Ref SnapToRestore, ""]]
    

    Resources 下,我正在使用这些条件创建数据库(isntLarge & isRestore)

    DB:
    Type: "AWS::RDS::DBInstance"
    Condition: isntLarge # added - only create the MySQL DB if its small/med
    DeletionPolicy: Snapshot
    Properties:
      AllocatedStorage: 5
      DBInstanceClass: !FindInMap [InstanceSize, !Ref EnvironmentSize, DB]
      DBName: !If [isRestore, !Ref "AWS::NoValue", !Ref DatabaseName]
      Engine: MySQL
      StorageType: gp2
      MasterUsername: !If [isRestore, !Ref "AWS::NoValue", !Ref DatabaseUser]
      MasterUserPassword: !If [isRestore, !Ref "AWS::NoValue", !Ref DatabasePassword]
      DBSnapshotIdentifier: !If [isRestore, !Ref SnapToRestore, !Ref "AWS::NoValue"]
    

    希望对您有所帮助。 PS:EnvironmentSize & SnapToRestore 在我的 yaml 的 Parameters 部分中定义。

    【讨论】:

    • 这很有帮助,感谢最终得到了条件和类似ServerHost: !If [hasServerOverride, !Ref "ServerOverride", !FindInMap [AccountMap, "ServerHost", !Ref "AWS::AccId"]]的构造
    猜你喜欢
    • 2010-10-10
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 2012-08-01
    • 2010-11-08
    • 1970-01-01
    相关资源
    最近更新 更多