如何在AWS CloudFormation的Parameters部分中添加条件?

问题描述

所以我想做的是,定义了一个名为EnvType的参数,并允许其值为test或production。

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

如果用户选择生产作为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参数的允许值中容易出错,但是我正在寻找一种替代方法。

请帮助!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)