Serverless 應用引擎 SAE(Serverless App Engine)的命名空間可以將您的應用從邏輯上進行劃分,例如測試環境、開發環境、預發環境和線上環境等。本文介紹如何通過Terraform建立、更新和刪除SAE命名空間。
本教程所含範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作方式請參見建立RAM使用者和建立AccessKey。
為運行Terraform命令的RAM使用者綁定以下最小權限原則,以擷取管理本樣本所涉及資源的許可權。更多資訊,請參見為RAM使用者授權。
該權限原則允許使用者管理SAE命名空間,包括建立、刪除、更新、查看和列出命名空間。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "sae:CreateNamespace", "sae:DeleteNamespace", "sae:UpdateNamespace", "sae:GetNamespace", "sae:ListNamespaces" ], "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的命名空間。
建立命名空間
本樣本以在華東1(杭州)地區下建立命名空間為例,命名空間名稱為admin、命名空間ID為cn-hangzhou:admin。
- 建立一個用於存放Terraform資源的專案檔夾,命名為terraform。
- 執行以下命令,進入專案目錄。
cd terraform 執行以下命令,建立名為main.tf的設定檔。
provider "alicloud" { region = var.region_id } # 定義地區變數,預設值為 cn-hangzhou variable "region_id" { type = string default = "cn-hangzhou" } # 定義命名空間描述變數,預設值為 "a namespace sample" variable "namespace_description" { description = "Namespace Description" default = "a namespace sample" } # 定義命名空間名稱變數,預設值為 "admin" variable "namespace_name" { description = "Namespace Name" type = string default = "admin" } # 定義命名空間ID變數,預設值為 "cn-hangzhou:admin" variable "namespace_id" { description = "Namespace ID" type = string default = "cn-hangzhou:admin" } resource "alicloud_sae_namespace" "default" { namespace_description = var.namespace_description namespace_id = var.namespace_id namespace_name = var.namespace_name } output "namespace_id" { value = alicloud_sae_namespace.default.namespace_id description = "The ID of the created namespace." }執行以下命令,初始化Terraform運行環境。
terraform init預期輸出:
Initializing the backend... Initializing provider plugins... - Checking for available provider plugins... - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.233.0... The following providers do not have any version constraints in configuration, so the latest version was installed. To prevent automatic upgrades to new major versions that may contain breaking changes, it is recommended to add version = "..." constraints to the corresponding provider blocks in configuration, with the constraint strings suggested below. * provider.alicloud: version = "~> 1.233" Warning: registry.terraform.io: For users on Terraform 0.13 or greater, this provider has moved to aliyun/alicloud. Please update your source in required_providers. 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: 1 added, 0 changed, 1 destroyed. Outputs: namespace_id = cn-hangzhou:admin
命名空間已成功建立。
驗證結果
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料:
terraform show
Serverless應用引擎SAE控制台
登入Serverless應用引擎SAE控制台,查看建立命名空間。

更新命名空間
本樣本以更新命名空間名稱為例,將華東1(杭州)地區下命名空間的名稱從admin更新為prod。
開啟 main.tf 檔案,將 namespace_name 變數的預設值從 "admin" 修改為 "prod"。
provider "alicloud" {
region = var.region_id
}
# 定義地區變數,預設值為 cn-hangzhou
variable "region_id" {
type = string
default = "cn-hangzhou"
}
# 定義命名空間描述變數,預設值為 "a namespace sample"
variable "namespace_description" {
description = "Namespace Description"
default = "a namespace sample"
}
# 修改定義命名空間名稱變數為 "prod"
variable "namespace_name" {
description = "Namespace Name"
type = string
default = "prod"
}
# 定義命名空間ID變數,預設值為 "cn-hangzhou:dev"
variable "namespace_id" {
description = "Namespace ID"
type = string
default = "cn-hangzhou:admin"
}
resource "alicloud_sae_namespace" "default" {
namespace_description = var.namespace_description
namespace_id = var.namespace_id
namespace_name = var.namespace_name
}
output "namespace_id" {
value = alicloud_sae_namespace.default.namespace_id
description = "The ID of the created namespace."
}執行如下命令,初始化Terraform運行環境。
terraform init返回資訊如下,Terraform初始化成功。
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.繼續運行以下 命令來應用更改,在執行過程中,根據提示輸入 yes 並按下 Enter 鍵,等待命令執行完成。
terraform apply預期輸出:
alicloud_sae_namespace.default: Modifying... [id=cn-hangzhou:admin]
alicloud_sae_namespace.default: Modifications complete after 1s [id=cn-hangzhou:admin]
Apply complete! Resources: 0 added, 1 changed, 0 destroyed.
Outputs:
namespace_id = "cn-hangzhou:admin"命名空間名稱已成功更新為prod。
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料:
terraform show
Serverless應用引擎SAE控制台
登入Serverless應用引擎SAE控制台,查看修改後的命名空間。

刪除命名空間
本樣本以在華東1(杭州)地區下刪除命名空間為例,命名空間名稱為prod、命名空間ID為cn-hangzhou:admin。
在目標專案目錄內執行以下命令,回合組態檔案。關於
terraform destroy的更多資訊,請參見常用命令。terraform destroy預期輸出:
alicloud_sae_namespace.default: Refreshing state... [id=cn-hangzhou:admin] 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_namespace.default will be destroyed - resource "alicloud_sae_namespace" "default" { - enable_micro_registration = true -> null - id = "cn-hangzhou:admin" -> null - namespace_description = "a namespace sample" -> null - namespace_id = "cn-hangzhou:admin" -> null - namespace_name = "prod" -> null - namespace_short_id = "admin" -> null } Plan: 0 to add, 0 to change, 1 to destroy. Changes to Outputs: - namespace_id = "cn-hangzhou:admin" -> null Do you really want to destroy all resources? Terraform will destroy all your managed infrastructure, as shown above. There is no undo. Only 'yes' will be accepted to confirm. Enter a value: yes alicloud_sae_namespace.default: Destroying... [id=cn-hangzhou:admin] alicloud_sae_namespace.default: Destruction complete after 1s Destroy complete! Resources: 1 destroyed.命名空間已成功刪除。
完整樣本
相關文檔
Terrafrom介紹,請參見Terraform產品介紹。
ROS提供了Terraform託管服務,因此您可以直接在ROS控制台部署Terraform模板。詳細操作,請參見建立Terraform類型資源棧。