Serverless 應用引擎 SAE(Serverless App Engine)是面向應用的Serverless PaaS平臺,向上抽象了應用的概念。將應用部署至SAE后,您無需管理和維護集群與服務器,可以專注于設計和構建應用程序,將其部署在SAE。除通過控制臺、API、插件、CI/CD部署外,您還可以通過Terraform來部署SAE應用。本文介紹如何通過Terraform自動創建、自定義創建以及刪除SAE應用。
前提條件
- 安裝Terraform。支持Terraform 0.13及以上版本。
- 配置阿里云賬號信息。
選擇一種阿里云認證方式,為Terraform的執行提供認證信息。本文以環境變量認證方式為例:
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-hangzhou"
說明 為保障數據安全性,建議您按需為RAM用戶授予SAE資源的操作權限。具體操作,請參見為RAM用戶授權。
背景信息
Terraform的alicloud_sae_application資源提供一系列參數來管理SAE應用。本文通過介紹創建和刪除應用的多種方式,說明部分參數的用法,展示如何通過Terraform管理云資源。更多信息,請參見alicloud_sae_application。
目前不支持通過Terraform更新應用。
創建應用
SAE支持鏡像部署和代碼包部署。其中代碼包支持JAR包、WAR包和PHP ZIP包。創建應用時,您可以按需選擇以下方式配置專有網絡:
自動配置:SAE將自動幫您配置命名空間、VPC、vSwitch及安全組等。命名空間為默認命名空間。
自定義配置:您需要為創建的應用配置所需的命名空間、VPC、vSwitch及安全組等。
自動配置
本示例以在華東1(杭州)地域下創建應用為例,介紹如何通過鏡像方式自動部署應用。
- 創建一個用于存放Terraform資源的項目文件夾,命名為terraform。
- 執行以下命令,進入項目目錄。
cd terraform
創建名為main.tf的配置文件。內容如下。
terraform { required_providers { alicloud = { source = "hashicorp/alicloud" version = "1.156.0" } } } resource "alicloud_sae_application" "auto" { count = 1 app_name = var.app_name app_description = var.app_description auto_config = true image_url = var.image_url package_type = var.package_type timezone = "Asia/Beijing" replicas = var.replicas cpu = var.cpu memory = var.memory } # 應用名稱 variable "app_name" { description = "The name of the application" type = string } # 應用描述 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 } # 實例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 } # 鏡像地址 variable "image_url" { description = "The image url of the application, like `registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9`" type = string } # 應用實例數 variable "replicas" { default = "1" description = "The replicas of the application" type = string } output "app_id" { description = "The id of the application" value = alicloud_sae_application.auto.0.id } output "app_name" { description = "The name of the application" value = var.app_name }
- 執行以下命令,初始化配置。
terraform init
依次執行以下命令,創建SAE應用。
執行以下命令,執行配置文件。
terraform apply
根據提示依次輸入應用的各項信息。
app_name:應用名稱。輸入
auto-app-1
。image_url:鏡像地址。輸入
registry.cn-hangzhou.aliyuncs.com/****/****:01
。您可以登錄容器鏡像服務控制臺,在目標實例倉庫的基本信息頁面查看鏡像地址。格式如下:
registry.<regionId>.aliyuncs.com/<命令空間名稱>/<鏡像倉庫名稱>:<鏡像版本號>
預期輸出:
... Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + app_id = (known after apply) + app_name = "auto-app-1" alicloud_sae_application.auto[0]: Creating... ... alicloud_sae_application.auto[0]: Creation complete after 59s [id=f8e2f217-8788-41e0-85d1-ce96105b****] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: app_id = "f8e2f217-8788-41e0-85d1-ce96105b****" app_name = "auto-app-1"
如果輸出代碼符合預期輸出,說明已成功創建以鏡像部署的應用。
自定義配置:鏡像部署
本示例以在華東1(杭州)地域下創建應用為例,介紹如何通過鏡像方式自定義部署應用。
- 創建一個用于存放Terraform資源的項目文件夾,命名為terraform。
- 執行以下命令,進入項目目錄。
cd terraform
創建名為main.tf的配置文件。內容如下。
terraform { required_providers { alicloud = { source = "hashicorp/alicloud" version = "~> 1.163.0" } } } # 命名空間信息 resource "alicloud_sae_namespace" "default" { namespace_description = var.namespace_description namespace_id = var.namespace_id namespace_name = var.namespace_name } # 命名空間描述 variable "namespace_description" { description = "Namespace Description" default = "a namespace" } # 命名空間名稱 variable "namespace_name" { description = "Namespace Name" type = string } # 命名空間ID variable "namespace_id" { description = "Namespace ID" type = string } output "namespace_id" { value = var.namespace_id description = "Namespace ID" } # VPC和安全組 resource "alicloud_security_group" "sg" { name = var.name description = var.description vpc_id = module.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 } module "vpc" { source = "git::github.com/kubevela-contrib/terraform-modules.git//alibaba/vswitch" zone_id = var.zone_id } 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-hongkong-b" } resource "alicloud_sae_application" "manual" { 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 = module.vpc.VSWITCH_ID vpc_id = module.vpc.VPC_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 # 環境變量設置 envs="[{'name':'envtmp','value':'0'},{'name':'envtmp2','value':'0'}]" # 自定義Host映射 custom_host_alias = "[{hostName:'samplehost',ip:'127.0.0.1'},{'hostName':'example.com','ip':'128.0.X.X'}]" # 應用健康檢查 liveness = "{'httpGet':{'path':'/','port':80,'scheme':'HTTP'},'initialDelaySeconds':20,'periodSeconds':10,'timeoutSeconds':1}" readiness = "{'httpGet':{'path':'/','port':80,'scheme':'HTTP'},'initialDelaySeconds':20,'periodSeconds':10,'timeoutSeconds':1}" # 容器啟動后執行腳本 post_start ="{'exec':{'command':['sh','-c','echo hello > /tmp/hello.txt']}}" # 容器停止前執行腳本 pre_stop = "{'exec':{'command':['sh','-c','echo hello']}}" # 優雅下線超時時間 termination_grace_period_seconds = 50 # 自動開啟應用彈性策略 auto_enable_application_scaling_rule = true # 最小存活實例數 min_ready_instances = 1 } # 應用日志采集到SLS variable "slsConfig" { default = "[{"logDir":"","logType":"stdout"},{"logDir":"/home/admin/logs/*.log"}]" description = "The config of sls log collect" type = string } # 應用名稱 variable "app_name" { description = "The name of the application" type = string } # 應用描述 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 } # 實例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 } # 鏡像地址 variable "image_url" { description = "The image url of the application, like `registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-slim:0.9`" type = string } # 應用實例數 variable "replicas" { default = "1" description = "The replicas of the application" type = string } 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
依次執行以下命令,以鏡像方式創建應用。
執行以下命令,部署應用。
terraform apply
根據提示依次輸入應用的各項信息。
app_name:應用名稱。輸入
manual-image
。namespace_name:命名空間名稱。輸入
demo
。namespace_id:命名空間ID。輸入
cn-hangzhou:demo
。image_url:鏡像地址。輸入
registry.cn-hangzhou.aliyuncs.com/****/****:01
。您可以登錄容器鏡像服務控制臺,在目標實例倉庫的基本信息頁面查看鏡像地址。格式如下:
registry.<regionId>.aliyuncs.com/<命令空間名稱>/<鏡像倉庫名稱>:<鏡像版本號>
預期輸出:
... Plan: 6 to add, 0 to change, 0 to destroy. Changes to Outputs: + app_id = (known after apply) + app_name = "manual-image" + namespace_id = "cn-hangzhou:demo" alicloud_sae_namespace.default: Creating... ... alicloud_sae_application.manual: Creation complete after 1m58s [id=af34c033-fc5a-4147-8baf-87c71d3a****] Apply complete! Resources: 6 added, 0 changed, 0 destroyed. Outputs: app_id = "af34c033-fc5a-4147-8baf-87c71d3a****" app_name = "manual-image" namespace_id = "cn-hangzhou:demo"
如果輸出代碼符合預期輸出,說明已成功創建以鏡像部署的應用。
自定義配置:JAR包部署
本示例以在華東1(杭州)地域下創建應用為例,介紹如何通過JAR包方式自定義部署應用。
- 創建一個用于存放Terraform資源的項目文件夾,命名為terraform。
- 執行以下命令,進入項目目錄。
cd terraform
創建名為main.tf的配置文件。內容如下。
terraform { required_providers { alicloud = { source = "hashicorp/alicloud" version = "~> 1.163.0" } } } # 命名空間 resource "alicloud_sae_namespace" "default" { namespace_description = var.namespace_description namespace_id = var.namespace_id namespace_name = var.namespace_name } # 命名空間描述 variable "namespace_description" { description = "Namespace Description" default = "a namespace" } # 命名空間名稱 variable "namespace_name" { description = "Namespace Name" type = string } # 命名空間ID variable "namespace_id" { description = "Namespace ID" type = string } output "namespace_id" { value = var.namespace_id description = "Namespace ID" } # VPC和安全組 resource "alicloud_security_group" "sg" { name = var.name description = var.description vpc_id = module.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 } module "vpc" { source = "git::github.com/kubevela-contrib/terraform-modules.git//alibaba/vswitch" zone_id = var.zone_id } 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 } # 安全組IP地址 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-hongkong-b" } resource "alicloud_sae_application" "manual" { app_name = var.app_name app_description = var.app_description deploy = true package_version = "12132111" package_url = "https://****.oss-ap-southeast-1.aliyuncs.com/javacommon-0.0.1-SNAPSHOT.jar" jdk = "Open JDK 8" namespace_id = alicloud_sae_namespace.default.id vswitch_id = module.vpc.VSWITCH_ID vpc_id = module.vpc.VPC_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 } # 應用名稱 variable "app_name" { description = "The name of the application" type = string } # 應用描述 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 } # 應用實例數 variable "replicas" { default = "1" description = "The replicas of the application" type = string } 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包方式創建應用。
執行以下命令,部署應用。
terraform apply
根據提示依次輸入應用的各項信息。
app_name:應用名稱。輸入
manual-jar
。namespace_name:命名空間名稱。輸入
demo
。namespace_id:命名空間ID。輸入
cn-hangzhou:demo
。
預期輸出:
Plan: 6 to add, 0 to change, 0 to destroy. Changes to Outputs: + app_id = (known after apply) + app_name = "manual-jar" + namespace_id = "cn-hangzhou:demo" alicloud_sae_namespace.default: Creating... ... alicloud_sae_application.manual: Creation complete after 1m46s [id=724b681e-e9e4-4891-b426-f16d7b78****] Apply complete! Resources: 6 added, 0 changed, 0 destroyed. Outputs: app_id = "724b681e-e9e4-4891-b426-f16d7b78****" app_name = "manual-jar" namespace_id = "cn-hangzhou:demo"
如果輸出代碼符合預期輸出,說明已成功創建以JAR包部署的應用。
刪除應用
本文以在華東1(杭州)地域下自動創建的應用auto-app-1
為例,介紹如何刪除應用。
- 在目標項目目錄內執行以下命令,運行配置文件。
terraform destroy
根據提示依次輸入應用的各項信息,刪除應用。
app_name:輸入
auto-app-1
。image_url:輸入
registry.cn-hangzhou.aliyuncs.com/****/****:01
。鏡像地址格式如下:
registry.<regionId>.aliyuncs.com/<命令空間名稱>/<鏡像倉庫名稱>:<鏡像版本號>
您可以登錄容器鏡像服務控制臺,在目標實例倉庫的基本信息頁面,查看鏡像地址。
預期輸出:
alicloud_sae_application.auto[0]: Refreshing state... [id=599a843b-f11d-456e-b934-dc9fdf99****] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: - destroy Terraform will perform the following actions: # alicloud_sae_application.auto[0] will be destroyed - resource "alicloud_sae_application" "auto" { - app_description = "description created by Terraform" -> null - app_name = "auto-app-1" -> null ... } Plan: 0 to add, 0 to change, 1 to destroy. Changes to Outputs: - app_id = "599a843b-f11d-456e-b934-dc9fdf99****" -> null - app_name = "auto-app-1" -> null alicloud_sae_application.auto[0]: Destroying... [id=599a843b-f11d-456e-b934-dc9fdf99****] alicloud_sae_application.auto[0]: Destruction complete after 5s Destroy complete! Resources: 1 destroyed.
如果輸出代碼符合預期輸出,說明已成功刪除應用
auto-app-1
。