This topic describes how to use the Alibaba Cloud ESS SDK to create a multi-zone scaling group in either Java or Python.

Prerequisites

Before performing the instructions provided in this tutorial, make sure that you have an Alibaba Cloud account. To create an Alibaba Cloud account, click Create a new Alibaba Cloud account.

Background information

The network type of a scaling group can be either Virtual Private Cloud (VPC) or classic network. When creating a VPC-connected scaling group, you must configure a VSwitch for the scaling group. After the scaling group is created, all ECS instances that are scaled out through the group use this VSwitch.

Auto Scaling allowed a VPC-connected scaling group to have only one VSwitch configured. A VSwitch belongs to only one zone. If ECS instances cannot be created in the zone where the VSwitch resides due to reasons such as insufficient inventory, the scaling configuration, scaling rules, and monitoring tasks of the scaling group become invalid.

To address the preceding issue and improve the availability of the scaling group, the VSwitchIds.N parameter has been added to allow you to create multi-zone scaling groups. When creating a scaling group, you can use the VSwitchIds.N parameter to configure multiple VSwitches for the scaling group. When ECS instances cannot be created in the zone where a VSwitch resides, Auto Scaling automatically switches to the zone where a different VSwitch resides. When you use this parameter, note that:

  • If the VSwitchIds.N parameter is specified, the VSwitchId parameter is ignored.
  • The VSwitchIds.N parameter allows you to specify up to five VSwitches within a VPC in multiple zones when you create a scaling group. Valid values of N are integers from 1 to 5.
  • VSwitches specified in the VSwitchIds.N parameter must be within the same VPC.
  • In the VSwitchIds.N parameter, N indicates the priority of each VSwitch. The VSwitch with N set to 1 has the highest priority to create an ECS instance. The greater the N value, the lower the priority.
  • When an ECS instance cannot be created in the zone where the VSwitch with the highest priority resides, the instance will be created in the zone where the VSwitch with the second highest priority resides. We recommend that you specify VSwitches in different zones of the same region to avoid failing to create ECS instances due to insufficient inventory in a single zone and improve the availability of scaling groups.

Use the Alibaba Cloud ESS SDK for Java to create a multi-zone scaling group

  1. Install the Alibaba Cloud ESS SDK for Java.
    Download the aliyun-java-sdk-core and aliyun-java-sdk-ess dependency libraries. You can visit Maven Central to search for and download the JAR packages accordingly. The JAR package version for aliyun-java-sdk-ess must be V2.1.3 or later and that for aliyun-java-sdk-core must be the latest.
    You can also use Apache Maven to manage the dependency libraries of your Java projects by adding the following dependencies to the pom.xml file.
    <dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-ess</artifactId>
    <version>2.1.3</version>
    </dependency>
    <dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>3.5.0</version>
    </dependency> 
  2. Use the Java SDK to create a multi-zone scaling group.
    After importing the Alibaba Cloud ESS SDK for Java to a Java project, you can use the SDK code to create a multi-zone scaling group. The sample code is as follows:
    public class EssSdkDemo {
    public static final String       REGION_ID          = "cn-hangzhou";
    public static final String       AK                 = "ak";
    public static final String       AKS                = "aks";
    public static final Integer      MAX_SIZE           = 10;
    public static final Integer      MIN_SIZE           = 1;
    public static final String       SCALING_GROUP_NAME = "TestScalingGroup";
    
    // The list of VSwitches. The priority of the VSwitch decreases from the beginning to the end of the list, with the first VSwitch having the highest priority.
    public static final String[]     vswitchIdArray     = { "vsw-id1", "vsw-id2", "vsw-id3", "vsw-id4", "vsw-id5" };
    public static final List<String> vswitchIds         = Arrays.asList(vswitchIdArray);
    public static void main(String[] args) throws Exception {
        IClientProfile clientProfile = DefaultProfile.getProfile(REGION_ID, AK, AKS);
        IAcsClient client = new DefaultAcsClient(clientProfile);
        createScalingGroup(client);
    }
    
    /**
     * Create a multi-zone scaling group.
     * @param client
     * @return
     * @throws Exception
    */
    public static String createScalingGroup(IAcsClient client) throws Exception {
        CreateScalingGroupRequest request = new CreateScalingGroupRequest();
        request.setRegionId("cn-beijing");
        request.setMaxSize(MAX_SIZE);
        request.setMinSize(MIN_SIZE);
        request.setScalingGroupName(SCALING_GROUP_NAME);
        request.setVSwitchIds(vswitchIds);
        CreateScalingGroupResponse response = client.getAcsResponse(request);
        return response.getScalingGroupId();
    }
    }          

    In the preceding code, the priority of the VSwitch decreases from the beginning to the end of the list. The VSwitch at the top of the list has the highest priority.

Use the Alibaba Cloud ESS SDK for Python to create a multi-zone scaling group

  1. Install the Alibaba Cloud ESS SDK for Python.
    To install the Alibaba Cloud ESS SDK for Python, you must download and install the aliyun-python-sdk-ess and aliyun-python-sdk-core dependency libraries. We recommend that you use pip to install Python dependency libraries. For more information about how to install pip, visit Installation-pip. After pip is installed, run the pip install aliyun-python-sdk-ess==2.1.3 pip install aliyun-python-sdk-core==3.5.0 command to install the dependency libraries.
  2. Use the Python SDK to create a multi-zone scaling group.
    After installing the dependency libraries of the Alibaba Cloud ESS SDK for Python, you can use the SDK code to create a multi-zone scaling group. The sample code is as follows:
    #  coding=utf-8
    import json
    import logging
    from aliyunsdkcore import client
    from aliyunsdkess.request.v20140828.CreateScalingGroupRequest import CreateScalingGroupRequest
    logging.basicConfig(level=logging.INFO,
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S')
    # Replace the following ak and aks values with your own AccessKey ID and secret.
    ak = 'ak'
    aks = 'aks'
    scaling_group_name = 'ScalingGroupTest'
    max_size = 10
    min_size = 1
    vswitch_ids = ["vsw-id1", "vsw-id2", "vsw-id3", "vsw-id4", "vsw-id5"]
    region_id = 'cn-beijing'
    clt = client.AcsClient(ak, aks, region_id)   
    def _create_scaling_group():
    request = CreateScalingGroupRequest()
    request.set_ScalingGroupName(scaling_group_name)
    request.set_MaxSize(max_size)
    request.set_MinSize(min_size)
    request.set_VSwitchIds(vswitch_ids)
    response = _send_request(request)
    return response.get('ScalingGroupId')
    def _send_request(request):
    request.set_accept_format('json')
    try:
        response_str = clt.do_action(request)
        logging.info(response_str)
        response_detail = json.loads(response_str)
        return response_detail
    except Exception as e:
        logging.error(e)
    if __name__ == '__main__':
    scaling_group_id = _create_scaling_group()
    print 'Scaling group created successfully. Scaling group ID:' + str (scaling_group_id)

    In the preceding code, the priority of the VSwitch decreases from the beginning to the end of the list. The VSwitch at the top of the list has the highest priority.