All Products
Search
Document Center

Resource Orchestration Service:Use Count to create multiple resources

Last Updated:Jun 16, 2026

Resource Orchestration Service (ROS) provides the Count feature to create multiple resources in batches. This example uses Count to create multiple ECS instances and elastic IP addresses (EIPs) and bind them to their corresponding ECS instances.

Background

Provisioning an elastic IP address (EIP) requires the ALIYUN::VPC::EIP resource type. Declaring multiple ALIYUN::VPC::EIP resources makes the template verbose. The Count feature lets you create multiple resources efficiently. For more information, see Count. You can also specify the DependsOn attribute and set the ParallelCount parameter to control resource creation concurrency.

Step 1: Write the template

This template uses the Count feature to create the following resources:

  • One Virtual Private Cloud (VPC)

  • One vSwitch

  • One security group

  • Four pay-as-you-go ECS instances

  • Four elastic IP addresses (EIPs)

The following sections provide the sample template and explain its components.

  • Sample template

    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
  • Template explanation

    • Create four EIPs.

      The Count parameter is set to 4. Because ParallelCount is set to 2, ROS creates the four EIPs in two batches. After preprocessing, ROS generates four resources: Eip[0] and Eip[1] (Batch 1), then Eip[2] and Eip[3] (Batch 2).

      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
    • Create four ECS instances.

      The Count parameter is set to 4. The MaxAmount property of the ALIYUN::ECS::InstanceGroup resource references the Count parameter to create four ECS instances.

      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
    • Create four EipBind resources to sequentially bind the ECS instances to the EIPs using the ALIYUN::Index pseudo parameter.

      The Count parameter is set to 4, and ParallelCount is set to 2, so ROS creates the four EipBind resources in two batches. After preprocessing, ROS generates four resources: EipBind[0] and EipBind[1] (Batch 1), then EipBind[2] and EipBind[3] (Batch 2). During preprocessing, ROS replaces ALIYUN::Index with the current iteration number, binding each instance to its corresponding EIP: the first instance to Eip[0], the second to Eip[1], and so on.

      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 navigation pane on the left, click Stacks.

  3. In the top navigation bar, select the region where you want to create the stack, such as China (Hangzhou).

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

  5. On the Select Template page, in the Specify Template section, click Select an Existing Template.

  6. In the Template Import Method section, click Enter Template Content, paste the template content from Step 1: Write the template, and then click Next.

  7. On the Configure Parameters page, set the Stack Name and configure the parameters in the Template Parameters section.

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

    If a resource is not created or updated within the specified Timeout Period, the operation fails. The system then uses the Rollback on Failure setting to determine whether to roll back the resources.

  9. On the Compliance Precheck page, complete the compliance check, and then click Next.

    Note

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

    1. In the Detection Rules section, add detection rules.

      Select detection rules based on the cloud resources defined in the ROS template.

    2. Click Start Check.

      If a resource is found to be Non-compliant, you can click Remediation Plan and modify the cloud resource configuration or ROS template content based on the Remediation Plan to ensure resource compliance.

  10. On the Check and Confirm page, click Create.

Step 3 (Optional): View resources and template

After the stack is created, you can view the resources and preprocessed template on the stack details page.

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

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

    The following 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