To control which instances are removed during a scale-in event—for example, removing instances with the lowest CPU load first—you can use Function Compute to create a custom scale-in policy. When Auto Scaling performs a scale-in activity, it automatically selects specific instances for removal based on this policy.
Background information
Auto Scaling supports multiple built-in policies for removing instances during a scale-in activity. These policies use criteria such as the instance creation time or the version of the scaling configuration. For more information, see Configure an instance termination policy for a scaling group.
If you have more specific requirements, you can use Function Compute to create a custom scale-in policy tailored to your business needs. Common use cases include:
-
Scenario 1: Remove instances with the lowest CPU load to minimize impact on your application.
-
Scenario 2: Integrate with your own scheduling system to remove idle instances first, reducing costs while maintaining service availability.
Prerequisites
-
Function Compute is activated and you have granted the required permissions. For more information, see Step 1: Activate Function Compute.
-
Simple Log Service is activated, and you have the required permissions. For more information, see Collect and analyze ECS text logs by using LoongCollector.
Procedure
Step 1: Create a function
The Alibaba Cloud Function Compute console is available in two versions: Function Compute 2.0 and Function Compute 3.0. This guide uses Function Compute 2.0.
-
Log on to the Function Compute console.
If you are in the Function Compute 3.0 console, click Back to Function Compute 2.0.
-
In the left-side navigation pane, click Services & Functions.
-
In the top navigation bar, select a region.
-
Create a service.
-
On the Services page, click Create Service.
-
In the panel that appears, configure the service settings and click OK.
This topic uses the following settings as an example. Use the default values for any other parameters. For more information, see Create a service.
Parameter
Example
Description
Service
test-service
The name of the Function Compute service. Follow the naming conventions shown in the console.
Description
test
A description for the Function Compute service.
Logging
Enable
Enable logging to automatically send function execution logs to Simple Log Service. This lets you debug code, diagnose failures, and perform data analysis.
-
-
Create a function.
-
Click the service name, and then click Function Management in the left-side navigation pane.
-
Click Create Function.
-
On the Create Function page, configure the function settings and click Create.
This topic uses the Use Built-in Runtime method to create the function. The following settings are used as an example. Use the default values for any other parameters. For more information, see Create a function.
Parameter
Example
Description
Basic Settings
Function Name
test-fc
Enter a name for the function. Follow the naming conventions shown in the console.
Handler Type
Event Handler
The following handler types are supported:
-
Event Handler: The function processes event-driven requests.
-
HTTP Handler: The function processes HTTP or WebSocket requests.
Code
Runtime Environment
Python 3.9
The runtime environment for your code.
Code Upload Method
Use Sample Code
Hello, world! sample
The method for uploading the function code to Function Compute.
-
-
-
Write and deploy the function code.
-
Click the function name to go to the function details page.
-
On the Code tab, edit the
index.pyfile in the code editor and click Deploy.You can implement specific filtering logic based on your business requirements, such as retrieving the CPU load of each instance or querying your own service scheduling system.
The following sample code selects instances for removal based on the number of instances to be removed (
Capacity) and the list of instance IDs (InstanceId) passed in the event.# -*- 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
-
-
(Optional) Test the function.
-
On the Code tab, click the
icon next to Test Function and select Configure Test Parameters from the drop-down list. -
Enter the test event object and click OK.
The following code is used as a test example.
ScalingGroupIdspecifies the scaling group to manage.CapacityToRemovespecifies to remove one ECS instance from thecn-beijing-gzone.Instancesspecifies the list of instances available for removal.{ "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" } -
Click Test Function.
-
On the Response tab, view the test result.
The expected response from the function is shown below. This response instructs Auto Scaling to remove the first instance in the list during a scale-in event.
{ "InstanceIds": [ "i-2zeinb37ovdsx6l0****" ] }
-
-
At the top of the page, click Publish Version and then click OK.
Step 2: Configure the scale-in policy
This section shows how to apply the custom scale-in policy when you create a new scaling group. If you have an existing scaling group, you can modify its scale-in policy instead.
Log on to the Auto Scaling console.
-
Create a scaling group.
-
On the Scaling Groups page, click Create Scaling Group.
-
Configure the scaling group parameters and click Create.
This topic uses the following settings as an example. Use the default values for any unmentioned parameters. For more information, see Configure a scaling group.
Parameter
Example
Description
Scaling Group Name
test
Enter a name for the scaling group. Follow the naming conventions shown in the console.
Type
ECS
The type of instances in the scaling group.
This example uses ECS, which means the scaling group manages ECS instances.
Instance Configuration Source
Create from Scratch
The template for creating instances.
This example uses Create from Scratch, which means no template is specified at this stage. You must then create a scaling configuration after the scaling group is created.
Instance Removing Policy
Custom Policy
-
Service: test-service
-
Version: LATEST
-
Function: test-fc
The policy for removing instances from the scaling group during a scale-in activity.
Select Custom Policy and then select the function you created in Step 1.
Minimum Number of Instances
0
The minimum number of instances in the scaling group. If the current number of instances falls below this value, Auto Scaling adds instances to meet the minimum requirement.
Maximum Number of Instances
5
The maximum number of instances in the scaling group. If the current number of instances exceeds this value, Auto Scaling removes instances to meet the maximum requirement.
Enable Expected Number of Instances and Expected Number of Instances
Enabled, 3
Enable this feature and set the expected number of instances. Auto Scaling automatically maintains this number of instances in the group.
-
-
-
Create a scaling configuration. For more information, see Create a scaling configuration for ECS instances.
-
Enable the scaling group. For more information, see Enable or disable a scaling group.
Because the expected number of instances for this scaling group is set to 3, Auto Scaling automatically creates three ECS instances after the group is enabled.
Step 3: Verify the scale-in
-
Trigger a scale-in activity and verify the instance removal.
-
Trigger a scale-in activity by modifying the expected number of instances for the scaling group. For more information, see View or modify a scaling group.
ImportantYou can also trigger scale-in activities by using a scheduled task or an event-triggered task, or by manually executing a scaling rule. However, a custom scale-in policy created by using Function Compute cannot be applied to scale-in activities triggered by modifying the minimum or maximum number of instances for a scaling group.
In this example, the expected number of instances is changed to 2. Because the scaling group previously had three ECS instances, this change automatically triggers a scale-in activity to remove one ECS instance. This reduces the instance count to the new expected number.
-
View the scaling activity details to confirm which instance was removed. For more information, see View scaling activity details.
In this example, the ID of the removed instance is
i-2ze2qdthrkpf****tldq.In the scaling activity details, the Cause field shows that the user changed the expected capacity from 3 to 2. The Details field shows that the corresponding instance was deleted. The Status Information field indicates that one ECS instance was removed, and the activity status is Successful.
-
-
View the function invocation logs to confirm that the scale-in activity triggered the function.
-
In the Function Compute console, go to the Services page and click the name of your service.
-
On the Function Management page, click the name of your function.
-
On the function details page, click the Logs tab.
-
On the Requests tab, in the Requests List, find the request that corresponds to the time of the scale-in activity and click the request ID.
-
In the request details panel that appears, click Log Details to view the logs for the function invocation.
The log output below shows that when the scale-in activity was triggered, Auto Scaling passed the number of instances to remove and the list of current instance IDs in the scaling group to Function Compute. After its logic was applied, the function returned the instance ID
i-2ze2qdthrkpf****tldqfor removal. This ID matches the ID of the removed instance.FC Invoke Start RequestId: 000BFA95-CAA4-5D83-B643-BC51C42402CD 2024-06-13 11:28:22 000BFA95-CAA4-5D83-B643-BC51C42402CD [INFO] {'ScalingGroupARN': 'acs:ess:cn-beijing:16099825xxx:scalinggroup/asg-2e6ny5x9dtxxx', 'ScalingGroupId': 'asg-2e6ny5x9dtiaxxxg', 'CapacityToRemove': [{'ZoneId': 'cn-beijing-g', 'Capacity': 1}], 'Instances': [{'InstanceId': 'i-2ze2qdthrxxxxtldq', 'ZoneId': 'cn-beijing-g', 'InstanceType': 'ecs.c6e.large', 'ChargeType': 'SpotAsPriceGo'}, {'InstanceId': 'i-2ze2qdtxxxxtldr', 'ZoneId': 'cn-beijing-g', 'InstanceType': 'ecs.c6e.large', 'ChargeType': 'SpotAsPriceGo'}, {'InstanceId': 'i-2ze2qdthxxxxtlds', 'ZoneId': 'cn-beijing-g', 'InstanceType': 'ecs.c6e.large', 'ChargeType': 'SpotAsPriceGo'}], 'AdjustmentType': 'SystemScaleIn'} 2024-06-13 11:28:22 000BFA95-CAA4-5D83-B643-BC51C42402CD [INFO] 1 2024-06-13 11:28:22 000BFA95-CAA4-5D83-B643-BC51C42402CD [INFO] {'InstanceIds': ['i-2ze2qdtxxxxtldq']} FC Invoke End RequestId: 000BFA95-CAA4-5D83-B643-BC51C42402CD
-