All Products
Search
Document Center

Serverless App Engine:Use Terraform to manage SAE namespaces

Last Updated:Jun 16, 2026

A Serverless App Engine (SAE) namespace allows you to logically divide your applications into environments such as testing, development, staging, and production. You can use Terraform to create, update, and delete SAE namespaces.

Note

The sample code in this tutorial can be run directly in Terraform Explorer.

Prerequisites

  • Your Alibaba Cloud account has full permissions over all its resources. Leaked credentials for this account pose a significant security risk. We recommend using a RAM user and creating a dedicated AccessKey pair for that user. For more information, see Create a RAM user and Create an AccessKey.

  • Attach the following minimum policy to your RAM user to grant the permissions required for this tutorial. For more information, see Manage RAM user permissions.

    This policy grants permissions to create, delete, update, view, and list SAE namespaces.

    {
        "Version": "1",
        "Statement": [
            {
                "Effect": "Allow",
                "Action": [
                    "sae:CreateNamespace",
                    "sae:DeleteNamespace",
                    "sae:UpdateNamespace",
                    "sae:GetNamespace",
                    "sae:ListNamespaces"
                ],
                "Resource": "*"
            }
        ]
    }
  • Prepare a Terraform runtime environment. You can use one of the following methods:

    • ROS provides a managed Terraform service. You can directly deploy Terraform templates on the ROS console. For more information, see Create a Terraform stack.

    • Use Terraform in Terraform Explorer: Alibaba Cloud provides an online runtime environment for Terraform. You can log on to test Terraform without needing to install it. This method is ideal for quick and convenient testing at no cost.

    • Use Terraform to quickly create resources: Alibaba Cloud Shell comes with Terraform pre-installed and authentication credentials pre-configured. You can run Terraform commands directly in Cloud Shell. This method provides a quick, convenient, and low-cost way to use Terraform.

    • Install and configure Terraform on your local machine: This method is suitable for scenarios with poor network connectivity or when a custom development environment is required.

Resources

Create a namespace

The following example creates a namespace named admin with the ID cn-hangzhou:admin in the China (Hangzhou) region.

  1. Create a project folder named terraform for storing Terraform resources.
  2. Run the following command to go to the project directory:
    cd terraform
  3. Create a configuration file named main.tf.

    provider "alicloud" {
      region = var.region_id
    }
    # Define a variable for the region. The default value is cn-hangzhou.
    variable "region_id" {
      type    = string
      default = "cn-hangzhou"
    }
    # Define a variable for the namespace description. The default value is "a namespace sample".
    variable "namespace_description" {
      description = "Namespace Description"
      default     = "a namespace sample"
    }
    # Define a variable for the namespace name. The default value is "admin".
    variable "namespace_name" {
      description = "Namespace Name"
      type        = string
      default     = "admin"
    }
    # Define a variable for the namespace ID. The default value is "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."
    }
  4. Initialize the Terraform runtime environment.

    terraform init
  5. Expected output:

    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.
  6. Run the following command to create the SAE namespace.

    1. Run the command to apply the configuration. When prompted, enter yes and press Enter.

      terraform apply

      Expected output:

      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
      Outputs:
      namespace_id = cn-hangzhou:admin

    The namespace is created.

  7. Verify that the namespace was created.

Terraform show command

Run the following command to inspect the Terraform-managed resource state:

terraform show
shell@Alicloud:~/serverlesstest$ terraform show
# alicloud_sae_namespace.default:
resource "alicloud_sae_namespace" "default" {
    enable_micro_registration = true
    id                        = "cn-hangzhou:admin"
    namespace_description     = "a namespace sample"
    namespace_id              = "cn-hangzhou:admin"
    namespace_name            = "admin"
    namespace_short_id        = "admin"
}
Outputs:
namespace_id = "cn-hangzhou:admin"

SAE console

Log on to the Serverless App Engine (SAE) console to view the created namespace.

On the Namespaces page, verify that the namespace admin is listed with the ID cn-hangzhou:admin.

Update the namespace

The following example updates the namespace name in the China (Hangzhou) region from admin to prod.

  1. Open the main.tf file and change the default value of the namespace_name variable from "admin" to "prod".

provider "alicloud" {
  region = var.region_id
}
# Define a variable for the region. The default value is cn-hangzhou.
variable "region_id" {
  type    = string
  default = "cn-hangzhou"
}
# Define a variable for the namespace description. The default value is "a namespace sample".
variable "namespace_description" {
  description = "Namespace Description"
  default     = "a namespace sample"
}
# Change the default namespace name to "prod".
variable "namespace_name" {
  description = "Namespace Name"
  type        = string
  default     = "prod"
}
# Define a variable for the namespace ID. The default value is "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."
}
  1. Initialize the Terraform runtime environment.

    terraform init

    The output confirms that Terraform has been initialized.

    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.
  2. Run the following command to apply the changes. When prompted, enter yes and press Enter.

terraform apply

Expected output:

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"

The namespace name is updated to prod.

Terraform show command

Run the following command to inspect the Terraform-managed resource state:

terraform show
shell@Alicloud:~/serverlesstest$ terraform show
# alicloud_sae_namespace.default:
resource "alicloud_sae_namespace" "default" {
    enable_micro_registration = true
    id                       = "cn-hangzhou:admin"
    namespace_description    = "a namespace sample"
    namespace_id             = "cn-hangzhou:admin"
    namespace_name           = "prod"
    namespace_short_id       = "admin"
}
Outputs:
namespace_id = "cn-hangzhou:admin"

SAE console

Log on to the Serverless App Engine (SAE) console to view the updated namespace.

The namespace list now displays the namespace as prod with the ID cn-hangzhou:admin, confirming the update.

Delete the namespace

The following example deletes the namespace named prod with the ID cn-hangzhou:admin from the China (Hangzhou) region.

  1. In the project directory, run the following command to destroy the namespace. For more information about the terraform destroy command, see Common commands.

    terraform destroy

    Expected output:

    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.

    The namespace is deleted.

Complete example

Note

The sample code in this tutorial can be run directly in Terraform Explorer.

provider "alicloud" {
  region = var.region_id
}
# Define a variable for the region. The default value is cn-hangzhou.
variable "region_id" {
  type    = string
  default = "cn-hangzhou"
}
# Define a variable for the namespace description. The default value is "a namespace sample".
variable "namespace_description" {
  description = "Namespace Description"
  default     = "a namespace sample"
}
# Define a variable for the namespace name. The default value is "admin".
variable "namespace_name" {
  description = "Namespace Name"
  type        = string
  default     = "admin"
}
# Define a variable for the namespace ID. The default value is "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."
}

References