All Products
Search
Document Center

Auto Scaling:Customize a target tracking scaling rule with formulas

Last Updated:Jun 21, 2026

With target tracking scaling, you can combine multiple Hybrid Cloud Monitoring metrics in a formula to create a custom target tracking scaling rule that meets your business needs.

Overview

Benefits

  • Supports formulas. You can use a formula to combine up to three metrics as the tracking target for more flexibility.

  • Supports more monitoring metrics. This approach is based on Hybrid Cloud Monitoring. Compared with target tracking scaling rules that use system metrics, this approach allows you to use a wider range of metrics for target tracking. For more information about Hybrid Cloud Monitoring, see Overview of Hybrid Cloud Monitoring.

Workflow

This solution combines a target tracking scaling rule for a scaling group with Hybrid Cloud Monitoring. You can follow this workflow to create a custom target tracking scaling rule.

  1. Design a target tracking scaling solution.

    First, identify the Hybrid Cloud Monitoring metrics that you need and design a target tracking scaling solution based on your requirements.

  2. Implement the scaling solution.

    Prepare the required Hybrid Cloud Monitoring metrics and create a target tracking scaling rule based on these metrics.

  3. Continuously monitor and optimize the solution.

    Continuously monitor scaling activities and metric changes to optimize the solution.

1. Design a target tracking scaling solution

First, identify the required Hybrid Cloud Monitoring metrics and design a scaling solution. The following section provides an example.

For more information about the metrics supported by Hybrid Cloud Monitoring, see Appendix 1: Cloud service metrics. To ensure responsive target tracking, select metrics with a minimum aggregation period of 60 seconds.
The calculated metric value must be proportional to the number of instances in the scaling group. This ensures a scale-out occurs when the metric value is above the target and a scale-in occurs when it is below.

Sample solution design

This topic uses an example solution that dynamically adjusts the number of instances in a scaling group based on the number of active messages in a Message Service (MNS) queue.

  • Expected outcome

    The goal is to keep an average of three queued messages per instance in the scaling group. If the number of messages per instance rises above three, the group scales out. If it drops below three, the group scales in.

  • Implementation

    To implement this feature, you need two metrics: the number of active messages in the MNS queue (ActiveMessages) and the number of running instances in the scaling group (RunningInstanceCount). You can create a target tracking scaling rule based on the ratio of these two metrics and set the target value to 3. This maintains the ratio at approximately 3, satisfying the following formula: .

2. Implement the scaling solution

2.1. Prepare Hybrid Cloud Monitoring metrics

If you have the Hybrid Cloud Monitoring metrics ready, you can skip this step.
To use this feature, you must activate Hybrid Cloud Monitoring. For more information, see Activate Hybrid Cloud Monitoring.

Before you create a target tracking scaling rule, prepare the required metrics in Hybrid Cloud Monitoring. Follow these steps:

  1. Create a namespace.

    For more information, see Create a namespace.

  2. Add the required metrics.

    Based on your business requirements, add the required metrics to the namespace. For more information, see Manage metric import tasks.

    Note

    To ensure responsive target tracking, select metrics with a minimum aggregation period of 60 seconds.

Metrics required for the sample solution

Create a namespace named ess-metrics-repo-test and add the following two metrics:

  • The ActiveMessages metric of Active Messages.

  • The Auto Scaling metric of Running Instances.

2.2. Create a target tracking scaling rule

If you have not created a scaling group, create one first. For more information, see Create an ECS scaling group and Create an ECI scaling group.
Important

A target tracking scaling rule supports scaling down to zero instances. Before you create a target tracking scaling rule, set the Minimum Number of Instances and Maximum Number of Instances for the scaling group based on your business requirements. This defines the boundaries for scale-out and scale-in activities and helps prevent service unavailability or unexpected costs resulting from an incorrect solution design.

  1. Navigate to the scaling rule creation page for your scaling group.

    1. Log on to the Auto Scaling console.

    2. In the navigation pane on the left, click Scaling Groups.

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

    4. Find the scaling group that you want to manage and click Actions in the View Details column.

    5. Click the Scaling Rules and Tasks tab.

    6. On the Scaling Rules tab, click Create Scaling Rule.

  2. On the Create Scaling Rule page, configure the following parameters.

    Parameter

    Description

    Rule Type

    Select Target Tracking Scaling Rule.

    Metric Type

    Select Hybrid Cloud Monitoring.

    Monitor Namespace

    Select the namespace that you prepared in Step 2.1.

    Metric Description

    Select the metrics that you prepared in Step 2.1 and the resources that you want to monitor.

    Metric Calculation Expression

    Enter an expression based on your business requirements. The +-*/ and () operators are supported.

    Important
    • A metric expression can contain a maximum of three different metrics.

    • The calculated metric must be proportional to the number of instances. This ensures a scale-out occurs when the metric value is above the target and a scale-in occurs when it is below.

    Target Value

    The target value for the metric expression. The target tracking scaling rule maintains the calculated value of the Hybrid Cloud Monitoring metric at a level that is close to the target value.

    Instance Warmup Time

    Unit: seconds. An instance in the warmup state is added to the scaling group, but its monitoring data is not reported to Hybrid Cloud Monitoring. When calculating scaling adjustments, Auto Scaling does not count instances in the warmup state as part of the current capacity. This prevents metric fluctuations.

    Threshold for Scale-out Alerts

    Specifies the number of consecutive times the threshold must be met to trigger the automatically created scale-out event-triggered task.

    Threshold for Scale-in Alerts

    Specifies the number of consecutive times the threshold must be met to trigger the automatically created scale-in event-triggered task.

    Disable Scale-in

    This parameter specifies whether to disable scale-in and affects the number of event-triggered tasks that are automatically created.

    • If you disable scale-in, the target tracking scaling rule creates only a scale-out event-triggered task. No ECS instances or ECI instances are removed from the scaling group.

    • If you allow scale-in, the target tracking scaling rule creates two event-triggered tasks: one for scale-out and one for scale-in.

    Key configuration for the sample solution

    Parameter

    Example

    Metric Type

    Select Hybrid Cloud Monitoring.

    Monitor Namespace

    Select the namespace that you prepared in Step 2.1, which is ess-metrics-repo-test.

    Metric Description

    Metric a:

    • For Metric Name, select AliyunEss_RunningInstanceCount.

    • For Monitored Resource, select the current scaling group.

    Metric b:

    • For Metric Name, select AliyunMnsnew_ActiveMessages.

    • For Monitored Resource, select the MNS queue that you want to monitor.

    Metric Calculation Expression

    Enter b/a. This expression indicates the average number of messages that each instance needs to process.

    Target Value

    Enter 3. This indicates that the expected average queue length per instance is 3 messages.

  3. After you configure the parameters, click OK to create the target tracking scaling rule.

3. Monitor and optimize the solution

After you create the target tracking scaling rule, continuously monitor your scaling group to ensure it runs as expected. You can review the calculated metric value, scaling activities, and scaling group monitoring data to verify its behavior.

Related operations

  • View information about the target tracking scaling rule

    On the Scaling Rules and Event-triggered Tasks > Scaling Rules tab, find the target tracking scaling rule that you created and click the scaling rule ID to view its details. You can click an associated event-triggered task to view its monitoring information.

  • Monitor the calculated metric value

    After you create a target tracking scaling rule, two Event-triggered Task (Hybrid Cloud Monitoring) are automatically created. On the Scaling Rules and Tasks > Event-triggered Tasks > Event-triggered Task (Hybrid Cloud Monitoring) tab, find the corresponding task and click its ID to go to the task details page and view the calculated metric values.

  • View scaling activities

    On the Scaling Activities tab, view the logs of scaling activities in the scaling group.

Create a rule with the API

Call the CreateScalingRule operation to create a target tracking scaling rule based on Hybrid Cloud Monitoring metrics. Key parameters are described below.

Key parameters

Parameter

Type

Description

Configuration

RegionId

String

The ID of the region where the scaling group is located.

Set this parameter to the region where the target scaling group resides.

ScalingGroupId

string

The ID of the scaling group.

Set this parameter to the ID of the scaling group that you want to modify.

ScalingRuleName

string

The name of the scaling rule.

The name must be 2 to 64 characters in length, start with a digit, a letter, or a Chinese character, and can contain digits, underscores (_), hyphens (-), and periods (.).

The name must be unique within the same region and scaling group for the same Alibaba Cloud account.

If you do not specify this parameter, it defaults to the value of ScalingRuleId.

ScalingRuleType

string

The type of the scaling rule.

To create a target tracking scaling rule, set this parameter to TargetTrackingScalingRule.

DisableScaleIn

boolean

Specifies whether to disable scale-in. This parameter is applicable only to target tracking scaling rules.

  • true: disables scale-in.

  • false: enables scale-in.

Configure this parameter based on your solution.

TargetValue

integer

The target value.

The value of TargetValue must be greater than 0 and can have up to three decimal places.

EstimatedInstanceWarmup

integer

The instance warmup time. An ECS instance in the warmup state is added to the scaling group, but its monitoring data is not reported to Hybrid Cloud Monitoring.

When calculating the number of instances to add or remove, Auto Scaling does not count instances in the warmup state as part of the current capacity.

Valid values: 0 to 86400. Unit: seconds.

Default value: 300.

ScaleOutEvaluationCount

integer

After you create a target tracking scaling rule, an event-triggered task is automatically created. This parameter specifies the number of consecutive times that the threshold must be met for the scale-out event-triggered task to be triggered.

Default value: 3.

Configure this parameter based on your solution.

ScaleInEvaluationCount

integer

After you create a target tracking scaling rule, an event-triggered task is automatically created. This parameter specifies the number of consecutive times that the threshold must be met for the scale-in event-triggered task to be triggered.

Default value: 15.

Configure this parameter based on your solution.

MetricType

string

The type of the metric.

To create a target tracking scaling rule that is based on Hybrid Cloud Monitoring metrics, set this parameter to hybrid. This value specifies that Hybrid Cloud Monitoring is used as the metric source.

HybridMonitorNamespace

string

The ID of the Hybrid Cloud Monitoring namespace.

For more information about how to manage a namespace, see Manage a namespace.

Configure this parameter based on your solution.

HybridMetrics

array<object>

The settings of the Hybrid Cloud Monitoring metrics.

An array of Hybrid Cloud Monitoring metrics.

Subparameters of HybridMetrics

Id

string

The reference ID of a metric in the metric expression.

This is similar to declaring a variable that you reference later in the expression.

You can set this parameter to a, b, c, and so on.

MetricName

string

The name of the metric in the Hybrid Cloud Monitoring namespace.

Configure this parameter based on your solution.

Statistic

string

The aggregation method of the metric.

Configure this parameter based on your solution. Valid values:

  • Average: the average of all data points in the specified period.

  • Minimum: the minimum of all data points in the specified period.

  • Maximum: the maximum of all data points in the specified period.

Expression

string

A metric expression that combines multiple Hybrid Cloud Monitoring metrics. The calculation result of the expression is used to trigger scale-out or scale-in activities.

The expression uses infix notation and supports the +, -, *, and / operators.

Configure this parameter based on your solution.

Dimensions

object

The dimensions of the metric. This parameter is used to specify the resource that you want to monitor.

Subparameters of Dimensions

DimensionKey

string

The key of the metric dimension.

Configure this parameter based on your solution.

DimensionValue

string

The value of the metric dimension.

Configure this parameter based on your solution.

Example request

The following example shows how to use the API to implement the sample solution described in Section 1.

The following request body shows the parameter configuration for this solution.

{
    "RegionId": "cn-hangzhou",
    "ScalingGroupId": "asg-bp1******",
    "ScalingRuleType": "TargetTrackingScalingRule",
    "ScalingRuleName": "TrackingQueueLength",
    "DisableScaleIn": false,
    "TargetValue": 3,
    "EstimatedInstanceWarmup": 300,
    "ScaleOutEvaluationCount": 3,
    "ScaleInEvaluationCount": 15,
    "MetricType": "hybrid",
    "HybridMonitorNamespace": "ess-metrics-repo-test",
    "HybridMetrics.1.Id": "a",
    "HybridMetrics.1.MetricName": "AliyunEss_RunningInstanceCount",
    "HybridMetrics.1.Statistic": "Average",
    "HybridMetrics.1.Dimensions.1.DimensionKey": "instanceId",
    "HybridMetrics.1.Dimensions.1.DimensionValue": "asg-bp1******",
    "HybridMetrics.2.Id": "b",
    "HybridMetrics.2.MetricName": "AliyunMnsnew_ActiveMessages",
    "HybridMetrics.2.Statistic": "Average",
    "HybridMetrics.2.Dimensions.1.DimensionKey": "region",
    "HybridMetrics.2.Dimensions.1.DimensionValue": "cn-hangzhou",
    "HybridMetrics.2.Dimensions.2.DimensionKey": "userId",
    "HybridMetrics.2.Dimensions.2.DimensionValue": "******",
    "HybridMetrics.2.Dimensions.3.DimensionKey": "queue",
    "HybridMetrics.2.Dimensions.3.DimensionValue": "test",
    "HybridMetrics.3.Id": "Expression",
    "HybridMetrics.3.Expression": "b/a"
}

How the key JSON fields map to the formula:

  • Declare variable a as the value of the AliyunEss_RunningInstanceCount metric in the ess-metrics-repo-test namespace, and specify the scaling group ID as asg-bp1******.

    Meaning: the number of running instances in the scaling group whose ID is asg-bp1******.
    "HybridMetrics.1.Id": "a",
    "HybridMetrics.1.MetricName": "AliyunEss_RunningInstanceCount",
    "HybridMetrics.1.Statistic": "Average",
    "HybridMetrics.1.Dimensions.1.DimensionKey": "instanceId",
    "HybridMetrics.1.Dimensions.1.DimensionValue": "asg-bp1******",
  • Declare variable b as the value of the AliyunMnsnew_ActiveMessages metric in the ess-metrics-repo-test namespace, and specify the metric dimensions as region=cn-hangzhou, userId=******, and queue=test.

    Meaning: The number of active messages in the test queue in the China (Hangzhou) region under the specified user account.
    "HybridMetrics.2.Id": "b",
    "HybridMetrics.2.MetricName": "AliyunMnsnew_ActiveMessages",
    "HybridMetrics.2.Statistic": "Average",
    "HybridMetrics.2.Dimensions.1.DimensionKey": "region",
    "HybridMetrics.2.Dimensions.1.DimensionValue": "cn-hangzhou",
    "HybridMetrics.2.Dimensions.2.DimensionKey": "userId",
    "HybridMetrics.2.Dimensions.2.DimensionValue": "******",
    "HybridMetrics.2.Dimensions.3.DimensionKey": "queue",
    "HybridMetrics.2.Dimensions.3.DimensionValue": "test",
  • Set the metric formula to b/a.

    Meaning: implements the formula .
    "HybridMetrics.3.Id": "Expression",
    "HybridMetrics.3.Expression": "b/a"
  • Set the target value for the formula result.

    Meaning: the target value for the result of the formula is 3.
    "TargetValue": 3