Cloudformation堆栈模板条件

时间:2017-12-22 18:36:53

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

如何使用Conditions子句创建Cloudformation模板以创建10个实例作为参数。

我使用以下示例最多5个实例。但是如何使用它超过5?这取决于我们如何创建条件子句。

Conditions:
  Launch1: !Equals [1, 1]
  Launch2: !Not [!Equals [1, !Ref InstanceCount]]
  Launch3: !Or
  - !Not [!Equals [1, !Ref InstanceCount]]
  - !Not [!Equals [2, !Ref InstanceCount]]
  Launch4: !Or
  - !Equals [4, !Ref InstanceCount]
  - !Equals [5, !Ref InstanceCount]
  Launch5: !Equals [5, !Ref InstanceCount]

你能帮助我将这个例子扩展到10个实例吗?

1 个答案:

答案 0 :(得分:1)

高度建议您依赖AutoScaling Groups(AWS::AutoScaling::AutoScalingGroup)。这样,您就可以使用DesiredCapacity属性引用实例计数。通过这样做,您还可以获得许多其他好处。

编辑:但如果您仍想使用条件进行操作,那么您需要这样做:

AWSTemplateFormatVersion: "2010-09-09"
Parameters:
  InstanceCount:
    Type: Number

Conditions:
  Launch10: !Equals [10, !Ref InstanceCount]
  Launch9: !Or
  - !Equals [9, !Ref InstanceCount]
  - !Condition Launch10
  Launch8: !Or
  - !Equals [8, !Ref InstanceCount]
  - !Condition Launch9
  Launch7: !Or
  - !Equals [7, !Ref InstanceCount]
  - !Condition Launch8
  Launch6: !Or
  - !Equals [6, !Ref InstanceCount]
  - !Condition Launch7
  Launch5: !Or
  - !Equals [5, !Ref InstanceCount]
  - !Condition Launch6
  Launch4: !Or
  - !Equals [4, !Ref InstanceCount]
  - !Condition Launch5
  Launch3: !Or
  - !Equals [3, !Ref InstanceCount]
  - !Condition Launch4
  Launch2: !Or
  - !Equals [2, !Ref InstanceCount]
  - !Condition Launch3
  Launch1: !Or
  - !Equals [1, !Ref InstanceCount]
  - !Condition Launch2

Resources:
  Bucket1:
    Condition: Launch1
    Type: AWS::S3::Bucket

  Bucket2:
    Condition: Launch2
    Type: AWS::S3::Bucket

  Bucket3:
    Condition: Launch3
    Type: AWS::S3::Bucket

  Bucket4:
    Condition: Launch4
    Type: AWS::S3::Bucket

  Bucket5:
    Condition: Launch5
    Type: AWS::S3::Bucket

  Bucket6:
    Condition: Launch6
    Type: AWS::S3::Bucket

  Bucket7:
    Condition: Launch7
    Type: AWS::S3::Bucket

  Bucket8:
    Condition: Launch8
    Type: AWS::S3::Bucket

  Bucket9:
    Condition: Launch9
    Type: AWS::S3::Bucket

  Bucket10:
    Condition: Launch10
    Type: AWS::S3::Bucket

我已经使用了存储桶,因此测试

会更快