全部产品
Search
文档中心

边缘节点服务ENS:Terraform集成示例

更新时间:May 26, 2026

Terraform是一种开源工具,用于安全高效地预配和管理云基础结构。您可以使用Terraform管理ENS资源,本文为您演示如何通过Terraform创建一个ENS的实例。

操作步骤

配置权限

执行Terraform模板需要RAM用户权限,我们需要创建一个RAM用户,并获取AccessKey,添加权限策略。该AccessKey会配置为Terraform工具的环境变量。您需要为RAM用户添加以下权限:

  • AliyunENSFullAccess:管理ENS资源的权限。

阿里云账号拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维。请根据业务的实际情况按需分配权限后进行接口调用。RAM用户需具备操作边缘节点服务 ENS资源的权限。具体操作,请参见边缘节点服务 ENS系统权限策略参考

安装Terraform

  • 在本地安装和配置Terraform,请参见在本地安装和配置Terraform

    安装完成后,您可以打开命令行终端,输入terraform version,看到返回版本信息表示已成功安装。

  • 如果您不想安装Terraform,可以使用阿里云提供的在线服务Cloud Shell,其中内置了Terraform的运行环境。

编写模板

创建一个名为eip.tf文件,填入以下内容,用于创建一个实例。

variable "name" {
  default = "terraform-example"
}
resource "alicloud_ens_eip" "default" {
  description   = "EipDescription_autotest"
  bandwidth     = "5"
  isp           = "cmcc"
  payment_type  = "PayAsYouGo"
  ens_region_id = "cn-chenzhou-telecom_unicom_cmcc"
  eip_name      = var.name
  internet_charge_type = "95BandwidthByMonth"
}

运行模板

该示例以Windows操作系统为例,在其他操作系统中,运行命令的具体方式可能会有所不同。

  1. 初始化加载模块,包括Provider等模块。

    terraform init
    D:\Terraform>terraform init
    Initializing the backend...
    Initializing provider plugins...
    - Reusing previous version of hashicorp/alicloud from the dependency lock file
    - Using previously-installed hashicorp/alicloud v1.226.0
    ╷
    │ Warning: Additional provider information from registry
    │
    │ The remote registry returned warnings for registry.terraform.io/hashicorp/alicloud:
    │ - 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.
    D:\Terraform>
  2. 验证模板语法是否正确。

    terraform validate
    D:\Terraform>terraform validate
    Success! The configuration is valid.
  3. 创建执行计划。

    terraform plan
    D:\Terraform>terraform plan
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    Terraform will perform the following actions:
      # alicloud_ens_eip.default will be created
      + resource "alicloud_ens_eip" "default" {
          + bandwidth            = 5
          + create_time          = (known after apply)
          + description          = "EipDescription_autotest"
          + eip_name             = "terraform-example"
          + ens_region_id        = "cn-chenzhou-telecom_unicom_cmcc"
          + id                   = (known after apply)
          + internet_charge_type = "95BandwidthByMonth"
          + isp                  = "cmcc"
          + payment_type         = "PayAsYouGo"
          + status               = (known after apply)
        }
    Plan: 1 to add, 0 to change, 0 to destroy.
    Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
  4. 部署模板。

    terraform apply
    D:\Terraform>terraform apply
    Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
      + create
    Terraform will perform the following actions:
      # alicloud_ens_eip.default will be created
      + resource "alicloud_ens_eip" "default" {
          + bandwidth           = 5
          + create_time         = (known after apply)
          + description         = "EipDescription_autotest"
          + eip_name            = "terraform-example"
          + ens_region_id       = "cn-chenzhou-telecom_unicom_cmcc"
          + id                  = (known after apply)
          + internet_charge_type = "95BandwidthByMonth"
          + isp                 = "cmcc"
          + payment_type        = "PayAsYouGo"
          + status              = (known after apply)
        }
    Plan: 1 to add, 0 to change, 0 to destroy.
    Do you want to perform these actions?
      Terraform will perform the actions described above.
      Only 'yes' will be accepted to approve.
  5. 输入yes即可真正变更资源。您可以通过terraform destroy释放当前模板创建的资源。

    alicloud_ens_eip.default: Creating...
    alicloud_ens_eip.default: Creation complete after 7s [id=eip-5xxxxxxxxxxxx]
    Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
  6. 创建完成后,您可以通过OpenAPI、SDK或者在ENS控制台,可以查看到创建的实例。

    在 ENS 控制台左侧导航栏选择算力与镜像 > 实例,进入实例列表页面,确认已创建的实例状态为运行中