简体   繁体   English

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

[英]How to add conditions in Parameters section in AWS CloudFormation?

So what I am trying to do is, I defined a Parameter called EnvType with allowed values test or production.所以我想要做的是,我定义了一个名为 EnvType 的参数,允许值测试或生产。

What should happen is when the user selects one of these environments test or production, lets say test then he should be able to select another parameter called InstanceType in which allowed values would be all the 't' type instances in the drop-down list while creating the stack.应该发生的情况是,当用户选择这些环境中的一个测试或生产时,假设测试然后他应该能够选择另一个名为 InstanceType 的参数,其中允许的值将是下拉列表中的所有“t”类型实例,而创建堆栈。

If the user selects production as the EnvType then the allowed values under the same parameter called InstanceType must be all instances types except 't' type for eg ('m' type).如果用户选择生产作为 EnvType,那么在名为 InstanceType 的同一参数下允许的值必须是除 't' 类型之外的所有实例类型,例如('m' 类型)。

And the same must apply for rds as well.同样必须适用于 rds。 Let's say user chooses EnvType as test then allowed values under parameter called DBInstanceType must be 'db.t' type instances otherwise 'db.r' type instances.假设用户选择 EnvType 作为测试,然后在名为 DBInstanceType 的参数下允许的值必须是 'db.t' 类型实例,否则为 'db.r' 类型实例。

Parameters参数

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
       

Mapping映射

    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资源

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

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

Well i know the template is not valid, and is prone to errors in allowed values in InstanceType and DBInstanceType parameters but i am seeking an alternative of doing this.好吧,我知道模板无效,并且在 InstanceType 和 DBInstanceType 参数中的允许值中容易出错,但我正在寻找替代方法。

Please help!!请帮忙!!

Simply, such functionality is not possible in CloudFormation (CFN).简单地说,这样的功能在 CloudFormation (CFN) 中是不可能的 Sadly, there is no alternative in CFN for that.可悲的是,CFN 对此别无选择。 You would have to develop a custom solution for deployment of such templates.您必须为部署此类模板开发自定义解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM