如何在 AWS CloudFormation 的参数部分添加条件?

How to add conditions in Parameters section in AWS CloudFormation?

所以我想做的是,我定义了一个名为 EnvType 的参数,其允许值为测试或生产。

当用户 select 在这些环境之一测试或生产时应该发生什么,比方说测试 那么他应该能够 select 另一个名为 InstanceType 的参数,其中允许的值将是创建堆栈时下拉列表中的所有 't' 类型实例。

如果用户selects production 作为 EnvType,则在名为 InstanceType 的同一参数下的允许值必须是除 't' 类型之外的所有实例类型,例如('m' 类型) .

同样也必须适用于 rds。假设用户选择 EnvType 作为测试,然后名为 DBInstanceType 的参数下的允许值必须是 'db.t' 类型实例,否则 'db.r' 类型实例。

参数

Parameters:
  
  EnvType: 
    Default: test
    Type: String
    AllowedValues: 
      - production
      - test
  
  InstanceType:
    Type: String
    AllowedValues: !FindInMap 
      - InstanceTypeMap
      - !Ref EnvType
      - instanceType
      
  DBInstanceType:
    Type: String
    AllowedValues: !FindInMap 
      - InstanceTypeMap
      - !Ref EnvType
      - dbinstanceType
       

映射

    InstanceTypeMap:
      production:
        instanceType: [m1.small, m1.medium, m1.large, m1.xlarge, m2.xlarge, m2.2xlarge, m2.4xlarge]
        dbinstancetype: [db.r5.large, db.r5.xlarge]
      test:
        instanceType: [t1.micro, t2.nano, t2.micro, t2.small, t2.medium, t2.large]
        dbinstancetype: [db.t2.small, db.t2.medium, db.t3.small]
      
        
        

资源

Resources:
  WebServer:
    Type: 'AWS::EC2::Instance'
    Properties:
      InstanceType: !Ref InstanceType
   

  DBInstance:
    Type: AWS::RDS::DBInstance
    Properties:
      DBInstanceClass: !FindInMap 
        - MyEnvironmentMap
        - !Ref EnvType
        - dbinstanceType 
     

好吧,我知道模板无效,并且 InstanceType 和 DBInstanceType 参数中的允许值容易出错,但我正在寻找替代方法。

请帮忙!!

简单地说,这样的功能在 CloudFormation (CFN) 中不可能。遗憾的是,在 CFN 中没有其他选择。您必须为部署此类模板开发自定义解决方案。