雲命令列(Cloud Shell)中預裝了Terraform。Terraform是一種開源工具,用於安全高效地預配和管理雲基礎結構。您可以通過Terraform管理阿里雲資源。
本教程管理雲資源範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
前提條件
為運行Terraform命令的RAM使用者綁定以下最小權限原則,以擷取管理本樣本所涉及資源的許可權。更多資訊,請參見管理RAM使用者的許可權。
該自訂權限原則允許使用者啟動、停止、擷取和列出Cloud Shell會話,並且可以建立、修改、刪除和管理ECS執行個體及其相關資源。
{ "Version": "1", "Statement": [ { "Effect": "Allow", "Action": [ "cloudshell:StartSession", "cloudshell:StopSession", "cloudshell:GetSession", "cloudshell:ListSessions", "ecs:CreateInstance", "ecs:RunInstances", "ecs:StartInstance", "ecs:StopInstance", "ecs:RebootInstance", "ecs:TerminateInstance", "ecs:ModifyInstanceAttribute", "ecs:DescribeInstances", "ecs:AllocatePublicIpAddress", "ecs:AssignPrivateIpAddresses", "ecs:UnassignPrivateIpAddresses", "ecs:ModifyInstanceVpcAttribute", "ecs:ResetInstance", "ecs:AttachKeyPair", "ecs:DetachKeyPair", "ecs:CreateSecurityGroup", "ecs:DeleteSecurityGroup", "ecs:AuthorizeSecurityGroup", "ecs:RevokeSecurityGroup", "ecs:CreateVpc", "ecs:DeleteVpc", "ecs:CreateVSwitch", "ecs:DeleteVSwitch", "ecs:CreateDisk", "ecs:DeleteDisk", "ecs:AttachDisk", "ecs:DetachDisk", "ecs:CreateSnapshot", "ecs:DeleteSnapshot", "ecs:CreateImage", "ecs:DeleteImage" ], "Resource": "*" } ] }
使用的資源
alicloud_vpc:使用者自訂的虛擬網路環境,使用者可以在其中啟動阿里雲資源。
alicloud_vswitch:是在VPC內定義的邏輯交換器,用於將流量轉寄到VPC內的不同部分。
alicloud_security_group:安全性群組是用於控制網路流量進出雲端服務器的安全性原則集合。
alicloud_instance:使用者可以按需建立、管理和銷毀ECS執行個體,以滿足計算需求。
alicloud_security_group_rule:安全性群組規則定義了允許通過安全性群組的流量類型。
操作步驟
啟動雲命令列
在啟動雲命令列之前,您需要使用已授予許可權的可用RAM身份登入阿里雲首頁。雲命令列將使用當前登入態為您自動設定身份憑證,無需手動操作。為避免自動設定憑證功能失效,建議您在操作時避免對初始設定檔default進行修改或刪除。
您可以通過以下方式啟動雲命令列:
在控制台中運行
單擊控制台首頁頂部導覽列中的雲命令列按鈕,啟動雲命令列。
獨立運行
在瀏覽器中輸入https://shell-sg.alibabacloud.com,開啟雲命令列操作介面。
雲命令列啟動時會為您建立一台虛擬機器,這個過程將消耗一些時間,最長不超過30秒。
您可以根據實際需要開啟多個雲命令列視窗,最多可同時開啟5個雲命令列視窗。多個雲命令列視窗會串連到同一台虛擬機器,虛擬機器數量不會因為您開啟新的命令列視窗而增加。
更多資訊,請參見什麼是雲命令列。
建立雲資源
在Cloud Shell中編寫Terraform模板。
您可以使用
vim命令直接編寫模板。執行如下命令建立一個工程目錄及模板檔案:
mkdir terraform-project cd terraform-project touch main.tf以下程式碼範例是一個建立ECS執行個體的Terraform模板,請將內容粘貼到
main.tf中。Cloud Shell 可自動擷取登入帳號的身份認證資訊,無需額外設定環境變數。provider "alicloud" { region = var.region_id } variable "region_id" { default = "cn-shanghai" } variable "available_disk_category" { default = "cloud_efficiency" } variable "available_resource_creation" { default = "VSwitch" } variable "vpc_name" { default = "tf_test_fofo" } variable "vpc_cidr_block" { default = "172.16.0.0/12" } variable "vswitch_cidr_block" { default = "172.16.0.0/21" } variable "security_group_name" { default = "default" } variable "instance_type" { default = "ecs.n4.large" } variable "image_id" { default = "ubuntu_18_04_64_20G_alibase_20190624.vhd" } variable "instance_name" { default = "test_fofo" } variable "internet_max_bandwidth_out" { default = 10 } variable "port_range" { default = "1/65535" } variable "priority" { default = 1 } variable "cidr_ip" { default = "0.0.0.0/0" } data "alicloud_zones" "default" { available_disk_category = var.available_disk_category available_resource_creation = var.available_resource_creation } resource "alicloud_vpc" "vpc" { vpc_name = var.vpc_name cidr_block = var.vpc_cidr_block } resource "alicloud_vswitch" "vsw" { vpc_id = alicloud_vpc.vpc.id cidr_block = var.vswitch_cidr_block zone_id = data.alicloud_zones.default.zones[0].id } resource "alicloud_security_group" "default" { name = var.security_group_name vpc_id = alicloud_vpc.vpc.id } resource "alicloud_instance" "instance" { availability_zone = data.alicloud_zones.default.zones[0].id security_groups = [alicloud_security_group.default.id] instance_type = var.instance_type system_disk_category = var.available_disk_category image_id = var.image_id instance_name = var.instance_name vswitch_id = alicloud_vswitch.vsw.id internet_max_bandwidth_out = var.internet_max_bandwidth_out } resource "alicloud_security_group_rule" "allow_all_tcp" { type = "ingress" ip_protocol = "tcp" nic_type = "intranet" policy = "accept" port_range = var.port_range priority = var.priority security_group_id = alicloud_security_group.default.id cidr_ip = var.cidr_ip }執行
init命令初始化Terraform。terraform init預期結果:
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.執行
apply命令建立ECS執行個體,在執行過程中,根據提示輸入yes並按下Enter鍵,等待命令執行完成,若出現以下資訊,則表示建立搶佔式執行個體完成。terraform apply預期結果:
Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
操作驗證
執行terraform show命令
您可以使用以下命令查詢Terraform已建立的資來源詳細資料:
terraform show# alicloud_instance.instance:
resource "alicloud_instance" "instance" {
availability_zone = "cn-shanghai-a"
cpu = 2
create_time = "2024-11-13T08:52Z"
deletion_protection = false
dry_run = false
enable_jumbo_frame = false
expired_time = "2099-12-31T15:59Z"
host_name = "iZuxxx"
http_put_response_hop_limit = 0
id = "i-ufxxx"
image_id = "ubuntu_18_04_64_20G_alibase_xxx vhd"
instance_charge_type = "PostPaid"
instance_name = "test_fofo"
instance_type = "ecs.n4.large"
internet_charge_type = "PayByTraffic"
internet_max_bandwidth_in = 500
internet_max_bandwidth_out = 10
ipv6_address_count = 0
ipv6_addresses = []
maintenance_action = "AutoRecover"
maintenance_notify = false
memory = 4096
network_interface_id = "eni-uf6ixxx"
network_interface_traffic_mode = "Standard"
os_name = "Ubuntu 18.04 64位"
os_type = "linux"
primary_ip_address = "xxx"
private_ip = "xxx"
public_ip = "xxx"
queue_pair_number = 0Elastic Compute Service控制台截圖
登入雲伺服器ECS控制台,查看建立執行個體。
在左側導覽列單擊執行個體,進入執行個體列表頁面,確認目標執行個體的執行個體 ID 及運行狀態。
清理資源
當您不再需要上述通過terraform建立或管理的資源時,運行下面的命令釋放資源。關於terraform destroy的更多資訊,請參見常用命令。
terraform destroy完整樣本
當前範例程式碼支援一鍵運行,您可以直接運行代碼。一鍵運行
範例程式碼
切換 Terraform 版本
Cloudshell 中預設的 Terraform 版本為 0.12.31,如果需要更高的版本可以通過tfenv來切換。
查看Cloud Shell中內建的Terraform版本。
tfenv list切換到需要的Terraform版本。
tfenv use <terraform_version>相關文檔
Terrafrom介紹,請參見瞭解阿里雲Terraform。