全部產品
Search
文件中心

ENS:Terraform整合樣本

更新時間:May 27, 2026

Terraform是一種開源工具,用於安全高效地預配和管理雲基礎結構。您可以使用Terraform管理ENS資源,本文為您示範如何通過Terraform建立一個ENS的執行個體。

操作步驟

配置許可權

執行Terraform模板需要RAM使用者權限,我們需要建立一個RAM使用者,並擷取AccessKey,添加權限原則。該AccessKey會配置為Terraform工具的環境變數。您需要為RAM使用者添加以下許可權:

  • AliyunENSFullAccess:管理ENS資源的許可權。

阿里雲帳號擁有所有API的存取權限,風險很高。強烈建議您建立並使用RAM使用者進行API訪問或日常營運。請根據業務的實際情況按需分配許可權後進行介面調用。RAM使用者需具備操作Edge Node Service資源的許可權。具體操作,請參見邊緣節點服務 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 控制台左側導覽列選擇算力與鏡像 > 執行個體,進入執行個體列表頁面,確認已建立的執行個體狀態為運行中