Resource Orchestration Service (ROS) provides the Count feature. The feature 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, which is inefficient. The Count feature allows you to create multiple resources at a time in an efficient manner. 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 the maximum number of resources that you can create at a time.

Step 1: Compile a template

You can use Count to compile 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

Sample code and 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 this template to create four EIPs.

      Set the Count parameter to 4 and set 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. The first batch includes Eip[0] and Eip[1]. The second batch includes Eip[2] and Eip[3].

      {
          "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 this template to create four ECS instances.

      Reference the Count parameter in the MaxAmount property of the ALIYUN::ECS::InstanceGroup resource type, and set the Count parameter to 4. 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 this template 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 set the ParallelCount parameter to 2. In this case, four EipBind resources are created in two batches. After ROS preprocesses the template, four EIPs are generated in two batches. The first batch includes EipBind[0] and EipBind[1]. The second batch includes EipBind[2] and EipBind[3]. ALIYUN::Index is referenced 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 upper-left corner of the page, select the region in which you want to create a stack from the drop-down list.
  4. On the Stacks page, click Create Stack and select Use New Resources (Standard) from the drop-down list.
  5. In the Select Template step of the Use New Resources (Standard) wizard, click Select an Existing Template in the Specify Template section.
  6. Set the Template Import Method parameter to Enter Template Content. Enter the template content that is compiled in Step 1: Compile a template and click Next.
  7. In the Configure Template Parameters step, configure the Stack Name parameter and other parameters. Then, click Next.
  8. In the Configure Stack step, configure the Stack Policy, Rollback on Failure, Timeout Period, Deletion Protection, RAM Role, Tags, and Resource Group parameters. Then, click Next.
    If no resources are 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 to the state before the resources are created or updated based on the Rollback on Failure settings.
  9. In the Check and Confirm step, click Create.

Step 3: (Optional) View the resources and template

After you create the stack, 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 shows a 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"
              ]
            }
          ]
        }
      }
    }