In distributed application management, auto scaling is an important operations capability. It automatically increases or decreases the number of instances by scaling out or scaling in based on their status to improve resource utilization and reduce resource costs. This article describes how to use Terraform to enable or disable auto scaling policies for SAE applications.
Background information
SAE auto scaling policies include scheduled auto scaling policies, metric-based auto scaling policies, and mixed auto scaling policies. To enable an auto scaling policy for an SAE application by using Terraform, you must configure the alicloud_sae_application_scaling_rule resource when you create the application. To disable the auto scaling policy, you must delete both the auto scaling policy and the application. For more information about how to set a time period, see Using Crontab Expressions.
Scenarios
A scheduled auto scaling policy is suitable for applications that have predictable and cyclical resource usage patterns. This policy is often used in industries such as finance, healthcare, government, and education.
A metric-based auto scaling policy is suitable for applications that experience sudden traffic bursts or have typical periodic traffic patterns. This policy is often used in industries such as the internet, gaming, and social media.
A hybrid auto scaling policy is suitable for applications that have both cyclical resource usage patterns and experience traffic bursts or periodic traffic patterns. This policy is often used in industries such as the internet, education, and food services.
For more information, see Configure an auto scaling policy.
The sample code in this topic can be run with one click in Terraform Explorer. Run in Terraform Explorer
Prerequisites
An Alibaba Cloud account has full permissions for all its resources, which poses a significant security risk if the credentials are leaked. We recommend that you use a RAM user and create an AccessKey pair for the RAM user. For more information, see Create a RAM user and Create an AccessKey.
Attach the following policy to the RAM user that you use to run Terraform commands. This policy provides the minimum permissions required for this tutorial. For more information, see Create a custom policy by using the script editor.
This custom policy allows a RAM user or role to manage the full lifecycle of Serverless Application Engine (SAE) applications. The permissions include creating, updating, deleting, starting, stopping, deploying, and rolling back applications, and managing auto scaling rules.
{ "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": "*" } ] }Prepare a Terraform runtime environment. You can use one of the following methods to run Terraform:
Resource Orchestration Service (ROS) provides a managed service for Terraform. You can deploy Terraform templates directly in the ROS console. For more information, see Create a stack of the Terraform type.
Use Terraform in Terraform Explorer: Alibaba Cloud provides an online runtime environment for Terraform. You do not need to install Terraform. You can log on to use and try out Terraform online. This method is suitable for scenarios where you want to try and debug Terraform quickly and easily at no cost.
Use Terraform to quickly create resources: Terraform is pre-installed and credentials are configured in Cloud Shell, which allows you to run Terraform commands directly. This method is ideal for scenarios where you need to use Terraform quickly, conveniently, and at a low cost.
Install and configure Terraform locally: This method is suitable for scenarios where you have a poor network connection or need a custom development environment.
Resources used
alicloud_sae_namespace: Provides a resource to create an SAE namespace.
alicloud_security_group: Provides a resource to create a security group.
alicloud_security_group_rule: Provides a resource to create a security group rule.
alicloud_sae_application: Provides a resource to create an SAE application.
alicloud_sae_application_scaling_rule: Provides a resource 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 it from an image, and configure a scheduled auto scaling policy for the application.
The auto scaling policy is as follows: The cycle is set to daily. The first trigger point is at 19:35 with a target of 5 instances, and the second is at 20:35 with a target of 2 instances. As a result, between 19:35 and 20:35, SAE maintains 5 instances for the application based on the configured rules. Between 20:35 and 19:35 on the next day, the number of application instances is maintained at 2.
- Create a project folder named terraform for storing Terraform resources.
Run the following command to navigate to the project directory.
cd terraformCreate a configuration file named main.tf.
# Configure the provider. provider "alicloud" { region = var.region_id } # Define variables. variable "region_id" { type = string default = "cn-shenzhen" } variable "app_name" { description = "The name of the application." type = string default = "app-scaling" } variable "image_url" { description = "The address of the image." type = string default = "registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9" } variable "namespace_name" { description = "The name of the namespace." type = string default = "demo" } variable "namespace_id" { description = "The ID of the namespace." type = string default = "cn-shenzhen:demo" } # Create a namespace. resource "alicloud_sae_namespace" "default" { namespace_description = var.namespace_description namespace_id = var.namespace_id namespace_name = var.namespace_name } # Create a VPC. resource "alicloud_vpc" "default" { vpc_name = var.name cidr_block = "10.0.0.0/16" } # Create a 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 } # Create a 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 } # Configure the application. 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/Shanghai" replicas = var.replicas cpu = var.cpu memory = var.memory } # Configure the auto scaling policy. resource "alicloud_sae_application_scaling_rule" "default" { app_id = alicloud_sae_application.default.id scaling_rule_name = "timing-rule" scaling_rule_enable = true scaling_rule_type = "timing" scaling_rule_timer { period = "* * *" schedules { at_time = "19:35" target_replicas = 5 } schedules { at_time = "20:35" target_replicas = 2 } } } # Define other variables. variable "namespace_description" { description = "The description of the namespace." default = "A sample namespace for Terraform." } variable "name" { default = "tf-example" description = "The name of the resource." type = string } variable "description" { default = "Created by Terraform for an example." description = "The description of the resource." type = string } variable "port_range" { default = "1/65535" description = "The port range of the security group rule." type = string } variable "cidr_ip" { description = "The CIDR block for the security group rule." type = string default = "0.0.0.0/0" } variable "zone_id" { description = "The ID of the availability zone." type = string default = "cn-shenzhen-e" } variable "app_description" { default = "An application 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 millicores." type = number } variable "memory" { default = 1024 description = "The memory of the application, in MB." type = number } variable "replicas" { default = 1 description = "The number of replicas for the application." type = number } variable "port" { description = "The port of the SLB instance." type = string default = "8000" } # Define outputs. output "namespace_id" { value = alicloud_sae_namespace.default.id description = "The ID of the namespace." } 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 configuration:
terraform initIf the command runs successfully, the following output is returned:
Terraform has been successfully initialized!
You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.
If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.Run the following commands in order to create the application.
Run the following command to deploy the application.During the process, enter
yesand press the Enter key when prompted. Wait for the command to complete. If the following message appears, the authorization is complete.
terraform applyExpected output:
Apply complete! Resources: 7 added, 0 changed, 0 destroyed.
Outputs:
app_id = "3abab264-xxx"
app_name = "app-scaling"
namespace_id = "cn-shenzhen:demo"Verify the result
Terraform show
You can run the following command to query the details of the resources that are created by Terraform:
terraform show# alicloud_sae_application_scaling_rule.default:
resource "alicloud_sae_application_scaling_rule" "default" {
app_id = "3abab264-72ba-42xxx"
id = "3abab264-72ba-42xxx:timing-rule"
scaling_rule_enable = true
scaling_rule_name = "timing-rule"
scaling_rule_type = "timing"
scaling_rule_timer {
period = "* * *"
schedules {
at_time = "19:35"
target_replicas = 5
}
schedules {
at_time = "20:35"
target_replicas = 2
}
}
}
...SAE console
You have successfully created the application app-scaling with a scheduled auto scaling policy. You can log on to the SAE console to view the status of the auto scaling policy and application instances on the Instance Deployment Information tab of the Basic Information page for the target application.
In the left-side navigation pane, choose Application Management > Application List. Find the target application app-scaling. Confirm that its Auto Scaling Policy Status is Enabled (Scheduled) and the Current/Target Instances is 2/2.
Enable a metric-based auto scaling policy
This example is based on the main.tf file, which is configured with a scheduled auto scaling policy. In this file, only the configuration of the alicloud_sae_application_scaling_rule resource is changed from a scheduled auto scaling policy to a metric-based auto scaling policy. The other configurations remain unchanged. For more information, see Enable a scheduled auto scaling policy.
# Configure the auto scaling policy.
resource "alicloud_sae_application_scaling_rule" "default" {
app_id = alicloud_sae_application.default.id
scaling_rule_name = "metric-rule"
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 = 20
}
scale_up_rules {
step = 10
disabled = false
stabilization_window_seconds = 0
}
scale_down_rules {
step = 10
disabled = false
stabilization_window_seconds = 10
}
}
}This policy scales the application out to a maximum of 50 instances when average CPU utilization exceeds 20%, and scales it in to a minimum of 3 instances when utilization falls below that threshold.
The sample code uses a low CPU utilization threshold to make the scaling effect easier to observe. In a production environment, set parameters appropriate for your workload.
Save the changes to the
main.tffile.
terraform initRun the following commands in order to create an application.
Run the following command to deploy the application.When prompted, enter
yesand press the Enter key. Wait for the command to complete. If the following message is displayed, the authorization is complete.
terraform applyTerraform detects the change in the scaling rule and updates the resource.
alicloud_sae_application_scaling_rule.default: Modifying... [id=...]
alicloud_sae_application_scaling_rule.default: Modifications complete after 1s [id=...]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.Verify the result
Terraform show
Run the terraform show command again. The output now shows that the alicloud_sae_application_scaling_rule resource has been updated to scaling_rule_type = "metric" and contains a scaling_rule_metric block.
terraform showExample output:
# alicloud_sae_application_scaling_rule.default:
resource "alicloud_sae_application_scaling_rule" "default" {
app_id = "22238343-81c2-4xxx"
id = "22238343-81c2-4xxx:metric-rule"
scaling_rule_enable = true
scaling_rule_name = "metric-rule"
scaling_rule_type = "metric"
scaling_rule_metric {
max_replicas = 50
min_replicas = 3
metrics {
metric_target_average_utilization = 20
metric_type = "CPU"
}
# ...
}
}
# ...SAE console
The application app-scaling with an elastic scheduled policy enabled has been successfully created. You can log on to the SAE console and view the running status of the scaling policy and application instances on the Instance Deployment Information tab of the Basic Information page for the target application.
On the Application List page, the Auto Scaling Policy Status column for the app-scaling application displays Enabled (Metric-based).
Enable a hybrid auto scaling policy
This example is based on the main.tf file that is configured with a scheduled auto scaling policy. Only the content of the alicloud_sae_application_scaling_rule resource is changed from a scheduled auto scaling policy to a hybrid auto scaling policy, and the other content remains unchanged. For specific operations, see Enable a scheduled auto scaling policy.
Sample code:
resource "alicloud_sae_application_scaling_rule" "default" {
app_id = alicloud_sae_application.default.id
scaling_rule_name = "hybrid-rule"
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:35"
target_replicas = 10
}
schedules {
at_time = "20:35"
target_replicas = 3
}
}
scaling_rule_metric {
max_replicas = 40
min_replicas = 3
metrics {
metric_type = "CPU"
metric_target_average_utilization = 20
}
scale_up_rules {
step = 10
disabled = false
stabilization_window_seconds = 0
}
scale_down_rules {
step = 10
disabled = false
stabilization_window_seconds = 10
}
}
}The policy in this example is configured as follows:
Default behavior (metric-based): The application scales between 3 and 40 instances based on CPU utilization, scaling out when the average CPU utilization exceeds 20%.
Scheduled behavior: Between November 26, 2024, and November 30, 2024, a scheduled policy takes precedence.
From 19:35 to 20:35, the application is set to a target of 10 instances.
Outside this window, the target is 3 instances.
The sample code uses a low CPU utilization threshold to make the scaling effect easier to observe. In a production environment, set parameters appropriate for your workload.
Save the changes to the
main.tffile and runterraform applyagain.
terraform applyRun the following commands to create an application.
Run the following command to deploy the application.During the execution, enter
yesand press Enter when prompted. Wait for the command to complete. If the following information is displayed, the authorization is complete.
terraform applyExpected output:
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.Verify the result
Terraform show
Run terraform show to see the final configuration. The scaling_rule_type is now mix, and the resource contains both scaling_rule_timer and scaling_rule_metric blocks.
terraform showExample output:
# alicloud_sae_application_scaling_rule.default:
resource "alicloud_sae_application_scaling_rule" "default" {
app_id = "2fadcbf8-fcbf-455d-xxx"
id = "2fadcbf8-fcbf-455d-xxx:hybrid-rule"
scaling_rule_enable = true
scaling_rule_name = "hybrid-rule"
scaling_rule_type = "mix"
scaling_rule_metric {
max_replicas = 40
min_replicas = 3
# ...
}
scaling_rule_timer {
begin_date = "2024-11-26"
end_date = "2024-11-30"
period = "* * *"
# ...
}
}SAE console
You have successfully created the application app-scaling with a scheduled auto scaling policy enabled. You can log on to SAE console to view the running status of the auto scaling policy and application instances on the Instance Deployment Information tab of the Basic Information page for the target application.
Navigate to the Auto Scaling tab for the application. You can view the details for both the scheduled and metric-based components of the enabled hybrid-rule policy.
Delete the auto scaling policy and application
This example uses the application app-scaling in the China (Hangzhou) region to describe how to disable an auto scaling policy and delete the application.
- Run the following command in the project directory to execute the configuration file:
terraform destroy Expected output:
... Destroy complete! Resources: 7 destroyed.Successfully deactivated the scaling policy and deleted the application
app-scaling.Complete code
NoteYou can run the sample code in this topic with one click in Terraform Explorer. Run in Terraform Explorer
# Configure the provider. provider "alicloud" { region = var.region_id } # Define variables. variable "region_id" { type = string default = "cn-shenzhen" } variable "app_name" { description = "The name of the application." type = string default = "app-scaling" } variable "image_url" { description = "The address of the image." type = string default = "registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9" } variable "namespace_name" { description = "The name of the namespace." type = string default = "demo" } variable "namespace_id" { description = "The ID of the namespace." type = string default = "cn-shenzhen:demo" } # Create a namespace. resource "alicloud_sae_namespace" "default" { namespace_description = var.namespace_description namespace_id = var.namespace_id namespace_name = var.namespace_name } # Create a VPC. resource "alicloud_vpc" "default" { vpc_name = var.name cidr_block = "10.0.0.0/16" } # Create a 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 } # Create a 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 } # Configure the application. 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/Shanghai" replicas = var.replicas cpu = var.cpu memory = var.memory } # Configure the auto scaling policy. resource "alicloud_sae_application_scaling_rule" "default" { app_id = alicloud_sae_application.default.id scaling_rule_name = "hybrid-rule" 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:35" target_replicas = 10 } schedules { at_time = "20:35" target_replicas = 3 } } scaling_rule_metric { max_replicas = 40 min_replicas = 3 metrics { metric_type = "CPU" metric_target_average_utilization = 20 } scale_up_rules { step = 10 disabled = false stabilization_window_seconds = 0 } scale_down_rules { step = 10 disabled = false stabilization_window_seconds = 10 } } } # Define other variables. variable "namespace_description" { description = "The description of the namespace." default = "A sample namespace for Terraform." } variable "name" { default = "tf-example" description = "The name of the resource." type = string } variable "description" { default = "Created by Terraform for an example." description = "The description of the resource." type = string } variable "port_range" { default = "1/65535" description = "The port range of the security group rule." type = string } variable "cidr_ip" { description = "The CIDR block for the security group rule." type = string default = "0.0.0.0/0" } variable "zone_id" { description = "The ID of the availability zone." type = string default = "cn-shenzhen-e" } variable "app_description" { default = "An application 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 millicores." type = number } variable "memory" { default = 1024 description = "The memory of the application, in MB." type = number } variable "replicas" { default = 1 description = "The number of replicas for the application." type = number } variable "port" { description = "The port of the SLB instance." type = string default = "8000" } # Define outputs. output "namespace_id" { value = alicloud_sae_namespace.default.id description = "The ID of the namespace." } 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 Alibaba Cloud Terraform?.