简体   繁体   中英

Cloudformation Stack Template Conditions

How can I create a Cloudformation Template with Conditions clause to create 10 instances as a parameter.

I have used the following example for upto 5 instances. But how can I use it for more than 5? It depends on how we create our Conditions clause.

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]

Can you help me expand this example upto 10 instances?

I highly suggest you rely on AutoScaling Groups ( AWS::AutoScaling::AutoScalingGroup ). This way you'll be able to reference the instance count using the DesiredCapacity property. You'll have a ton of other benefits by doing so too.

EDIT: but if you still wanted to do it using conditions, this is what you would need to do:

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

I've used bucket so it would be faster to test

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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