文档

使用Terraform管理SAE命名空间

更新时间:

Serverless 应用引擎 SAE(Serverless App Engine)的命名空间可以将您的应用从逻辑上进行划分,例如测试环境、开发环境、预发环境和线上环境等。本文介绍如何通过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_namespace资源提供了以下参数:

  • 必需:namespace_id

    命名空间ID,格式为<RegionId>:<namespace_name>,例如cn-hangzhou:dev。更新该参数时会创建新的命名空间。SAE支持的RegionId,请参见DescribeRegions

  • 必需:namespace_name

    命名空间名称。

  • 可选:namespace_description

    命名空间描述信息。

更多信息,请参见alicloud_sae_namespace

创建命名空间

本示例以在华东1(杭州)地域下创建命名空间为例,命名空间名称为dev、命名空间ID为cn-hangzhou:dev

  1. 创建一个用于存放Terraform资源的项目文件夹,命名为terraform
  2. 执行以下命令,进入项目目录。
    cd terraform
  3. 执行以下命令,创建名为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"
    }
  4. 执行以下命令,初始化Terraform运行环境。

    terraform init

    预期输出:

    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.
  5. 依次执行以下命令,创建SAE命名空间。

    1. 执行以下命令,执行配置文件。

      terraform apply
    2. 根据提示依次输入命名空间ID、命名空间名称等信息。

      • namespace_id:输入cn-hangzhou:dev

      • namespace_name:输入dev

      预期输出:

      ...
      
      Plan: 1 to add, 0 to change, 0 to destroy.
      
      Changes to Outputs:
        + namespace_id = "cn-hangzhou:dev"
      
      ...
      
      alicloud_sae_namespace.default: Creating...
      alicloud_sae_namespace.default: Creation complete after 5s [id=cn-hangzhou:dev]
      
      Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
      
      Outputs:
      
      namespace_id = "cn-hangzhou:dev"

    命名空间已成功创建。

更新命名空间

本示例以更新命名空间名称为例,将华东1(杭州)地域下命名空间的名称从dev更新为prod

  1. 执行以下命令,执行配置文件。
    terraform apply
  2. 根据提示依次输入命名空间ID、命名空间名称等信息,更新SAE命名空间名称。

    • namespace_id:输入cn-hangzhou:dev

    • namespace_name:输入prod

    预期输出:

    ...
    
    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"

    命名空间名称已成功更新为prod

删除命名空间

本示例以在华东1(杭州)地域下删除命名空间为例,命名空间名称为prod、命名空间ID为cn-hangzhou:dev

  1. 在目标项目目录内执行以下命令,运行配置文件。
    terraform destroy
  2. 根据提示依次输入命名空间ID、命名空间名称等信息,删除SAE命名空间。

    • namespace_id:输入cn-hangzhou:dev

    • namespace_name:输入prod

    预期输出:

    ...
    
    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.

    命名空间已成功删除。

  • 本页导读 (1)
文档反馈