在分布式應用管理中,Auto Scaling是很重要的營運能力,它能夠根據執行個體狀態自動增加或減少執行個體數量,即擴容或縮容,從而提高資源使用率、降低資源成本。本文介紹如何通過Terraform為SAE應用啟停彈性策略。
背景資訊
SAE自動Auto Scaling策略包括定時Auto Scaling策略、監控指標Auto Scaling策略和混合Auto Scaling策略。通過Terraform為SAE應用啟用彈性策略的實質,是在建立應用時配置alicloud_sae_application_scaling_rule資源。如需停用彈性策略,您需要同時刪除彈性策略和應用。關於如何設定時間周期,請參見使用Crontab運算式。
適用情境
定時彈性策略:適用於資源使用率有周期性規律的應用情境,多用於證券、醫學、政府和教育等行業。
監控指標彈性策略:適用於突發流量和典型周期性流量的應用情境,多用於互連網、遊戲和社交平台等行業。
混合彈性策略:適用於兼備資源使用率有周期性規律和有突發流量、典型周期性流量的應用情境,多用於互連網、教育和餐飲等行業。
更多資訊,請參見配置Auto Scaling策略。
當前範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作方式請參見建立RAM使用者和建立AccessKey。
為運行Terraform命令的RAM使用者綁定以下最小權限原則,以擷取管理本樣本所涉及資源的許可權。更多資訊,請參見通過指令碼編輯模式建立自訂權限原則。
該自訂權限原則允許RAM使用者或角色對Serverless Application Engine (SAE) 應用程式進行全生命週期管理,包括建立、更新、刪除、啟動、停止、部署、復原以及管理自動調整規則。
{ "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": "*" } ] }準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。
ROS提供了Terraform託管服務,因此您可以直接在ROS控制台部署Terraform模板。詳細操作,請參見建立Terraform類型資源棧。
在Terraform Explorer中使用Terraform:阿里雲提供了Terraform的線上運行環境,您無需安裝Terraform,登入後即可線上使用和體驗Terraform。適用於零成本、快速、便捷地體驗和調試Terraform的情境。
Cloud Shell:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。
在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。
使用的資源
alicloud_sae_namespace:建立一個阿里雲 SAE 命名空間。
alicloud_security_group:建立一個阿里雲安全性群組。
alicloud_security_group_rule:建立一個阿里雲安全性群組規則。
alicloud_sae_application:建立一個阿里雲 SAE 應用。
alicloud_sae_application_scaling_rule:建立一個阿里雲 SAE 應用的Auto Scaling策略。
啟用定時彈性策略
本樣本以在華南1(深圳)地區下建立應用為例,介紹如何通過自訂配置,以鏡像方式部署應用並為應用配置一條定時彈性策略。
彈性策略內容如下:周期設為每天,第一段觸發點的開始時間為19:35,目標執行個體數為5個;第二段觸發點的開始時間為20:35,目標執行個體數為2個。則在19:35至20:35之間,SAE依據所設的規則,將該應用的執行個體數保持為5個,在20:35至次日19:35之間,應用執行個體數保持為2個。
建立一個用於存放Terraform資源的專案檔夾,命名為terraform。
執行以下命令,進入專案目錄。
cd terraform建立名為main.tf的設定檔。
# 供應商配置 provider "alicloud" { region = var.region_id } # 變數定義 variable "region_id" { type = string default = "cn-shenzhen" } variable "app_name" { description = "應用程式名稱" type = string default = "app-scaling" } variable "image_url" { description = "鏡像地址" type = string default = "registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9" } variable "namespace_name" { description = "命名空間名稱" type = string default = "demo" } variable "namespace_id" { description = "命名空間ID" type = string default = "cn-shenzhen:demo" } # 命名空間 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 } # 安全性群組 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 } # 應用配置 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策略 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 } } } # 其他變數定義 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" } # 輸出 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 }執行以下命令,初始化配置。
terraform init預期結果:
依次執行以下命令,建立應用。
執行以下命令,部署應用。在執行過程中,根據提示輸入
yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示授權完成。
terraform apply預期結果:
結果驗證
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料:
terraform show
Serverless應用引擎SAE控制台
已成功建立啟用了彈性定時策略的應用app-scaling。您可以登入SAE控制台,在目標應用基本資料頁面的執行個體部署資訊頁簽查看彈性策略與應用執行個體的運行狀態。

啟用監控指標彈性策略
本樣本基於配置了定時彈性策略的main.tf檔案,僅將資源alicloud_sae_application_scaling_rule的內容從定時彈性策略替換為監控指標彈性策略,其餘內容保持不變。具體操作,請參見啟用定時彈性策略。
# Auto Scaling策略
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
}
}
}本樣本的彈性策略內容如下:當CPU使用率超過1%時擴容,應用最大執行個體數為50個;當CPU使用率低於1%時縮容,應用最小執行個體數為3個。
範例程式碼為顯示測試效果,設定了較低的CPU使用率,您在實際操作過程中可以按需設定合理的參數值。
執行以下命令,初始化配置。
terraform init預期結果:
依次執行以下命令,建立應用。
執行以下命令,部署應用。在執行過程中,根據提示輸入
yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示授權完成。
terraform apply預期結果:
結果驗證
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料:
terraform show
Serverless應用引擎SAE控制台
已成功建立啟用了彈性定時策略的應用app-scaling。您可以登入SAE控制台,在目標應用基本資料頁面的執行個體部署資訊頁簽查看彈性策略與應用執行個體的運行狀態。

啟用混合彈性策略
本樣本基於配置了定時彈性策略的main.tf檔案,僅將資源alicloud_sae_application_scaling_rule的內容從定時彈性策略替換為混合彈性策略,其餘內容保持不變。具體操作,請參見啟用定時彈性策略。
範例程式碼如下:
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
}
}
}本樣本的彈性策略內容如下:
一般時間段:當CPU使用率超過1%時擴容,應用最大執行個體數為40個;當CPU使用率低於1%時縮容,應用最小執行個體數為3個。
特殊時間段:基於CPU閾值,在2024年11月26日至2022年11月30日執行定時Auto Scaling策略。
在19:45至20:45之間,應用最小執行個體數為10個,應用最大執行個體數為50個。
在20:45至次日20:45之間,應用最小執行個體數為3個,應用最大執行個體數為40個。
範例程式碼為顯示測試效果,設定了較低的CPU使用率,您在實際操作過程中可以按需設定合理的參數值。
執行以下命令,初始化配置。
terraform init預期結果:
依次執行以下命令,建立應用。
執行以下命令,部署應用。在執行過程中,根據提示輸入
yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示授權完成。
terraform apply預期結果:
結果驗證
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料:
terraform show
Serverless應用引擎SAE控制台
已成功建立啟用了彈性定時策略的應用app-scaling。您可以登入SAE控制台,在目標應用基本資料頁面的執行個體部署資訊頁簽查看彈性策略與應用執行個體的運行狀態。

刪除彈性策略和應用
本樣本以在華東1(杭州)地區下的應用app-scaling為例,介紹如何停用彈性策略並刪除應用。
在目標專案目錄內執行以下命令,回合組態檔案。
terraform destroy預期結果:

已成功停用彈性策略並刪除應用
app-scaling。完整代碼
說明當前範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
# 供應商配置 provider "alicloud" { region = var.region_id } # 變數定義 variable "region_id" { type = string default = "cn-shenzhen" } variable "app_name" { description = "應用程式名稱" type = string default = "app-scaling" } variable "image_url" { description = "鏡像地址" type = string default = "registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9" } variable "namespace_name" { description = "命名空間名稱" type = string default = "demo" } variable "namespace_id" { description = "命名空間ID" type = string default = "cn-shenzhen:demo" } # 命名空間 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 } # 安全性群組 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 } # 應用配置 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策略 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 } } } # 其他變數定義 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" } # 輸出 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 }相關文檔
Terrafrom介紹,請參見Terraform產品介紹。