This topic describes how to create a Server Load Balancer (SLB) instance and then delete it by using the Alibaba Cloud SDK for Python.

Background information

In this example, an SLB instance named SLB1 is created. Region of the SLB instance: China North 3 (Zhangjiakou). Primary zone: cn-zhangjiakou-a. Backup zone: cn-zhangjiakou-b. Billing method: Pay-As-You-Go. Specification: slb.s1.small. Peak bandwidth of the listener: 1 Mbit/s. Default values are used for other parameters. After the SLB instance is created, the instance is deleted.

Procedure

  1. From the downloaded SDK package, open the folder $aliyun-openapi-python-sdk-examples\sdk_examples\examples\slb.
  2. Open slb_quick_start.py by using an editor. Set the user authentication parameter ACS_CLIENT and other parameters as needed, save the file, and exit.
    Note Because the identity of a user must be authenticated by the SDK, you can also set the AcsClient parameter in the constants file and call the constants file when needed.
    #encoding=utf-8
    """
    Create an SLB instance->Delete the created SLB instance
    """
    import sys
    import json
    import uuid
    
    
    #Call the AcsClient parameter for authentication.
    from aliyunsdkcore.client import AcsClient
    #Use the exception handling module of the Alibaba Cloud SDK.
    from aliyunsdkcore.acs_exception.exceptions import ServerException, ClientException
    #Call the API used to create an SLB instance.   
    from aliyunsdkslb.request.v20140515 import CreateLoadBalancerRequest
    #Call the API used to delete an SLB instance.  
    from aliyunsdkslb.request.v20140515 import DeleteLoadBalancerRequest
    #Handle exceptions.
    from sdk_lib.exception import ExceptionHandler
    #Redirect command line output.  
    from sdk_lib.common_util import CommonUtil
    
    #Set the user authentication parameter.
    ACS_CLIENT = AcsClient(
        'your-access-key-id',     #your-access-key-id
        'your-access-key-secret',   #your-access-key-secret
        'cn-zhangjiakou',     #your-region-id
    )
    
    def create_load_balancer(params):
        '''
        create_load_balancer: Create an SLB instance.
        For more information about APIs, go to https://www.alibabacloud.com/help/doc-detail/27577.htm?spm=a2c63.p38356.b99.137.1aad1093fJoHIT.
        '''
        try:
        #Set the API parameters for creating an SLB instance.
        request = CreateLoadBalancerRequest.CreateLoadBalancerRequest() 
        request.set_MasterZoneId(params["master_zone_id"]) 
        request.set_SlaveZoneId(params["slave_zone_id"]) 
        request.set_LoadBalancerName(params["load_balancer_name"]) 
        request.set_PayType(params["pay_balancer_type"]) 
        request.set_LoadBalancerSpec(params["load_balancer_spec"]) 
        request.set_Bandwidth(params["balancer_listener_bandwith"])
            request.set_ClientToken(str(uuid.uuid1()))
        #Send the request and receive the response.
        response = ACS_CLIENT.do_action_with_exception(request)
        response_json = json.loads(response)
        #Return results.    
        return response_json
        except ServerException as e:
            ExceptionHandler.server_exception(e)
        except ClientException as e:
            ExceptionHandler.client_exception(e)
    
    def delete_load_balancer(load_balancer_id):
        '''
        delete_load_balancer: Delete the SLB instance.
        For more information about APIs, go to https://www.alibabacloud.com/help/doc-detail/27579.htm?spm=a2c63.p38356.b99.138.50d5108biWok5Y.
        '''
        try:
            request = DeleteLoadBalancerRequest.DeleteLoadBalancerRequest()
            #The ID of the SLB instance.
            request.set_LoadBalancerId(load_balancer_id)
            response = ACS_CLIENT.do_action_with_exception(request)
            response_json = json.loads(response)
            return response_json
        except ServerException as e:
            ExceptionHandler.server_exception(e)
        except ClientException as e:
            ExceptionHandler.client_exception(e)
    
    def main():
        #Set parameters for the SLB instance.
        params = {} 
        #Set the primary zone of the SLB instance to cn-zhangjiakou-a. 
        params["master_zone_id"] = "cn-zhangjiakou-a" 
        #Set the backup zone of the SLB instance to cn-zhangjiakou-b. 
        params["slave_zone_id"] = "cn-zhangjiakou-b" 
        #Set the name of the SLB instance to SLB1.  
        params["load_balancer_name"] = "SLB1"
        #Set the billing method of the SLB instance to Pay-As-You-Go.
        params["pay_balancer_type"] = "PayOnDemand"
        #Set the specification of the SLB instance to slb.s1.small.
        params["load_balancer_spec"] = "slb.s1.small"
        #Set the peak bandwidth of the listener to 1 Mbit/s.
        params["balancer_listener_bandwith"] = "1"
    
        #Create an SLB instance.
        #Obtain the returned value of create_load_balancer. "load_balancer_json" is the returned value in json format.
        load_balancer_json = create_load_balancer(params)
        #Print the returned value of load_balancer_json, where "create_load_balancer" is the name of the json string.
        CommonUtil.log("create_load_balancer", load_balancer_json)
    
        #Delete the SLB instance.
        #Delete the SLB instance that the returned LoadBalancerId indicates.
        load_balancer_json = delete_load_balancer(load_balancer_json["LoadBalancerId"])
        #Print the returned value of load_balancer_json.
        CommonUtil.log("delete_load_balancer", load_balancer_json)
    
    
    if __name__ == "__main__":
        sys.exit(main())
                        
  3. Go to the slb_quick_start.py directory and run the following command to create and then delete an SLB instance.
    python slb_quick_start.py
    If the following output is displayed, an SLB instance has been created and then deleted:
    
    ---------------------------create_load_balancer---------------------------
    {
      "VpcId": "",
      "AddressIPVersion": "ipv4",
      "LoadBalancerName": "SLB1",
      "ResourceGroupId": "rg-acfmxxxxxxxxxy",
      "VSwitchId": "",
      "RequestId": "070A9361-EA2B-4A4E-91E8-F70C1E47866A",
      "Address": "172.16.XX.XX",
      "NetworkType": "classic",
      "LoadBalancerId": "lb-8vbninnfxxxxxxxxxxxxx"
    }
    
    ---------------------------delete_load_balancer---------------------------
    {
      "RequestId": "4825E5BB-5131-4A1E-AF67-0EF3E8E5B323"
    }