Serverless App Engine (SAE) namespaces are used to logically isolate your applications. For example, you can use namespaces to isolate your applications in the test environment, development environment, staging environment, and online environment. This topic describes how to use Terraform to create, update, and delete an SAE namespace.
Prerequisites
- Terraform is installed.
- Method 1: Use Terraform in Cloud Shell.
- Method 2: Install and configure Terraform in the local PC.
- The information about your Alibaba Cloud account is configured.
Select an Alibaba Cloud authentication method to provide the authentication information that is required by Terraform. The following example shows how to use environment variables to perform authentication:
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-hangzhou"
Note To ensure data security, we recommend that you grant a RAM user the permissions to manage SAE resources based on your business requirements. For more information, see Grant permissions to a RAM user.
Background information
The alicloud_sae_namespace resource of Terraform includes the following parameters:
- Required:namespace_id
The ID of the namespace. Format:
<RegionId>:<namespace_name>
. Example:cn-hangzhou:dev
. Each time you update the parameter, a new namespace is created. For information about the RegionId variable values that are supported by SAE, see DescribeRegions. - Required:namespace_name
The name of the namespace.
- Optional:namespace_description
The description of the namespace.
For more information, see alicloud_sae_namespace.
Create a namespace
In this example, a namespace is created in the China (Hangzhou) region. The name of the namespace is dev
and the ID of the namespace is cn-hangzhou:dev
.
- Create a project folder named terraform for storing Terraform resources.
- Run the following command to go to the project directory:
cd terraform
- Run the following command to create a configuration file named main.tf:
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 sample" } variable "namespace_name" { description = "Namespace Name" type = string } variable "namespace_id" { description = "Namespace ID" type = string } output "namespace_id" { value = var.namespace_id description = "Namespace ID" }
- Run the following command to initialize the runtime environment for Terraform:
terraform init
Expected output:Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/alicloud... - Installing hashicorp/alicloud v1.163.0... - Installed hashicorp/alicloud v1.163.0 (signed by HashiCorp) Terraform has created a lock file .terraform.lock.hcl to record the provider selections it made above. Include this file in your version control repository so that Terraform can guarantee to make the same selections by default when you run "terraform init" in the future. 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 sequence to create an SAE namespace.
The namespace is created.
Update a namespace
In this example, the name of a namespace that resides in the China (Hangzhou) region is changed from dev
to prod
.
- Run the following command to execute the configuration file:
terraform apply
- Enter the namespace ID and namespace name in sequence as prompted to update the SAE namespace name.
- namespace_id: Enter
cn-hangzhou:dev
. - namespace_name: Enter
prod
.
Expected output:... alicloud_sae_namespace.default: Refreshing state... [id=cn-hangzhou:dev] Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: ~ update in-place Terraform will perform the following actions: # alicloud_sae_namespace.default will be updated in-place ~ resource "alicloud_sae_namespace" "default" { id = "cn-hangzhou:dev" ~ namespace_name = "dev" -> "prod" # (2 unchanged attributes hidden) } Plan: 0 to add, 1 to change, 0 to destroy. ... alicloud_sae_namespace.default: Modifying... [id=cn-hangzhou:dev] alicloud_sae_namespace.default: Modifications complete after 1s [id=cn-hangzhou:dev] Apply complete! Resources: 0 added, 1 changed, 0 destroyed. Outputs: namespace_id = "cn-hangzhou:dev"
The namespace name is changed toprod
. - namespace_id: Enter
Delete a namespace
In this example, a namespace that resides in the China (Hangzhou) region is deleted. The name of the namespace is prod
and the ID of the namespace is cn-hangzhou:dev
.
- Run the following command in the project directory to execute the configuration file:
terraform destroy
- Enter the namespace ID and namespace name in sequence as prompted to delete the SAE namespace.
- namespace_id: Enter
cn-hangzhou:dev
. - namespace_name: Enter
prod
.
Expected output:... alicloud_sae_namespace.default: Refreshing state... [id=cn-hangzhou:dev] 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" { - id = "cn-hangzhou:dev" -> null - namespace_description = "a namespace sample" -> null - namespace_id = "cn-hangzhou:dev" -> null - namespace_name = "prod" -> null } Plan: 0 to add, 0 to change, 1 to destroy. Changes to Outputs: - namespace_id = "cn-hangzhou:dev" -> null ... alicloud_sae_namespace.default: Destroying... [id=cn-hangzhou:dev] alicloud_sae_namespace.default: Destruction complete after 4s Destroy complete! Resources: 1 destroyed.
The namespace is deleted. - namespace_id: Enter