This topic describes how to use Auto Scaling SDKs for Java and Python to create multi-zone scaling groups.

Prerequisites

  • An Alibaba Cloud account is created. To create an Alibaba Cloud account, go to the account registration page.

  • The Alibaba Cloud Credentials tool is configured.

    You can configure the Alibaba Cloud Credentials tool by specifying the following environment variables: ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. This allows you to access Alibaba Cloud OpenAPI Explorer without using a hard-coded plaintext AccessKey pair. For more information, see Configure environment variables.

Background information

If you create a scaling group of the VPC network type, you must specify at least one vSwitch for the scaling group. After you create the scaling group, the Elastic Compute Service (ECS) instances that are created in the scaling group use the specified vSwitch. Originally, Auto Scaling allows you to specify only one vSwitch for a scaling group of the VPC network type. Because a vSwitch belongs to only one zone, the scaling group is bound to a specific zone. In this case, if the zone does not have sufficient instance types, ECS instances cannot be created in the scaling group. In addition, the scaling configuration, scaling rules, and event-triggered tasks of the scaling group also become ineffective.

To prevent scale-out failures that are caused by insufficient instance types in a single zone and improve the availability of scaling groups of the VPC network type, Auto Scaling provides the VSwitchIds.N parameter. When you create a scaling group, you can use this parameter to specify multiple vSwitches for the scaling group. If a scale-out fails in one zone, Auto Scaling triggers a scale-out in another zone.

Usage notes

Before you use the VSwitchIds.N parameter, take note of the following usage notes:

  • If you use the VSwitchIds.N parameter, Auto Scaling ignores the VSwitchId parameter.
  • You can use the VSwitchIds.N parameter to specify up to five vSwitches for a scaling group. N in the VSwitchIds.N parameter specifies the vSwitch priority. A smaller value of N specifies a higher priority.

    If a scale-out fails in the zone where the vSwitch with the highest priority resides, Auto Scaling triggers a scale-out in the zone where the vSwitch with the next highest priority resides. We recommend that you use the VSwitchIds.N parameter to specify vSwitches of different zones. This helps prevent creation failures of ECS instances in a single zone due to insufficient instance types.

  • The vSwitches that are specified by the VSwitchIds.N parameter must belong to the same VPC.

Use Auto Scaling SDK for Java to create a multi-zone scaling group

  1. Import Auto Scaling SDK for Java.
    If you use Maven to manage Java dependencies, you can add the following dependencies to the pom.xml file of your project:
    <dependency>
      <groupId>com.aliyun</groupId>
      <artifactId>tea-openapi</artifactId>
      <version>0.2.8</version>
    </dependency>
     <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>ess20220222</artifactId>
          <version>1.0.5</version>
        </dependency>
        <dependency>
          <groupId>com.aliyun</groupId>
          <artifactId>credentials-java</artifactId>
          <version>0.2.11</version>
        </dependency>
  2. Use Auto Scaling SDK for Java to create a multi-zone scaling group.
    After you import Auto Scaling SDK for Java into your Java project, you can use Auto Scaling SDK for Java to compile code to create a multi-zone scaling group. Sample code:
    import com.aliyun.teaopenapi.models.Config;
    import java.util.Arrays;
    import java.util.List;
    
    public class EssSdkDemo {
        public static final String       REGION_ID          = "cn-hangzhou";
        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 vSwitches are listed in descending order of priority. The first vSwitch has the highest priority. 
        public static final String[]     vswitchIdArray     = { "vsw-id1", "vsw-id2" };
        public static final List<String> vswitchIds         = Arrays.asList(vswitchIdArray);
    
        public static void main(String[] args) throws Exception {
            com.aliyun.credentials.Client credentialClient = new com.aliyun.credentials.Client();
            com.aliyun.teaopenapi.models.Config config = new Config();
            config.setCredential(credentialClient);
            config.setEndpoint("ess.aliyuncs.com");
            com.aliyun.ess20220222.Client client = new com.aliyun.ess20220222.Client(config);
            createScalingGroup(client);
    
        }
    
        public static String createScalingGroup(com.aliyun.ess20220222.Client client) throws Exception {
            com.aliyun.ess20220222.models.CreateScalingGroupRequest request = new com.aliyun.ess20220222.models.CreateScalingGroupRequest();
            request.setRegionId(REGION_ID);
            request.setMaxSize(MAX_SIZE);
            request.setMinSize(MIN_SIZE);
            request.setScalingGroupName(SCALING_GROUP_NAME);
            request.setVSwitchIds(vswitchIds);
            com.aliyun.teautil.models.RuntimeOptions runtime = new com.aliyun.teautil.models.RuntimeOptions();
            com.aliyun.ess20220222.models.CreateScalingGroupResponse scalingGroupWithOptions = client.createScalingGroupWithOptions(request, runtime);
            return scalingGroupWithOptions.getBody().toMap().toString();
        }
    
    }       

    In the preceding code, the vSwitches are listed in descending order of priority. The first vSwitch has the highest priority.

Use Auto Scaling SDK for Python to create a multi-zone scaling group

  1. Install Auto Scaling SDK for Python.
    Run the following commands to install the dependencies:
    pip install alibabacloud_credentials
    pip install alibabacloud_ess20220222==1.0.6
  2. Use Auto Scaling SDK for Python to create a multi-zone scaling group.
    After you install the dependency libraries, you can use Auto Scaling SDK for Python to compile code to create a multi-zone scaling group. Sample code:
    #!/usr/bin/env python
    #coding=utf-8
    
    from aliyunsdkcore.client import AcsClient
    from aliyunsdkcore.request import CommonRequest
    from aliyunsdkcore.auth.credentials import AccessKeyCredential
    from alibabacloud_credentials.client import Client as CredClient
    cred=CredClient()
    accesskeyid = cred.get_access_key_id()
    accesskeysecret = cred.get_access_key_secret()
    credentials = AccessKeyCredential(accesskeyid, accesskeysecret)
    client = AcsClient(region_id='cn-hangzhou', credential=credentials)
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain('ess.aliyuncs.com')
    request.set_method('POST')
    request.set_protocol_type('https') # https | http
    request.set_version('2022-02-22')
    request.set_action_name('CreateScalingGroup')
    request.add_query_param('ScalingGroupName', "testname")
    request.add_query_param('MinSize', "1")
    request.add_query_param('MaxSize', "1")
    request.add_query_param('VSwitchIds.1', "vsw-1")
    request.add_query_param('VSwitchIds.2', "vsw-2")
    request.add_query_param('RegionId', "cn-hangzhou")
    
    response = client.do_action(request)
    # python2:  print(response)
    print(str(response, encoding = 'utf-8'))

    In the preceding code, Python 3.9 is used. The vSwitches are listed in descending order of priority. The first vSwitch has the highest priority.