1. Help Center
  2. CLI
  3. Developer Guide
  4. SDK
  5. Python
  6. AS Python SDK Demo

AS Python SDK Demo

Creating an AS Group

An AS group is a set of ECSs with the same application scenario configurations. An AS group defines the minimum and maximum numbers of ECSs.

You can create an AS group based on the following code, where vpc_idnetworks, and security_groups are mandatory parameters. Before creating an AS group, you must create a VPC as well as networks and security groups in this VPC.

def test_creat_group(self):
_group = {
        "name": "as_NameTest_modify",
        "scaling_configuration_id": "33b55531-78f8-43c9-8cf5-ffec40bd0c6f",
        "desire_instance_number": 10,
        "min_instance_number": 2,
        "max_instance_number": 10,
        "cool_down_time": 200,
        "lb_listener_id": "114863c227a64255bd25157e0beb783c",
        "available_zones": ["eu-de-02"],
        "health_periodic_audit_method": "NOVA_AUDIT",
        "health_periodic_audit_time": "15",
        "instance_terminate_policy": "OLD_CONFIG_NEW_INSTANCE",
        "vpc_id": "2f2b426c-2072-47a7-babc-c35080fa79d4",
        "networks": [{                                  
          "id": "f80308f4-2608-4ae8-9489-c87720383ae5"
        "notifications": ["EMAIL"],
        "delete_publicip": "true",
        "security_groups": [{
          "id": "57f0a6cd-c427-4e40-a9a2-301ca90893fd" 
      group = self.conn.auto_scaling.create_group(**_group)
      group = self.conn.auto_scaling.get_group(group)

Creating an AS Configuration

An AS configuration defines the configurations for creating instances in an AS group. The AS service automatically adds instances to an AS group based on the AS configuration.

You can create an AS configuration based on the following code. When using an existing ECS flavor as the template to create the AS configuration, specify parameter instance_id. In this case, parameter flavorRefimageRef, and disk do not take effect. If instance_id is not specified, flavorRefimageRef, and disk are mandatory.

def test_creat_config(self):
    instance_config = {
      "flavor_id": "normal1",
      "image_id": "ba391176-5e4c-4c06-8466-349f6b5fc91b",
      "disk": [{
          "size": 40,
          "volume_type": "SATA",
          "disk_type": "SYS"
      "metadata": {
          "key1": "value1",
          "tag": "app"
      "key_name": "KeyPair-0406-as",
      "user_data": "wewfef46565",
      "public_ip": {
          "eip": {
              "ip_type": "5_bgp",
              "bandwidth": {
                  "size": 10,
                  "share_type": "PER",
                  "charging_mode": "traffic"
      config_name = "auto-scaling-config-name"
      config = self.conn.auto_scaling.create_config(config_name, **instance_config)
      config = self.conn.auto_scaling.get_config(config)

Creating an AS Policy

The AS service supports the periodic, scheduled, and alarm policies. The periodic policy can be configured as daily, weekly, or monthly. If you configure the alarm policy, the selected or created alarm policies can be associated with only one AS group.

You can create a scheduled policy (daily) using OpenStack4j based on the following code:

def test_creat_policy_Daily(self):
    as_group_id = "196ddd9c-e1f2-4088-b150-b67ae5ebf746"
    as_policy_name = "as-policy-name"
    _policy = {
          "name": as_policy_name,
          "scaling_policy_action": {
              "operation": "ADD",
              "instance_number": 1
          "cool_down_time": 900,
          "scheduled_policy": {
              "launch_time": "16:00",
              "recurrence_type": "Daily",
              "recurrence_value": None,
              "start_time": "2017-07-14T03:34Z",
              "end_time": "2017-07-27T03:34Z"
          "type": "RECURRENCE",
          "scaling_group_id": as_group_id
    policy = self.conn.auto_scaling.create_policy(**_policy)
    policy = self.conn.auto_scaling.get_policy(policy)

Creating a Lifecycle Hook

The purpose of adding a lifecycle hook to the AS group is to suspend the instance status to Wait (Adding to AS group) or Wait (Removing from AS group) during a scaling action. This status retains until the suspension times out or you manually call back the action.

Code reference:

 #test_create lifecycle_hook
 def test_create_life_cycle_hook(self):
    groupID = "58cbfcab-ebc6-4263-8b71-7d414810488d"
    groupID1 = "936634ad-1a4b-4929-b574-2bc2cbacb608"
    attrs = {
        "lifecycle_hook_name": "test-hook_c",
        "lifecycle_hook_type": "INSTANCE_TERMINATING",
        "default_result": "ABANDON",
        "default_timeout": "",
        "notification_topic_urn": "urn:smn:cn-suzhou2-1:ebac0c927c104c4587687ce375d0b656:as_test",
        "notification_metadata": "xxxxxxxx"
    hook = self.conn.auto_scaling.create_lifecycle_hook(groupID,**attrs)