All Products
Search
Document Center

Resource Orchestration Service:Use Count to create multiple resources at a time

Last Updated:Feb 01, 2024

Resource Orchestration Service (ROS) provides the Count feature that allows you to create multiple resources at a time. This topic describes how to use Count to create multiple Elastic Compute Service (ECS) instances and elastic IP addresses (EIPs) at a time and bind the EIPs to the ECS instances.

Background information

ALIYUN::VPC::EIP is used to apply for EIPs. If you use a template to apply for multiple EIPs at a time, you must specify multiple ALIYUN::VPC::EIP resources in the template. This makes the template long-winded. In this case, you can use Count in the template to efficiently create multiple resources at a time. For more information about Count, see Count. When you use Count to create multiple resources at a time, you can specify the DependsOn property and the ParallelCount parameter to control concurrency during the resource creation.

Step 1: Create a template

You can use Count to create a template and use the template to create the following resources:

  • One virtual private cloud (VPC)

  • One vSwitch

  • One security group

  • Four pay-as-you-go ECS instances

  • Four EIPs

The following sections provide the sample code and the description of the template.

  • Sample code:

    ROSTemplateFormatVersion: '2015-09-01'
    Parameters:
      Count:
        Type: Number
        Default: 4
      ZoneId:
        Type: String
      InstanceType:
        Type: String
        Default: ecs.c6.large
      Password:
        Type: String
        Default: Abc1****
        NoEcho: true
      ParallelCount:
        Type: Number
        Default: 2
      SystemDiskCategory:
        Type: String
    Resources:
      Vpc:
        Type: ALIYUN::ECS::VPC
        Properties:
          CidrBlock: 10.0.0.0/8
          VpcName: test-resource-count
      VSwitch:
        Type: ALIYUN::ECS::VSwitch
        Properties:
          CidrBlock: 10.0.10.0/24
          ZoneId:
            Ref: ZoneId
          VpcId:
            Ref: Vpc
      SecurityGroup:
        Type: ALIYUN::ECS::SecurityGroup
        Properties:
          SecurityGroupName: test-resource-count
          VpcId:
            Ref: Vpc
      Eip:
        Type: ALIYUN::VPC::EIP
        Count:
          Ref: Count
        DependsOn:
          Fn::Select:
            - Fn::Calculate:
                - 1-{0}//{1}
                - 0
                - - Fn::Min:
                      - Ref: ALIYUN::Index
                      - Ref: ParallelCount
                  - Ref: ParallelCount
            - - Fn::Replace:
                  - index:
                      Fn::Calculate:
                        - '{0}-{1}'
                        - 0
                        - - Ref: ALIYUN::Index
                          - Ref: ParallelCount
                  - Eip[index]
        Properties:
          Bandwidth: 5
      Servers:
        Type: ALIYUN::ECS::InstanceGroup
        Properties:
          ImageId: centos_7
          InstanceType:
            Ref: InstanceType
          VpcId:
            Ref: Vpc
          VSwitchId:
            Ref: VSwitch
          SecurityGroupId:
            Ref: SecurityGroup
          Password:
            Ref: Password
          AllocatePublicIP: false
          MaxAmount:
            Ref: Count
          SystemDiskCategory:
            Ref: SystemDiskCategory
      EipBind:
        Type: ALIYUN::VPC::EIPAssociation
        Count:
          Ref: Count
        DependsOn:
          Fn::Select:
            - Fn::Calculate:
                - 1-{0}//{1}
                - 0
                - - Fn::Min:
                      - Ref: ALIYUN::Index
                      - Ref: ParallelCount
                  - Ref: ParallelCount
            - - Fn::Replace:
                  - index:
                      Fn::Calculate:
                        - '{0}-{1}'
                        - 0
                        - - Ref: ALIYUN::Index
                          - Ref: ParallelCount
                  - EipBind[index]
        Properties:
          InstanceId:
            Fn::Select:
              - Ref: ALIYUN::Index
              - Fn::GetAtt:
                  - Servers
                  - InstanceIds
          AllocationId:
            Fn::Select:
              - Ref: ALIYUN::Index
              - Ref: Eip
    Outputs:
      InstanceIds:
        Value:
          Fn::GetAtt:
            - Servers
            - InstanceIds
      AllocationIds:
        Value:
          Ref: Eip
      EipAddresses:
        Value:
          Fn::GetAtt:
            - Eip
            - EipAddress
  • Description:

    • You can use the following template fragment to create four EIPs.

      Set the Count parameter to 4 and the ParallelCount parameter to 2. In this case, four EIPs are created in two batches. After ROS preprocesses the template, four EIPs are generated in two batches. In the first batch, Eip[0] and Eip[1] are generated. In the second batch, Eip[2] and Eip[3] are generated.

      Eip:
        Type: ALIYUN::VPC::EIP
        Count:
          Ref: Count
        DependsOn:
          Fn::Select:
            - Fn::Calculate:
                - 1-{0}//{1}
                - 0
                - - Fn::Min:
                      - Ref: ALIYUN::Index
                      - Ref: ParallelCount
                  - Ref: ParallelCount
            - - Fn::Replace:
                  - index:
                      Fn::Calculate:
                        - '{0}-{1}'
                        - 0
                        - - Ref: ALIYUN::Index
                          - Ref: ParallelCount
                  - Eip[index]
        Properties:
          Bandwidth: 5
    • You can use the following template fragment to create four ECS instances.

      Set the Count parameter to 4 and reference the Count parameter in the MaxAmount property of ALIYUN::ECS::InstanceGroup. In this case, four ECS instances are created.

      Servers:
        Type: ALIYUN::ECS::InstanceGroup
        Properties:
          ImageId: centos_7
          InstanceType:
            Ref: InstanceType
          VpcId:
            Ref: Vpc
          VSwitchId:
            Ref: VSwitch
          SecurityGroupId:
            Ref: SecurityGroup
          Password:
            Ref: Password
          AllocatePublicIP: false
          MaxAmount:
            Ref: Count
          SystemDiskCategory:
            Ref: SystemDiskCategory
    • You can use the following template fragment to create four EipBind resources and use the ALIYUN::Index pseudo parameter to bind the EIPs to the ECS instances one by one.

      Set the Count parameter to 4 and the ParallelCount parameter to 2. In this case, four EipBind resources are created in two batches. After ROS preprocesses the template, four EipBind resources are generated in two batches. In the first batch, EipBind[0] and EipBind[1] are generated. In the second batch, EipBind[2] and EipBind[3] are generated. Then, reference ALIYUN::Index is in Count. When ROS preprocesses the template, ROS replaces ALIYUN::Index with numeric values. In this example, Eip[0] is bound to the first ECS instance, Eip[1] is bound to the second ECS instance, Eip[2] is bound to the third ECS instance, and Eip[3] is bound to the fourth ECS instance.

      EipBind:
        Type: ALIYUN::VPC::EIPAssociation
        Count:
          Ref: Count
        DependsOn:
          Fn::Select:
            - Fn::Calculate:
                - 1-{0}//{1}
                - 0
                - - Fn::Min:
                      - Ref: ALIYUN::Index
                      - Ref: ParallelCount
                  - Ref: ParallelCount
            - - Fn::Replace:
                  - index:
                      Fn::Calculate:
                        - '{0}-{1}'
                        - 0
                        - - Ref: ALIYUN::Index
                          - Ref: ParallelCount
                  - EipBind[index]
        Properties:
          InstanceId:
            Fn::Select:
              - Ref: ALIYUN::Index
              - Fn::GetAtt:
                  - Servers
                  - InstanceIds
          AllocationId:
            Fn::Select:
              - Ref: ALIYUN::Index
              - Ref: Eip

Step 2: Create a stack

  1. Log on to the ROS console.

  2. In the left-side navigation pane, click Stacks.

  3. In the top navigation bar, select the region where you want to create a stack from the region drop-down list. For example, you can select China (Hangzhou).

  4. On the Stacks page, click Create Stack and select Use ROS from the drop-down list.

  5. In the Specify Template section of the Select Template step, select Select an Existing Template.

  6. Set the Template Import Method parameter to Enter Template Content, enter the content of the template that you created in Step 1: Create a template, and then click Next.

  7. In the Configure Parameters step, specify the Stack Name parameter and configure parameters in the Configure Template Parameters section.

  8. In the Configure Stack Settings section, follow on-screen instructions to configure the Stack Policy, Rollback on Failure, Timeout Period, Deletion Protection, RAM Role, Tags, and Resource Group parameters. Then, click Next.

    If the resources are not created or updated within the time that is specified by the Timeout Period parameter, the system considers the operation failed. Then, the system determines whether to roll back the resources to the state before they are created or updated based on the value of the Rollback on Failure parameter.

  9. In the Compliance Precheck step, complete the compliance precheck and click Next.

    Note

    The compliance precheck feature is available only for specific resources. For more information, see Compliance precheck of ROS.

    1. In the Evaluate Rules section, add compliance rules.

      You can add compliance rules based on the cloud resources in the ROS template.

    2. Click Evaluate.

      You can modify the template content based on the suggestions that are provided to remediate invalid resources to ensure the compliance of the resources.

  10. In the Check and Confirm step, click Create.

Step 3: (Optional) View the resources and template

After the stack is created, you can view the information about the resources and the template that are preprocessed by ROS on the stack details page.

  1. On the stack details page, click the Resources tab to view the information about the resources.

  2. On the stack details page, click the Template tab to view the information about the template.

    The following sample code provides an example of the preprocessed template:

    ROSTemplateFormatVersion: '2015-09-01'
    Resources:
      EipBind[1]:
        Type: ALIYUN::VPC::EIPAssociation
        Properties:
          InstanceId:
            Fn::Select:
              - 1
              - Fn::GetAtt:
                  - Servers
                  - InstanceIds
          AllocationId:
            Ref: Eip[1]
      Eip[1]:
        Type: ALIYUN::VPC::EIP
        Properties:
          Bandwidth: 5
      SecurityGroup:
        Type: ALIYUN::ECS::SecurityGroup
        Properties:
          VpcId:
            Ref: Vpc
          SecurityGroupName: test-resource-count
      Servers:
        Type: ALIYUN::ECS::InstanceGroup
        Properties:
          SystemDiskCategory:
            Ref: SystemDiskCategory
          VpcId:
            Ref: Vpc
          SecurityGroupId:
            Ref: SecurityGroup
          ImageId: centos_7
          AllocatePublicIP: false
          VSwitchId:
            Ref: VSwitch
          Password:
            Ref: Password
          InstanceType:
            Ref: InstanceType
          MaxAmount:
            Ref: Count
      Eip[2]:
        Type: ALIYUN::VPC::EIP
        Properties:
          Bandwidth: 5
        DependsOn: Eip[0]
      Eip[0]:
        Type: ALIYUN::VPC::EIP
        Properties:
          Bandwidth: 5
      Vpc:
        Type: ALIYUN::ECS::VPC
        Properties:
          VpcName: test-resource-count
          CidrBlock: 10.0.0.0/8
      Eip[3]:
        Type: ALIYUN::VPC::EIP
        Properties:
          Bandwidth: 5
        DependsOn: Eip[1]
      VSwitch:
        Type: ALIYUN::ECS::VSwitch
        Properties:
          VpcId:
            Ref: Vpc
          CidrBlock: 10.0.10.0/24
          ZoneId:
            Ref: ZoneId
      EipBind[3]:
        Type: ALIYUN::VPC::EIPAssociation
        Properties:
          InstanceId:
            Fn::Select:
              - 3
              - Fn::GetAtt:
                  - Servers
                  - InstanceIds
          AllocationId:
            Ref: Eip[3]
        DependsOn: EipBind[1]
      EipBind[0]:
        Type: ALIYUN::VPC::EIPAssociation
        Properties:
          InstanceId:
            Fn::Select:
              - 0
              - Fn::GetAtt:
                  - Servers
                  - InstanceIds
          AllocationId:
            Ref: Eip[0]
      EipBind[2]:
        Type: ALIYUN::VPC::EIPAssociation
        Properties:
          InstanceId:
            Fn::Select:
              - 2
              - Fn::GetAtt:
                  - Servers
                  - InstanceIds
          AllocationId:
            Ref: Eip[2]
        DependsOn: EipBind[0]
    Parameters:
      Count:
        Default: 4
        Type: Number
      SystemDiskCategory:
        Type: String
      ZoneId:
        Type: String
      Password:
        Default: Abc1****
        NoEcho: true
        Type: String
      InstanceType:
        Default: ecs.c6.large
        Type: String
      ParallelCount:
        Default: 2
        Type: Number
    Outputs:
      AllocationIds:
        Value:
          - Ref: Eip[0]
          - Ref: Eip[1]
          - Ref: Eip[2]
          - Ref: Eip[3]
      InstanceIds:
        Value:
          Fn::GetAtt:
            - Servers
            - InstanceIds
      EipAddresses:
        Value:
          - Fn::GetAtt:
              - Eip[0]
              - EipAddress
          - Fn::GetAtt:
              - Eip[1]
              - EipAddress
          - Fn::GetAtt:
              - Eip[2]
              - EipAddress
          - Fn::GetAtt:
              - Eip[3]
              - EipAddress