我如何在 cloudformation yaml 文件中指定使用参数值,否则通过 FindInMap 使用默认值

How can I specify in cloudformation yaml file to use parameter value otherwise a default via FindInMap

假设我有一个 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:''

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

为数据库设置一些条件

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 部分定义。