When a scale-in is triggered, Auto Scaling removes instances from your scaling group based on the specified scale-in policy. This topic describes how to create a custom scale-in policy by using Function Compute.

Prerequisites

Background information

Auto Scaling supports multiple types of instance scale-in policies. When a scale-in is triggered in a scaling group, Auto Scaling removes instances from the scaling group based on conditions such as the instance creation time and scaling configuration. For more information, see Configure a combination policy for removing instances.

If you have high requirements for instance scale-in, you can create custom scale-in policies based on your business requirements. To create a custom scale-in policy in one of the following scenarios, we recommend that you use Function Compute together with Auto Scaling:
  • Scenario 1: Remove instances that have the lowest CPU utilization from your scaling group to reduce business impact from the scale-in.
  • Scenario 2: Remove idle instances from your scaling group based on your business scheduling system to save costs without compromising your business.

Introduction to custom scale-in policy

The following sample code shows a custom scale-in policy that includes information about the instances in a scaling group. When a scale-in is triggered in the scaling group, Auto Scaling removes instances from the scaling group based on the custom scale-in policy.
{
    "ScalingGroupARN": "acs:ess:cn-hangzhou:160998252992****:scalinggroup/asg-bp12zc8b5m4mxhra****",
    "ScalingGroupId": "asg-bp12zc8b5m4mxhra****",
    "CapacityToRemove": [
        {
            "ZoneId": "cn-hangzhou-i",
            "Capacity": 1
        }
    ],
    "Instances": [
        {
            "InstanceId": "i-bp11az18341u****t47v",
            "ZoneId": "cn-hangzhou-i",
            "InstanceType": "ecs.g7.xlarge",
            "ChargeType": "PostPaid"
        },
        {
            "InstanceId": "i-bp11mcx562ak****lcn6",
            "ZoneId": "cn-hangzhou-i",
            "InstanceType": "ecs.g7.xlarge",
            "ChargeType": "PostPaid"
        },
        {
            "InstanceId": "i-bp11mcx562aky****lcn7",
            "ZoneId": "cn-hangzhou-i",
            "InstanceType": "ecs.g7.xlarge",
            "ChargeType": "PostPaid"
        }
    ],
    "AdjustmentType": "SystemScaleIn"
}
The following table describes the parameters that are used in the preceding sample code.
ParameterDescription
ScalingGroupARNThe Alibaba Cloud Resource Name (ARN) of the scaling group, which is used to identify the scaling group.
ScalingGroupIdThe ID of the scaling group.
CapacityToRemoveThe recommended number of instances that can be removed from your scaling group in each zone. The value of this parameter is only for reference.

The CapacityToRemove parameter specifies the expected number of instances that are removed from your scaling group during the scale-in. To ensure that the actual number of instances that are removed from your scaling group meets your business requirements, the number of removed instances that are returned in response parameters must be greater than or equal to the value of this parameter.

InstancesThe information about the instances that can be removed from the scaling group. The information includes the ID, zone, instance type, and billing method of each instance.
AdjustmentTypeThe adjustment mode of the scaling rule.

Step 1: Create a function in Function Compute

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services & Functions.
  3. In the top navigation bar, select a region.
  4. On the Services page, click Create Service.
  5. In the Create Service panel, configure parameters and click OK. The following table describes the parameters.
    Note If you want to access a virtual private cloud (VPC) or the Internet, click Show Advanced Options to enable the features that you require. For more information, see Create a service.
    ParameterDescriptionExample
    NameEnter a name for the Function Compute service.

    The name must be 1 to 128 characters in length, and can contain letters, digits, underscores (_), and hyphens (-). It cannot start with a digit or a hyphen (-).

    ess_custom_terminate_policy
    DescriptionEnter a description for the Function Compute service. -
    LoggingSpecify whether to enable the logging feature. Valid values:
    • Enable: Function Compute sends function execution logs to Log Service for persistent storage. You can debug code, troubleshoot issues, and analyze data based on the logs.
    • Disable: You cannot use Log Service to store or query function execution logs.
    Enable
  6. In the left-side navigation pane, click Functions, and then click Create Function.
  7. On the Create Function page, configure parameters based on your business requirements and click Create.
    Figure - Create a function
    The following table describes the parameters that are used in this example.
    Note For parameters that are not described in the following table, you can use the default settings. For more information, see Create a function.
    ParameterDescriptionExample
    Select a method to create the functionValid values:
    • Use Built-in Runtime
    • Use Custom Runtime
    • Use Container Image
    Use Built-in Runtime
    Basic SettingsFunction NameEnter a name for the function.

    The name must be 1 to 64 characters in length, and can contain letters, digits, underscores (_), and hyphens (-). The name cannot start with a digit or a hyphen (-).

    ess_custom_terminate_policy_function
    Request TypeValid values:
    • Event Handler: The function processes event requests.
    • HTTP Handler: The function processes HTTP requests or WebSocket requests.
    Event Handler
    CodeRuntimeSelect a runtime that is supported by Function Compute. Python 3.9
    Code Upload MethodSelect a method to upload the function code to Function Compute. Valid values:
    • Use Sample Code
    • Upload ZIP
    • Upload Folder
    • Upload OSS

    Below the Use Sample Code tab, select Hello, world!.

    Hello, world!
  8. On the Code tab, copy the following sample code to the index.py file and click Deploy.
    Note On the details page of the function, click OK in the Web IDE Usage Notes dialog box.
    In the function code, you can define specific conditions to remove instances based on your business requirements. For example, you can specify instance vCPU utilization or business scheduling system as the instance removal condition. In this example, the specified order of instance IDs is used as the instance removal condition.
    # -*- coding: utf-8 -*-
    import logging
    import json
    
    def handler(event, context):
        evt = json.loads(event)
        logger = logging.getLogger()
        logger.info(evt)
        removeCount = 0
        if "CapacityToRemove" in evt:
            for cp in evt["CapacityToRemove"]:
                logger.info(cp["Capacity"])
                removeCount += int(cp["Capacity"])
        instances_to_terminate = []
        if "Instances" in evt:
            for i in evt["Instances"]:
                instances_to_terminate.append(i["InstanceId"])
        response = {
            'InstanceIds': instances_to_terminate[:removeCount]
        }
        logger.info(response)
        return response
  9. On the Code tab, click the Move icon icon to the right of Test Function. In the drop-down list, select Configure Test Parameters, enter the same code in Introduction to custom scale-in policy, and then click OK.
    Figure - Configure Test Parameters
  10. On the Code tab, click Test Function.
    After the function is executed, you can click the Response tab to view the IDs of the instances that can be removed based on the custom scale-in policy. Sample code:
    {
        "InstanceIds": [
            "i-bp11az18341u****t47v"
        ]
    }
    The InstanceIds parameter specifies the ID of the instances that can be removed from the scaling group based on the custom scale-in policy. Auto Scaling removes instances from the scaling group based on the value of the InstanceIds parameter and the scale-in rule. Examples:
    • If you set the CapacityToRemove parameter to 1, Auto Scaling removes one instance that is specified by the InstanceIds parameter from the scaling group.
    • If you leave the InstanceIds parameter empty, Auto Scaling removes no instance from the scaling group.
  11. In the upper part of the page that appears, click Publish Version, and then click OK.

Step 2: Configure a scaling group for the custom scale-in policy

  1. Log on to the Auto Scaling console.
  2. Create a scaling group.
    For more information, see Create scaling groups. When you create the scaling group, set the Scale-in Policy parameter to Custom Policy, as shown in the following figure.Figure - Create a scaling group
    The following table describes the parameters of the scaling group.
    ParameterDescriptionExample
    TypeThe type of instances that provide computing power in the scaling group. This parameter also specifies the type of instances that Auto Scaling adds to or removes from the scaling group during a scaling activity. Valid values:
    • ECS: Elastic Compute Service (ECS) instance
    • ECI: elastic container instance
    ECS
    Instance Configuration SourceAuto Scaling creates instances based on the value of the Instance Configuration Source parameter. Valid values:
    • Launch Templates
    • Select Existing Instance
    • Create from Scratch
    Create from Scratch
    Scale-In PolicyIf you want to remove instances from the scaling group and you have multiple options, configure the Scale-In Policy parameter. The Scale-In Policy parameter is supported only if you set the Type parameter to ECS.
    Note If multiple instances still meet the scale-in condition after Auto Scaling removes a specific number of instances from the scaling group based on the scale-in policy, Auto Scaling continues to remove an instance in a random manner.
    Valid values:
    • Earliest Instance Created Using Scaling Configuration
    • Earliest Created Instance
    • Most Recent Created Instance
    • Custom Policy
    Custom Policy
    • Set the Service parameter to ess_custom_terminate_policy.
    • Set the Version parameter to LATEST.
    • Set the Function parameter to ess_custom_terminate_policy_function.
    Maximum Number of InstancesIf the number of instances in the scaling group is greater than the value of this parameter, Auto Scaling removes instances until the number of instances in the scaling group does not exceed the maximum number. 3
    Note In this example, this parameter is set to 3 to facilitate the subsequent test.
    VPCSelect an existing VPC. vpc-bp1l5b6zsvw30qb6t****
    vSwitchEach vSwitch resides in a zone. To deploy instances across multiple zones, specify multiple vSwitches in different zones. vsw-bp1qo7s91cbch5i4l****
  3. Create a scaling configuration.
  4. Enable the scaling group.
    For more information, see Enable or disable scaling groups.
  5. Create a scaling rule.
    In this example, two simple scaling rules are created. For more information, see Create a scaling rule.
    • Create a scale-out rule. In the Create Scaling Rule dialog box, set the Operation parameter to Change To 3 Instances.
    • Create a scale-in rule. In the Create Scaling Rule dialog box, set the Operation parameter to Remove 1 Instances.
  6. Execute the scaling rules.
    In this step, execute the preceding scaling rules. For more information, see Execute a scaling rule.
    On the Scaling Activities tab, you can view the execution status of the scaling rules.

Check whether the custom scale-in policy takes effect

  1. Log on to the Function Compute console.
  2. In the left-side navigation pane, click Services & Functions.
  3. In the top navigation bar, select a region.
  4. On the Services tab, find the ess_custom_terminate_policy service that is created in Step 1: Create a function in Function Compute and click Functions in the Actions column.
  5. Click Functions in the left-side navigation pane, find the ess_custom_terminate_policy_function function that is created in Step 1: Create a function in Function Compute, and then click the name of the function.
  6. On the details page of the function, click the Logs tab.
  7. Click the Function Logs tab to view the records of function invocations that are triggered by scale-ins.
  8. Query the request parameters and response parameters that are logged.
    Sample code:
    1-640ad88d-b59ed1af57161e4b363cb7c9 [INFO] {'ScalingGroupARN': 'acs:ess:cn-hangzhou:160998252992****:scalinggroup/asg-bp12zc8b5m4mxhra****', 'ScalingGroupId': 'asg-bp12zc8b5m4mxhra****', 'CapacityToRemove': [{'ZoneId': 'cn-hangzhou-i', 'Capacity': 1}], 'Instances': [{'InstanceId': 'i-bp11az18341u****t47v', 'ZoneId': 'cn-hangzhou-b', 'InstanceType': 'ecs.g7.xlarge', 'ChargeType': 'PostPaid'}, {'InstanceId': 'i-bp11mcx562ak****lcn6', 'ZoneId': 'cn-hangzhou-i', 'InstanceType': 'ecs.g7.xlarge', 'ChargeType': 'PostPaid'}, {'InstanceId': 'i-bp11mcx562aky****lcn7', 'ZoneId': 'cn-hangzhou-i', 'InstanceType': 'ecs.g7.xlarge', 'ChargeType': 'PostPaid'}], 'AdjustmentType': 'SystemScaleIn'}
    1-640ad88d-b59ed1af57161e4b363cb7c9 [INFO] 1
    1-640ad88d-b59ed1af57161e4b363cb7c9 [INFO] {'InstanceIds': ['i-bp11az18341u****t47v']}
  9. View the details of the removed instances.
    For more information, see View the details of a scaling activity. Figure - Details of the scale-in
    In the response parameters of the function log, the instance {'InstanceIds': ['i-bp11az18341u****t47v']} is removed. This matches the Instances "i-bp11az18341u****t47v" are deleted message that appeared on the scaling activity details page. In this case, the custom scale-in policy works as expected.