Resource Orchestration Service (ROS) allows you to batch create resources by using the Count feature.

Background information

ALIYUN::VPC::EIP is used to apply for an elastic IP address (EIP). If you want to apply for multiple EIPs at once, you can specify multiple ALIYUN::VPC::EIP resources in a template. However, this makes the template verbose. In this case, you can use the Count feature to batch create resources. For more information about the Count feature, see Count.

The Count feature is used in this topic to batch create Elastic Compute Service (ECS) instances and EIPs, and associate the EIPs with the ECS instances. The following resources are created in the example:

  • One virtual private cloud (VPC)
  • One vSwitch
  • One security group
  • Two pay-as-you-go ECS instances
  • Two EIPs

Procedure

  1. Log on to the ROS console.
  2. In the left-side navigation pane, click Stacks.
  3. In the upper-left corner, select the region where your stack resides 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 Template Import Method to Enter Template Content. Enter the template content and click Next.

    The following code provides an example on the template content:

    {
      "ROSTemplateFormatVersion": "2015-09-01",
      "Parameters": {
        "Count": {
          "Type": "Number",
          "Default": 2
        },
        "ZoneId": {
          "Type": "String"
        },
        "InstanceType": {
          "Type": "String",
          "Default": "ecs.c6.large"
        },
        "Password": {
          "Type": "String",
          "Default": "Abc1****",
          "NoEcho": true
        }
      },
      "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"
          },
          "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"
            }
          }
        },
        "EipBind": {
          "Type": "ALIYUN::VPC::EIPAssociation",
          "Count": {
            "Ref": "Count"
          },
          "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"
            ]
          }
        }
      }
    }

    The following description provides details of the template:

    • Create two EIPs.

      Set the Count parameter to 2. The value of 2 indicates that two EIPs are created by using the Count feature. ROS preprocesses the template to generate two resources named Eip[0] and Eip[1].

      {
          "Eip": {
              "Type": "ALIYUN::VPC::EIP",
              "Count": {
                  "Ref": "Count"
              },
              "Properties": {
                  "Bandwidth": 5
              }
          }
      }
    • Create two ECS instances.

      Set the Count parameter to 2. Set the MaxAmount property of the ALIYUN::ECS::InstanceGroup resource to Count. The value of 2 indicates that two ECS instances are created by using the Count feature.

      {
          "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"
                  }
              }
          }
      }
    • Create two EipBind resources. You can use the ALIYUN::Index pseudo parameter to associate the EIPs to the ECS instances in sequence.

      Set the Count parameter to 2. The value of 2 indicates that two EipBind resources are created by using the Count feature. ROS preprocesses the template to generate two resources named EipBind[0] and EipBind[1]. The ALIYUN::Index pseudo parameter is used in the Count parameter. The Count value is replaced with the index value when ROS preprocesses the template. In the following example, the ECS instances are associated with the EIPs in sequence. The first ECS instance is associated with Eip[0], and the second ECS instance is associated with Eip[1].

      {
          "EipBind": {
              "Type": "ALIYUN::VPC::EIPAssociation",
              "Count": {
                  "Ref": "Count"
              },
              "Properties": {
                  "InstanceId": {
                      "Fn::Select": [
                          {
                              "Ref": "ALIYUN::Index"
                          },
                          {
                              "Fn::GetAtt": [
                                  "Servers",
                                  "InstanceIds"
                              ]
                          }
                      ]
                  },
                  "AllocationId": {
                      "Fn::Select": [
                          {
                              "Ref": "ALIYUN::Index"
                          },
                          {
                              "Ref": "Eip"
                          }
                      ]
                  }
              }
          }
      }
  7. In the Configure Template Parameters step, set Stack Name and Parameters, and click Next.
  8. In the Configure Stack step, set Stack Policy, Rollback on Failure, Timeout Period, Deletion Protection, RAM Role, Tags, and Resource Group, and click Next.
    If no resources are created or updated within the time limit specified by Timeout Period, the system considers the operation to have failed. The system then determines whether to roll back to the state before the resource was created or updated based on the Rollback on Failure configuration.
  9. In the Check and Confirm step, click Create.

Result

After the stack is created, you can click the Resources tab on the stack details page to view the resource information after preprocessing.

You can also click the Template tab on the stack details page to view the template information after preprocessing.

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