阿里雲邊緣節點服務ENS產品已經接入Terraform,可以通過Terraform來實現快速配置。本文介紹如何在Cloud Shell中使用Terraform建立ENS執行個體。
說明
當前範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
由於阿里雲帳號(主帳號)具有資源的所有許可權,一旦發生泄露將面臨重大風險。建議您使用RAM使用者,並為該RAM使用者建立AccessKey,具體操作方式請參見建立RAM使用者和建立AccessKey。
使用以下樣本為RAM使用者授權,需要為該RAM使用者授予以下許可權:。具體操作方式請參見為RAM使用者授權。
{
"Version": "1",
"Statement": [
{
"Effect": "Allow",
"Action": "ens:*",
"Resource": "*"
},
{
"Effect": "Allow",
"Action": [
"bssapi:*",
"bss:*",
"efc:*"
],
"Resource": "*"
}
]
}
準備Terraform運行環境,您可以選擇以下任一方式來使用Terraform。
在Explorer中使用Terraform:阿里雲提供了Terraform的線上運行環境,您無需安裝Terraform,登入後即可線上使用和體驗Terraform。適用於零成本、快速、便捷地體驗和調試Terraform的情境。
Cloud Shell:阿里雲Cloud Shell中預裝了Terraform的組件,並已配置好身份憑證,您可直接在Cloud Shell中運行Terraform的命令。適用於低成本、快速、便捷地訪問和使用Terraform的情境。
在本地安裝和配置Terraform:適用於網路連接較差或需要自訂開發環境的情境。
重要
請確保Terraform版本不低於v0.12.28。如需檢查現有版本,請運行terraform --version
命令。
說明
本教程樣本包含的部分資源會產生一定費用,請在不需要時及時進行退訂。
使用Terraform建立ENS執行個體
操作步驟
本樣本將建立ENS執行個體。
建立一個工作目錄,並且在工作目錄中建立以下名為main.tf
的設定檔。main.tfTerraform主檔案,定義了將要部署的資源。
variable "region" {
default = "cn-hangzhou"
}
provider "alicloud" {
region = var.region
}
variable "image_id" {
default = "win2022_21H2_x64_dtc_zh-cn_40G_alibase_20211116"
}
variable "instance_type" {
default = "ens.sn1.small"
}
variable "internet_charge_type" {
default = "BandwidthByDay"
}
resource "alicloud_ens_instance" "instance" {
period = 1
public_ip_identification = true
period_unit = "Month"
scheduling_strategy = "Disperse"
schedule_area_level = "Big"
image_id = var.image_id
instance_type = var.instance_type
host_name = "Host80"
password = "Ens-test@"
internet_charge_type = var.internet_charge_type
payment_type = "PayAsYouGo"
carrier = "cmcc"
scheduling_price_strategy = "PriceLowPriority"
internet_max_bandwidth_out = 1
net_district_code = "100102"
system_disk {
size = 40
}
}
執行以下命令,初始化Terraform
運行環境。
返回如下資訊,表示Terraform初始化成功。
Initializing the backend...
Initializing provider plugins...
- Reusing previous version of hashicorp/alicloud from the dependency lock file
- Using previously-installed hashicorp/alicloud v1.231.0
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.
建立執行計畫,並預覽變更。
執行以下命令,建立ENS執行個體。
在執行過程中,根據提示輸入yes
並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示ENS執行個體建立成功。
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.
Enter a value: yes
alicloud_ens_instance.instance: Creating...
alicloud_ens_instance.instance: Still creating... [10s elapsed]
alicloud_ens_instance.instance: Still creating... [20s elapsed]
...
alicloud_ens_instance.instance: Still creating... [11m30s elapsed]
alicloud_ens_instance.instance: Creation complete after 11m31s [id=i-5495sqdyatehco3teqtls****]
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
驗證結果
執行terraform show命令
登入邊緣節點服務ENS控制台
您可以使用以下命令查詢Terraform已建立的資來源詳細資料:

登入邊緣節點服務ENS控制台,查看已建立的執行個體。

清理資源
當您不再需要上述通過Terraform建立或管理的資源時,請運行以下命令以釋放資源。關於terraform destroy
的更多資訊,請參見常用命令。
完整樣本
說明
當前範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
範例程式碼
variable "region" {
default = "cn-hangzhou"
}
provider "alicloud" {
region = var.region
}
variable "image_id" {
default = "win2022_21H2_x64_dtc_zh-cn_40G_alibase_20211116"
}
variable "instance_type" {
default = "ens.sn1.small"
}
variable "internet_charge_type" {
default = "BandwidthByDay"
}
resource "alicloud_ens_instance" "instance" {
period = 1
public_ip_identification = true
period_unit = "Month"
scheduling_strategy = "Disperse"
schedule_area_level = "Big"
image_id = var.image_id
instance_type = var.instance_type
host_name = "Host80"
password = "Ens-test@"
internet_charge_type = var.internet_charge_type
payment_type = "PayAsYouGo"
carrier = "cmcc"
scheduling_price_strategy = "PriceLowPriority"
internet_max_bandwidth_out = 1
net_district_code = "100102"
system_disk {
size = 40
}
}
如果您想體驗更多完整樣本,請前往更多完整樣本中對應產品的檔案夾查看。