Serverless 應用引擎 SAE(Serverless App Engine)是面嚮應用的Serverless PaaS平台,向上抽象了應用的概念。將應用部署至SAE後,您無需管理和維護叢集與伺服器,可以專註於設計和構建應用程式,將其部署在SAE。除通過控制台、API、外掛程式、CI/CD部署外,您還可以通過Terraform來部署SAE應用。本文介紹如何通過Terraform自動建立、自訂建立以及刪除SAE應用。
前提條件
由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作方式請參見建立RAM使用者和建立AccessKey。
為運行Terraform命令的RAM使用者綁定以下最小權限原則,以擷取管理本樣本所涉及資源的許可權。更多資訊,請參見為RAM使用者授權。
此自訂權限原則允許使用者管理和操作SAE中的應用程式和服務,包括描述、建立、更新、刪除、部署、啟動和停止應用程式,以及建立、更新、刪除、綁定和解除綁定服務。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "sae:DescribeApplications", "sae:DescribeApplication", "sae:DescribeInstances", "sae:DescribeInstance", "sae:DescribeServices", "sae:DescribeService" ], "Resource": "*" }, { "Effect": "Allow", "Action": [ "sae:CreateApplication", "sae:UpdateApplication", "sae:DeleteApplication", "sae:DeployApplication", "sae:StartApplication", "sae:StopApplication" ], "Resource": "acs:sae:*:*:application/*" }, { "Effect": "Allow", "Action": [ "sae:CreateService", "sae:UpdateService", "sae:DeleteService", "sae:BindService", "sae:UnbindService" ], "Resource": "acs:sae:*:*:service/*" } ] }準備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_application:用於管理和建立阿里雲 Serverless 應用引擎(SAE)應用的資源。
alicloud_vpc:通過 VPC,使用者可以完全控制自己的網路設定,包括子網劃分、路由表和網路安全設定等。
alicloud_vswitch:是VPC內的子網,用於將VPC劃分為多個子網,每個子網可以有自己的IP位址範圍和路由表。通過VSwitch,使用者可以在不同的子網中部署不同的應用和服務。
alicloud_security_group:是阿里雲的安全性群組服務,用於管理VPC內的網路存取控制。
建立應用
SAE支援鏡像部署和程式碼封裝部署。其中程式碼封裝支援JAR包、WAR包和PHP ZIP包。建立應用時,您可以按需選擇以下方式配置專用網路:
自動設定:SAE將自動幫您配置命名空間、VPC、vSwitch及安全性群組等。命名空間為預設命名空間。
自訂配置:您需要為建立的應用配置所需的命名空間、VPC、vSwitch及安全性群組等。
自動設定
本樣本以在華東2(北京)地區下建立應用為例,介紹如何通過鏡像方式自動部署應用。
- 建立一個用於存放Terraform資源的專案檔夾,命名為terraform。
- 執行以下命令,進入專案目錄。
cd terraform 建立名為main.tf的設定檔。內容如下。
provider "alicloud" { region = var.region } variable "region" { default = "cn-beijing" } variable "name" { default = "serverless-example" } resource "random_integer" "default" { max = 99999 min = 10000 } data "alicloud_regions" "default" { current = true } data "alicloud_zones" "default" { available_resource_creation = "VSwitch" } resource "alicloud_vpc" "default" { vpc_name = var.name cidr_block = "10.4.0.0/16" } resource "alicloud_vswitch" "default" { vswitch_name = var.name cidr_block = "10.4.0.0/24" vpc_id = alicloud_vpc.default.id zone_id = data.alicloud_zones.default.zones.0.id } resource "alicloud_security_group" "default" { vpc_id = alicloud_vpc.default.id } resource "alicloud_sae_namespace" "default" { namespace_id = "${data.alicloud_regions.default.regions.0.id}:example${random_integer.default.result}" namespace_name = var.name namespace_description = var.name enable_micro_registration = false } resource "alicloud_sae_application" "default" { app_description = var.name app_name = "${var.name}-${random_integer.default.result}" namespace_id = alicloud_sae_namespace.default.id image_url = "registry-vpc.${data.alicloud_regions.default.regions.0.id}.aliyuncs.com/sae-demo-image/consumer:1.0" package_type = "Image" security_group_id = alicloud_security_group.default.id vpc_id = alicloud_vpc.default.id vswitch_id = alicloud_vswitch.default.id timezone = "Asia/Beijing" replicas = "1" cpu = "500" memory = "2048" }您可以登入Container Registry控制台,在目標執行個體倉庫的基本資料頁面查看鏡像地址。格式如下
registry.<regionId>.aliyuncs.com/<命令空間名稱><鏡像倉庫名稱>:<鏡像版本號碼>- 執行以下命令,初始化配置。
terraform init
預期輸出:
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.依次執行以下命令,建立SAE應用。
執行以下命令,執行設定檔。在執行過程中,根據提示輸入
yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示授權完成。
terraform apply預期輸出:
Apply complete! Resources: 6 added, 0 changed, 0 destroyed.驗證結果
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料:
terraform show
Serverless應用引擎SAE控制台截圖
登入Serverless應用引擎SAE控制台,查看建立應用列表。

以下操作步驟範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
自訂配置:鏡像部署
本樣本以在華南1(深圳)地區下建立應用為例,介紹如何通過鏡像方式自訂部署應用。
- 建立一個用於存放Terraform資源的專案檔夾,命名為terraform。
- 執行以下命令,進入專案目錄。
cd terraform 建立名為main.tf的設定檔。內容如下。
provider "alicloud" { region = var.region_id } # 地區ID variable "region_id" { type = string default = "cn-shenzhen" } # 應用程式名稱 variable "app_name" { description = "The name of the application" type = string default = "manual-jar-tf" } # 應用描述 variable "app_description" { default = "description created by Terraform" description = "The description of the application" type = string } # 應用部署方式 variable "package_type" { default = "FatJar" description = "The package type of the application" type = string } # 執行個體CPU規格 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 } # JAR包地址 variable "jar_url" { description = "The JAR url of the application, like `oss://my-bucket/my-app.jar`" type = string default = "https://edas-sz.oss-cn-shenzhen.aliyuncs.com/prod/demo/SPRING_CLOUD_CONSUMER.jar" } # 應用執行個體數 variable "replicas" { default = "1" description = "The replicas of the application" type = string } # 命名空間名稱 variable "namespace_name" { description = "Namespace Name" type = string default = "tfjardemo" } # 命名空間ID variable "namespace_id" { description = "Namespace ID" type = string default = "cn-shenzhen:tfjardemo" # 引用現有的命名空間ID } # 命名空間描述 variable "namespace_description" { description = "Namespace Description" default = "a namespace" } # VPC和安全性群組 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 } # CIDR地址 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" # 選擇一個資源充足的可用性區域 } # 應用日誌採集到SLS variable "slsConfig" { default = "[{\"logDir\":\"\",\"logType\":\"stdout\"},{\"logDir\":\"/home/admin/logs/*.log\"}]" description = "The config of sls log collect" type = string } resource "alicloud_vpc" "vpc" { vpc_name = "tf-vpc" cidr_block = "172.16.0.0/16" } resource "alicloud_vswitch" "vswitch" { vpc_id = alicloud_vpc.vpc.id cidr_block = "172.16.1.0/24" zone_id = var.zone_id vswitch_name = "tf-vswitch" description = "tf-vswitch description" } resource "alicloud_sae_namespace" "default" { namespace_id = var.namespace_id namespace_name = var.namespace_name namespace_description = var.namespace_description } output "namespace_id" { value = var.namespace_id description = "Namespace ID" } resource "alicloud_security_group" "sg" { name = var.name description = var.description vpc_id = alicloud_vpc.vpc.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" "manual" { app_name = var.app_name app_description = var.app_description deploy = true package_url = var.jar_url namespace_id = alicloud_sae_namespace.default.id vswitch_id = alicloud_vswitch.vswitch.id vpc_id = alicloud_vpc.vpc.id security_group_id = alicloud_security_group.sg.id package_type = var.package_type jdk = "Open JDK 8" timezone = "Asia/Beijing" replicas = var.replicas cpu = var.cpu memory = var.memory micro_registration = "0" lifecycle { ignore_changes = [ micro_registration ] } } output "app_id" { description = "The id of the application" value = alicloud_sae_application.manual.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控制台
登入Serverless應用引擎SAE控制台,查看建立應用列表。


自訂配置:JAR包部署
本樣本以在華南1(深圳)地區下建立應用為例,介紹如何通過JAR包方式自訂部署應用。
- 建立一個用於存放Terraform資源的專案檔夾,命名為terraform。
- 執行以下命令,進入專案目錄。
cd terraform 建立名為main.tf的設定檔。內容如下。
provider "alicloud" { region = var.region_id } # 地區ID variable "region_id" { type = string default = "cn-shenzhen" } # 應用程式名稱 variable "app_name" { description = "The name of the application" type = string default = "manual-jar-tf" } # 應用描述 variable "app_description" { default = "description created by Terraform" description = "The description of the application" type = string } # 應用部署方式 variable "package_type" { default = "FatJar" description = "The package type of the application" type = string } # 執行個體CPU規格 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 } # JAR包地址 variable "jar_url" { description = "The JAR url of the application, like `oss://my-bucket/my-app.jar`" type = string default = "https://edas-sz.oss-cn-shenzhen.aliyuncs.com/prod/demo/SPRING_CLOUD_CONSUMER.jar" } # 應用執行個體數 variable "replicas" { default = "1" description = "The replicas of the application" type = string } # 命名空間名稱 variable "namespace_name" { description = "Namespace Name" type = string default = "tfjardemo" } # 命名空間ID variable "namespace_id" { description = "Namespace ID" type = string default = "cn-shenzhen:tfjardemo" # 引用現有的命名空間ID } # 命名空間描述 variable "namespace_description" { description = "Namespace Description" default = "a namespace" } # VPC和安全性群組 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 } # CIDR地址 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" # 選擇一個資源充足的可用性區域 } # 應用日誌採集到SLS variable "slsConfig" { default = "[{\"logDir\":\"\",\"logType\":\"stdout\"},{\"logDir\":\"/home/admin/logs/*.log\"}]" description = "The config of sls log collect" type = string } resource "alicloud_vpc" "vpc" { vpc_name = "tf-vpc" cidr_block = "172.16.0.0/16" } resource "alicloud_vswitch" "vswitch" { vpc_id = alicloud_vpc.vpc.id cidr_block = "172.16.1.0/24" zone_id = var.zone_id vswitch_name = "tf-vswitch" description = "tf-vswitch description" } resource "alicloud_sae_namespace" "default" { namespace_id = var.namespace_id namespace_name = var.namespace_name namespace_description = var.namespace_description } output "namespace_id" { value = var.namespace_id description = "Namespace ID" } resource "alicloud_security_group" "sg" { name = var.name description = var.description vpc_id = alicloud_vpc.vpc.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" "manual" { app_name = var.app_name app_description = var.app_description deploy = true package_url = var.jar_url namespace_id = alicloud_sae_namespace.default.id vswitch_id = alicloud_vswitch.vswitch.id vpc_id = alicloud_vpc.vpc.id security_group_id = alicloud_security_group.sg.id package_type = var.package_type jdk = "Open JDK 8" timezone = "Asia/Beijing" replicas = var.replicas cpu = var.cpu memory = var.memory micro_registration = "0" lifecycle { ignore_changes = [ micro_registration ] } } output "app_id" { description = "The id of the application" value = alicloud_sae_application.manual.id } output "app_name" { description = "The name of the application" value = var.app_name }執行以下命令,初始化配置。
terraform init預期輸出:

依次執行以下命令,以JAR包方式建立應用。
執行以下命令,部署應用。在執行過程中,根據提示輸入
yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示授權完成。terraform apply預期輸出:

如果輸出代碼符合預期輸出,說明已成功建立以JAR包部署的應用。
驗證結果:
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料:
terraform show
Serverless應用引擎SAE控制台
登入Serverless應用引擎SAE控制台,查看建立命名空間。

刪除應用
本文以在華東2(上海)地區下自動建立的應用auto-app-1為例,介紹如何刪除應用。
在目標專案目錄內執行以下命令,回合組態檔案。
terraform destroy預期結果:

相關文檔
Terrafrom介紹,請參見Terraform產品介紹。