All Products
Search
Document Center

Auto Scaling:Use Function Compute to create custom scale-in policies for ECS instances

Last Updated:Oct 23, 2024

If you have specific requirements on Elastic Compute Service (ECS) instances to scale in (for example, you want to preferentially scale in ECS instances that have the lowest CPU utilization), you can use Function Compute to create custom scale-in policies. During scale-in events, Auto Scaling automatically removes the ECS instances that meet the conditions in custom scale-in policies from scaling groups.

Background information

Auto Scaling supports multiple types of instance scale-in policies. When a scale-in event 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 Combine scaling policies and scale-in policies.

If your business has 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: Prioritize scale-in of instances that have the lowest CPU load, to minimize business impact.

  • Scenario 2: Preferentially decrease the number of idle instances based on your scheduling system to ensure uninterrupted services while optimizing resource utilization and minimizing costs.

Prerequisites

  • Function Compute is activated, and the relevant authorization is complete. For more information, see Step 1: Activate Function Compute.

  • Simple Log Service is activated, and the relevant authorization is complete. For more information, see Getting Started.

Procedure

Step 1: Create a function

Alibaba Cloud provides Function Compute 2.0 and Function Compute 3.0. This topic shows how to use Function Compute 2.0 to create a custom scale-in policy.

  1. Log on to the Function Compute console.

    If Function Compute 3.0 is displayed, you can click Back to Function Compute 2.0.

    函数计算2.0.jpg

  2. In the left-side navigation pane, click Services & Functions.

  3. In the top navigation bar, select a region.

  4. Create a service.

    1. On the Services page, click Create Service.

    2. In the Create Service panel, configure the Name, Description, and Logging parameters based on your business requirements and click OK.

      The following table describes the parameter settings used in this example. For parameters that are not described in this table, use the default values. For more information, see Create a service.

      Parameter

      Description

      Description

      Name

      test-service

      Enter a name for the Function Compute service. The name must follow the format requirements displayed on the UI.

      Description

      test

      Enter a description for the Function Compute service.

      Logging

      Enable

      Specifies whether to enable the logging feature. If you enable the logging feature, Function Compute sends function execution logs to Simple Log Service for persistent storage. You can debug code, analyze failures, and analyze data based on the logs.

  5. Create a function.

    1. Click the name of the service that is created to go to the service details page. Then, click Functions in the left-side navigation pane.

    2. Click Create Function.

    3. On the Create Function page, configure parameters to complete function creation and click Create.

      In this example, the Use Built-in Runtime method is selected to create a function. The following table describes the parameter settings used in this example. For parameters that are not described in this table, use the default values. For more information, see Create a function.

      Parameter

      Example

      Description

      Basic Settings

      Function Name

      test-fc

      Enter a name for the function. The name must follow the format requirements displayed on the UI.

      Handler Type

      Event Handler

      Valid values:

      • Event Handler: The function processes event requests.

      • HTTP Handler: The function processes HTTP or WebSocket requests.

      Code

      Runtime

      Python 3.9

      Select the runtime environment of the code.

      Code Upload Method

      Hello, world!

      Select a method to upload the function code to Function Compute.

  6. Compile and deploy the function code.

    1. Click the function name to go to the function details page.

    2. On the Code tab, create a file named index.py in the code editor and click Deploy.

      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 number of ECS instances to scale in (Capacity) and specified order of instance IDs (InstanceId) are used as the scale-in conditions. Sample code:

      # -*- 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
      
  7. (Optional) Test the function.

    1. On the Code tab, click the 图标 icon next to Test Function and select Configure Test Parameters from the drop-down list.

    2. Enter the test code and click OK.

      In this example, the following code is tested. In the code, ScalingGroupId specifies the ID of the scaling group that you want to manage. CapacityToRemove specifies that Auto Scaling removes one ECS instance from the cn-beijing-g zone. Instances specifies the IDs of the ECS instances that can be scaled in.

      {
      	"ScalingGroupARN": "acs:ess:cn-beijing:160998252992****.scalinggroup/asg-2zei8mzn72rb115k****",
      	"ScalingGroupId": "asg-2zei8mzn72rb115k****",
      	"CapacityToRemove": [{
      		"ZoneId": "cn-beijing-g",
      		"Capacity": 1
      	}],
      	"Instances": [{
      			"InstanceId": "i-2zeinb37ovdsx6l0****",
      			"ZoneId": "cn-beijing-g",
      			"InstanceType": "ecs.g7.xlarge",
      			"ChargeType": "PostPaid"
      		},
      		{
      			"InstanceId": "i-2zeinb37ovdsx6l0****",
      			"ZoneId": "cn-beijing-g",
      			"InstanceType": "ecs.g7.xlarge",
      			"ChargeType": "PostPaid"
      		},
      		{
      			"InstanceId": "i-2zeinb37ovdsx6l0****",
      			"ZoneId": "cn-beijing-g",
      			"InstanceType": "ecs.g7.xlarge",
      			"ChargeType": "PostPaid"
      		}
      	],
      	"AdjustmentType": "SystemScaleIn"
      }
    3. Click Test Function on the Code tab.

    4. On the Result tab, view the test results.

      If the output is similar to the following code, Auto Scaling removes the ECS instance whose ID ranks first during a scale-in event.

      {
          "InstanceIds": [
              "i-2zeinb37ovdsx6l0****"
          ]
      }
  8. In the upper part of the page that appears, click Publish Version, and then click OK.

Step 2: Configure an instance removal policy for the scaling group

The following section describes how to configure an instance removal policy when you create a scaling group. If you have an existing scaling group, you can directly modify the instance removal policy based on your business requirements.

  1. Log on to the Auto Scaling console.

  2. Create a scaling group.

    1. On the Scaling Groups page, click Create Scaling Group.

    2. Configure parameters to complete the group creation and click Create.

      The following table describes the parameter settings used in this example. For parameters that are not described in this table, use the default values. For more information, see Manage scaling groups.

      实例移出策略.png

      Parameter

      Example

      Description

      Scaling Group Name

      test

      Enter a name for the scaling group. The name must follow the format requirements displayed on the UI.

      Type

      ECS

      Specify the type of instances managed by the scaling group.

      In the example, this parameter is set to ECS.

      Instance Configuration Source

      Create from Scratch

      Select a template based on which Auto Scaling can create ECS instances.

      In this example, this parameter is set to Create from Scratch. This value indicates that no template is specified when you create the scaling group. After you create the scaling group, create a scaling configuration.

      Scale-In Policy

      Custom Policy

      • Service: test-service

      • Version: LATEST

      • Function: test-fc

      Specify an instance scale-in policy.

      In this example, select Custom Policy and then select the created function.

      Minimum Number of Instances

      0

      Specify the minimum instance limit. If the number of instances in the scaling group is less than the value of this parameter, Auto Scaling adds ECS instances to the scaling group until the number of ECS instances in the scaling group reaches the minimum limit.

      Maximum Number of Instances

      5

      Specify the maximum instance limit. If the number of instances in the scaling group is greater than the value of this parameter, Auto Scaling removes ECS instances from the scaling group until the number of ECS instances in the scaling group decreases to the maximum limit.

      Enable Expected Number of Instances and Expected Number of Instances

      Enable and 3

      Enable the Expected Number of Instances feature and specify an expected number of instances. Auto Scaling automatically maintains the expected number of ECS instances in the scaling group.

  3. Create a scaling configuration. For more information, see Create a scaling configuration of the ECS type.

  4. Enable the scaling group. For information about how to enable a scaling group, see Enable or disable scaling groups.

    In this example, the value of Expected Number of Instances is 3. Therefore, Auto Scaling automatically creates three ECS instances in the scaling group.

Step 3: Check the scale-in effect

  1. Trigger a scale-in event and check information about the scaled-in ECS instance.

    1. Modify the Expected Number of Instances parameter to trigger a scale-in event. For more information, see View or modify scaling groups.

      Important

      You can use methods such as scheduled tasks, event-triggered tasks, and manual execution of scaling rules to trigger a scale-in event. However, during a scale-in event triggered by modifying the Minimum Number of Instances and Maximum Number of Instances parameters, the custom scale-in policy created by using Function Compute cannot take effect.

      In this example, the value of the Expected Number of Instances parameter is changed to 2 to check the custom scale-in effect. Because the scaling group previously contains three ECS instances, the modification of the Expected Number of Instances parameter triggers a scale-in event. During this time, one ECS instance is removed from the scaling group.

    2. View the details of the scale-in event and check information about the scaled-in ECS instance. For more information, see View the details of a scaling activity.

      In this example, the ID of the scaled-in ECS instance is i-2ze2qdthrkpf****tldq.

      自定义缩容.png

  2. View the invocation logs of the function to check the records of function invocation.

    1. On the Services page in the Function Compute console, click the name of your service.

    2. On the Functions page, click the name of your function.

    3. On the function details page, click the Logs tab.

    4. On the Requests List tab of the Requests tab, find the request that triggered the scale-in event and click the request ID.

    5. On the request details page, click Log Details to view the logs of function invocation triggered by the scale-in event.

      The following figure shows that Auto Scaling passes the required number of ECS instances to scale in and the current instance IDs to Function Compute. Function Compute invokes your function to return the ID of the ECS instance that is scaled in. In this example, the instance ID is i-2ze2qdthrkpf****tldq, which is the same as that of the actual scaled-in instance.

      函数计算日志.png