Scalability is an important feature that measures the O&M capabilities of distributed applications. This feature can automatically increase or decrease the number of instances to scale in or scale out system capacity. This improves resource utilization and reduces resource usage costs. This topic describes how to use Terraform to enable and disable an auto scaling policy for a Serverless App Engine (SAE) application.
Background
SAE auto scaling policies are classified into scheduled auto scaling policies, metric-based auto scaling policies, and hybrid auto scaling policies. If you want to use Terraform to enable an auto scaling policy for an SAE application, you must configure the alicloud_sae_application_scaling_rule resource when you create the application. If you want to disable the auto scaling policy, you must delete both the auto scaling policy and the application. For information about how to specify scheduled periods of time, see Use a crontab expression.
Scenarios
Scheduled auto scaling policies are suitable for scenarios in which an application needs to use resources within a specific period of time. Scheduled auto scaling policies are commonly used in industries such as securities, healthcare, public administration, and education.
Metric-based auto scaling policies are suitable for scenarios in which burst traffic and periodic traffic occur when an application uses resources. Metric-based auto scaling policies are commonly used in industries such as Internet, gaming, and social media.
Hybrid auto scaling policies are suitable for scenarios in which an application needs to use resources within a specific period of time and burst traffic and periodic traffic occur when the application uses the resources. Hybrid auto scaling policies are commonly used in industries such as Internet, education, and catering.
For more information, see Configure an auto scaling policy.
You can run the following sample code in this topic with a few clicks. For more information, see Terraform Explorer.
Prerequisites
An Alibaba Cloud account has full permissions on all resources that belong to this account. If the credentials of an Alibaba Cloud account are leaked, security risks may arise. We recommend that you use a Resource Access Management (RAM) user and create an AccessKey pair for the RAM user. For more information, see Create a RAM user and Create an AccessKey pair.
The following policy is attached to the RAM user that you use to run commands in Terraform. The policy includes the minimum permissions required to run commands in Terraform. For more information, see Create a custom policy on the JSON tab.
This custom policy allows RAM users or roles to manage the full lifecycle of SAE applications, including creating, updating, deleting, starting, stopping, deploying, and rolling applications, along with managing auto scaling rule.
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "sae:CreateApplication", "sae:UpdateApplication", "sae:DeleteApplication", "sae:GetApplication", "sae:ListApplications", "sae:CreateScalingRule", "sae:UpdateScalingRule", "sae:DeleteScalingRule", "sae:GetScalingRule", "sae:ListScalingRules", "sae:StartApplication", "sae:StopApplication", "sae:DeployApplication", "sae:RollbackApplication" ], "Resource": "*" } ] }The runtime environment for Terraform is prepared by using one of the following methods:
Terraform is available as a managed service in ROS. You can deploy Terraform templates in the ROS console. For more information, see Create a Terraform stack.
Use Terraform in Terraform Explorer: Alibaba Cloud provides an online runtime environment for Terraform. You can log on to the Terraform Explorer environment to use Terraform without the need to install Terraform. This method is suitable for scenarios in which you want to use and debug Terraform in a fast and convenient manner at no additional cost.
Cloud Shell: Alibaba Cloud Cloud Shell is a free O&M product that comes pre-installed with Terraform and configured with authentication credentials. Therefore, you can run Terraform commands in Cloud Shell. This method is suitable for scenarios in which you want to access and use Terraform in a fast and convenient manner at low costs.
Install and configure Terraform: This method is suitable for scenarios where network connections are unstable or a custom development environment is required.
Resources
alicloud_sae_namespace is used to create an SAE namespace.
alicloud_security_group is used to build and manage a security group.
alicloud_security_group_rule is used to define ingress or egress rules for a security group.
alicloud_sae_application is used to create an SAE application.
alicloud_sae_application_scaling_rule is used to create an auto scaling policy for an SAE application.
Enable a scheduled auto scaling policy
This example shows how to create an application in the China (Shenzhen) region, deploy the application in image mode and configure a scheduled auto scaling policy for the application.
In this example, the scheduled auto scaling policy is executed at an interval of one day. The policy starts to be executed the first time for five instances at 19:35 of a day. The policy starts to be executed the second time for two instances at 20:35 of the same day. SAE retains five instances of the application from 19:35 to 20:35 of the day and retains two instances of the application from 20:35 of the day to 19:35 of the next day based on the scheduled auto scaling policy.
- Create a project folder named terraform for storing Terraform resources.
Run the following command to go to the project directory:
cd terraformCreate a configuration file named main.tf.
# Provider configuration provider "alicloud" { region = var.region_id } # Variable definitions variable "region_id" { type = string default = "cn-shenzhen" } variable "app_name" { description = "Specify the name of the application" type = string default = "app-scaling" } variable "image_url" { description = "Specify the URL of the image" type = string default = "registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9" } variable "namespace_name" { description = "Specify the name of the namespace" type = string default = "demo" } variable "namespace_id" { description = "Specify the ID of the namespace" type = string default = "cn-shenzhen:demo" } # Namespace resource "alicloud_sae_namespace" "default" { namespace_description = var.namespace_description namespace_id = var.namespace_id namespace_name = var.namespace_name } # VPC resource "alicloud_vpc" "default" { vpc_name = var.name cidr_block = "10.0.0.0/16" } # VSwitch resource "alicloud_vswitch" "default" { vswitch_name = var.name cidr_block = "10.0.1.0/24" vpc_id = alicloud_vpc.default.id zone_id = var.zone_id } # Security group resource "alicloud_security_group" "sg" { name = var.name description = var.description vpc_id = alicloud_vpc.default.id } resource "alicloud_security_group_rule" "sg_rule" { type = "ingress" ip_protocol = "tcp" nic_type = "intranet" policy = "accept" port_range = var.port_range priority = 1 security_group_id = alicloud_security_group.sg.id cidr_ip = var.cidr_ip } # Application configuration resource "alicloud_sae_application" "default" { app_name = var.app_name app_description = var.app_description deploy = true image_url = var.image_url namespace_id = alicloud_sae_namespace.default.id vswitch_id = alicloud_vswitch.default.id vpc_id = alicloud_vpc.default.id security_group_id = alicloud_security_group.sg.id package_type = var.package_type timezone = "Asia/Beijing" replicas = var.replicas cpu = var.cpu memory = var.memory } # Auto scaling policy resource "alicloud_sae_application_scaling_rule" "metrics" { app_id = alicloud_sae_application.default.id scaling_rule_name = "metric-dev" scaling_rule_enable = true scaling_rule_type = "mix" scaling_rule_timer { begin_date = "2024-11-26" end_date = "2024-11-30" period = "* * *" schedules { at_time = "19:45" max_replicas = 50 min_replicas = 10 } schedules { at_time = "20:45" max_replicas = 40 min_replicas = 3 } } scaling_rule_metric { max_replicas = 40 min_replicas = 3 metrics { metric_type = "CPU" metric_target_average_utilization = 1 } scale_up_rules { step = 10 disabled = false stabilization_window_seconds = 0 } scale_down_rules { step = 10 disabled = false stabilization_window_seconds = 10 } } } # Other variable definitions variable "namespace_description" { description = "Namespace Description" default = "a namespace" } variable "name" { default = "tf" description = "The name of the security group rule" type = string } variable "description" { default = "The description of the security group rule" description = "The description of the security group rule" type = string } variable "port_range" { default = "1/65535" description = "The port range of the security group rule" type = string } variable "cidr_ip" { description = "cidr blocks used to create a new security group rule" type = string default = "0.0.0.0/0" } variable "zone_id" { description = "Availability Zone ID" type = string default = "cn-shenzhen-e" } variable "app_description" { default = "description created by Terraform" description = "The description of the application" type = string } variable "package_type" { default = "Image" description = "The package type of the application" type = string } variable "cpu" { default = "500" description = "The cpu of the application, in unit of millicore" type = string } variable "memory" { default = "1024" description = "The memory of the application, in unit of MB" type = string } variable "replicas" { default = "1" description = "The replicas of the application" type = string } variable "port" { description = "The port of SLB" type = string default = "8000" } # Outputs output "namespace_id" { value = alicloud_sae_namespace.default.id description = "Namespace ID" } output "app_id" { description = "The id of the application" value = alicloud_sae_application.default.id } output "app_name" { description = "The name of the application" value = var.app_name }Run the following command to initialize the configurations:
terraform initExpected output:
Perform the following steps to create an SAE application.
Run the following command to execute the configuration file.Enter
yesas prompted and press Enter. Wait for the command to be run. If the following command output is returned, the authorization is complete.
terraform applyExpected output:
Verify
Run the terraform show command
Run the following command to query the details of the resources that are created by Terraform:
terraform show
SAE console
The app-scaling application is created and a scheduled auto scaling policy is enabled for the application. You can log on to the SAE console and view the auto scaling policy and instance status on the Instance Information tab of the Basic Information page of the application.

Enable a metric-based auto scaling policy
This example shows how to enable a metric-based auto scaling policy based on the main.tf file that is created to configure a scheduled auto scaling policy. The setting of the alicloud_sae_application_scaling_rule resource is replaced by the configurations of the metric-based auto scaling policy. Other settings remain unchanged. For more information, see Enable a scheduled auto scaling policy.
# Auto scaling policy
resource "alicloud_sae_application_scaling_rule" "metrics" {
app_id = alicloud_sae_application.default.id
scaling_rule_name = "metric-dev"
scaling_rule_enable = true
scaling_rule_type = "metric"
scaling_rule_metric {
max_replicas = 50
min_replicas = 3
metrics {
metric_type = "CPU"
metric_target_average_utilization = 1
}
scale_up_rules {
step = 10
disabled = false
stabilization_window_seconds = 0
}
scale_down_rules {
step = 10
disabled = false
stabilization_window_seconds = 10
}
}
}In this example, if the CPU utilization exceeds 1%, the application is automatically scaled out and a maximum of 50 instances can be used. If the CPU utilization drops below 1%, the application is automatically scaled in and a minimum of three instances can be used.
A low CPU utilization is specified in the sample code to show the test result. You can specify parameter values based on your business requirements.
Run the following command to initialize the configurations:
terraform initExpected output:
Perform the following steps to create an SAE application.
Run the following command to execute the configuration file.Enter
yesas prompted and press Enter. Wait for the command to be run. If the following command output is returned, the authorization is complete.
terraform applyExpected output:
Verify
Run the terraform show command
Run the following command to query the details of the resources that are created by Terraform:
terraform show
SAE console
The app-scaling application is created and a metric-based auto scaling policy is enabled for the application. You can log on to the SAE console and view the auto scaling policy and instance status on the Instance Information tab of the Basic Information page of the application.

Enable a hybrid auto scaling policy
This example shows how to enable a hybrid auto scaling policy based on the main.tf file that is created to configure a scheduled auto scaling policy. The setting of the alicloud_sae_application_scaling_rule resource is replaced by the configurations of the hybrid auto scaling policy. Other settings remain unchanged. For more information, see Enable a scheduled auto scaling policy.
Sample code:
resource "alicloud_sae_application_scaling_rule" "metrics" {
app_id = alicloud_sae_application.default.id
scaling_rule_name = "metric-dev"
scaling_rule_enable = true
scaling_rule_type = "mix"
scaling_rule_timer {
begin_date = "2022-04-20"
end_date = "2022-05-31"
period = "* * *"
schedules {
at_time = "19:45"
max_replicas = 50
min_replicas = 10
}
schedules {
at_time = "20:45"
max_replicas = 40
min_replicas = 3
}
}
scaling_rule_metric {
max_replicas = 40
min_replicas = 3
metrics {
metric_type = "CPU"
metric_target_average_utilization = 1
}
scale_up_rules {
step = 10
disabled = false
stabilization_window_seconds = 0
}
scale_down_rules {
step = 10
disabled = false
stabilization_window_seconds = 10
}
}
}The following information describes the content of the hybrid auto scaling policy:
Common period of time: If the CPU utilization exceeds 1%, the application is automatically scaled out and a maximum of 40 instances can be used. If the CPU utilization drops below 1%, the application is automatically scaled in and a minimum of three instances can be used.
Special period of time: From November 26, 2024, to November 30, 2022, the scheduled auto scaling policy is executed based on the CPU utilization threshold.
From 19:45 to 20:45 of a day within the specified period of time, a minimum of 10 instances and a maximum of 50 instances can be used in the application.
From 20:45 of a day to 20:45 of the next day within the specified period of time, a minimum of three instances and a maximum of 40 instances can be used in the application.
A low CPU utilization is specified in the sample code to show the test result. You can specify parameter values based on your business requirements.
Run the following command to initialize the configurations:
terraform initExpected output:
Perform the following steps to create an SAE application.
Run the following command to execute the configuration file.Enter
yesas prompted and press Enter. Wait for the command to be run. If the following command output is returned, the authorization is complete.
terraform applyExpected output:
Verify.
Run the terraform show command
Run the following command to query the details of the resources that are created by Terraform:
terraform show
SAE console
The app-scaling application is created and a hybrid auto scaling policy is enabled for the application. You can log on to the SAE console and view the auto scaling policy and instance status on the Instance Information tab of the Basic Information page of the application.

Disable an auto scaling policy and delete an application
This section provides an example on how to disable an auto scaling policy and delete the application for which the auto scaling policy is enabled. In this example, the app-scaling application that resides in the China (Hangzhou) region is deleted.
- Run the following command in the project directory to execute the configuration file:
terraform destroy Expected output:

The auto scaling policy is disabled and the
app-scalingapplication is deleted.Complete code
NoteYou can run the sample code in this topic with a few clicks. For more information, see Terraform Explorer.
# Provider configuration provider "alicloud" { region = var.region_id } # Variable definitions variable "region_id" { type = string default = "cn-shenzhen" } variable "app_name" { description = "Specify the name of the application" type = string default = "app-scaling" } variable "image_url" { description = "Specify the URL of the image" type = string default = "registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9" } variable "namespace_name" { description = "Specify the name of the namespace" type = string default = "demo" } variable "namespace_id" { description = "Specify the ID of the namespace" type = string default = "cn-shenzhen:demo" } # Namespace resource "alicloud_sae_namespace" "default" { namespace_description = var.namespace_description namespace_id = var.namespace_id namespace_name = var.namespace_name } # VPC resource "alicloud_vpc" "default" { vpc_name = var.name cidr_block = "10.0.0.0/16" } # VSwitch resource "alicloud_vswitch" "default" { vswitch_name = var.name cidr_block = "10.0.1.0/24" vpc_id = alicloud_vpc.default.id zone_id = var.zone_id } # Security group resource "alicloud_security_group" "sg" { name = var.name description = var.description vpc_id = alicloud_vpc.default.id } resource "alicloud_security_group_rule" "sg_rule" { type = "ingress" ip_protocol = "tcp" nic_type = "intranet" policy = "accept" port_range = var.port_range priority = 1 security_group_id = alicloud_security_group.sg.id cidr_ip = var.cidr_ip } # Application configuration resource "alicloud_sae_application" "default" { app_name = var.app_name app_description = var.app_description deploy = true image_url = var.image_url namespace_id = alicloud_sae_namespace.default.id vswitch_id = alicloud_vswitch.default.id vpc_id = alicloud_vpc.default.id security_group_id = alicloud_security_group.sg.id package_type = var.package_type timezone = "Asia/Beijing" replicas = var.replicas cpu = var.cpu memory = var.memory } # Auto scaling policy resource "alicloud_sae_application_scaling_rule" "metrics" { app_id = alicloud_sae_application.default.id scaling_rule_name = "metric-dev" scaling_rule_enable = true scaling_rule_type = "mix" scaling_rule_timer { begin_date = "2024-11-26" end_date = "2024-11-30" period = "* * *" schedules { at_time = "19:45" max_replicas = 50 min_replicas = 10 } schedules { at_time = "20:45" max_replicas = 40 min_replicas = 3 } } scaling_rule_metric { max_replicas = 40 min_replicas = 3 metrics { metric_type = "CPU" metric_target_average_utilization = 1 } scale_up_rules { step = 10 disabled = false stabilization_window_seconds = 0 } scale_down_rules { step = 10 disabled = false stabilization_window_seconds = 10 } } } # Other variable definitions variable "namespace_description" { description = "Namespace Description" default = "a namespace" } variable "name" { default = "tf" description = "The name of the security group rule" type = string } variable "description" { default = "The description of the security group rule" description = "The description of the security group rule" type = string } variable "port_range" { default = "1/65535" description = "The port range of the security group rule" type = string } variable "cidr_ip" { description = "cidr blocks used to create a new security group rule" type = string default = "0.0.0.0/0" } variable "zone_id" { description = "Availability Zone ID" type = string default = "cn-shenzhen-e" } variable "app_description" { default = "description created by Terraform" description = "The description of the application" type = string } variable "package_type" { default = "Image" description = "The package type of the application" type = string } variable "cpu" { default = "500" description = "The cpu of the application, in unit of millicore" type = string } variable "memory" { default = "1024" description = "The memory of the application, in unit of MB" type = string } variable "replicas" { default = "1" description = "The replicas of the application" type = string } variable "port" { description = "The port of SLB" type = string default = "8000" } # Outputs output "namespace_id" { value = alicloud_sae_namespace.default.id description = "Namespace ID" } output "app_id" { description = "The id of the application" value = alicloud_sae_application.default.id } output "app_name" { description = "The name of the application" value = var.app_name }References
For more information about Terraform, see What is Terraform?.