使用Terraform模塊批量創(chuàng)建多臺ECS實例
本文介紹如何使用Terraform模塊批量創(chuàng)建多臺ECS實例。
前提條件
在開始之前,請您確保完成以下操作:
使用Terraform,您需要一個阿里云賬號和訪問密鑰(AccessKey)。 請在阿里云控制臺中的AccessKey管理頁面上創(chuàng)建和查看您的AccessKey。
已經(jīng)安裝并配置了Terraform,具體操作請參見在本地安裝和配置Terraform和在Cloud Shell中使用Terraform。
操作步驟
創(chuàng)建專有網(wǎng)絡(luò)和交換機。
創(chuàng)建terraform.tf文件,輸入以下內(nèi)容,保存在當前的執(zhí)行目錄中。
data "alicloud_zones" "default" { available_resource_creation = "VSwitch" } resource "alicloud_vpc" "vpc" { vpc_name = "tf_test_foo" cidr_block = "172.16.0.0/12" } resource "alicloud_vswitch" "vsw" { vpc_id = alicloud_vpc.vpc.id cidr_block = "172.16.0.0/21" zone_id = data.alicloud_zones.default.zones[0].id }
運行
terraform apply
開始創(chuàng)建。運行
terraform show
查看已創(chuàng)建的專有網(wǎng)絡(luò)和交換機。您也可以登錄VPC控制臺查看專有網(wǎng)絡(luò)和交換機的屬性。
在上一步創(chuàng)建的專有網(wǎng)絡(luò)中創(chuàng)建一個安全組,并添加一個允許任何地址訪問的安全組規(guī)則。
在terraform.tf文件中增加以下內(nèi)容。
resource "alicloud_security_group" "default" { name = "default" vpc_id = alicloud_vpc.vpc.id } resource "alicloud_security_group_rule" "allow_all_tcp" { type = "ingress" ip_protocol = "tcp" nic_type = "intranet" policy = "accept" port_range = "1/65535" priority = 1 security_group_id = alicloud_security_group.default.id cidr_ip = "0.0.0.0/0" }
運行
terraform apply
開始創(chuàng)建。運行
terraform show
查看已創(chuàng)建的安全組和安全組規(guī)則。您也可以登錄ECS控制臺查看安全組和安全組規(guī)則。
使用Module創(chuàng)建多臺ECS實例。在本示例中,創(chuàng)建3臺ECS實例。
重要在本教程中,通過module塊實現(xiàn)資源批量創(chuàng)建,Terraform使用Git作為module塊源代碼的獲取工具,所以需預先安裝Git。
alibaba/ecs-instance/alicloud模塊支持的Terraform版本需大于等于0.13.0。
在terraform.tf文件中增加以下內(nèi)容。
data "alicloud_instance_types" "default" { availability_zone = data.alicloud_zones.default.zones[0].id cpu_core_count = 1 memory_size = 2 } data "alicloud_images" "default" { name_regex = "^ubuntu_[0-9]+_[0-9]+_x64*" most_recent = true owners = "system" } module "tf-instances" { source = "alibaba/ecs-instance/alicloud" region = "cn-beijing" number_of_instances = 3 vswitch_id = alicloud_vswitch.vsw.id group_ids = [alicloud_security_group.default.id] private_ips = ["172.16.0.10", "172.16.0.11", "172.16.0.12"] image_ids = [data.alicloud_images.default.images[0].id] instance_type = data.alicloud_instance_types.default.instance_types[0].id internet_max_bandwidth_out = 10 associate_public_ip_address = true instance_name = "my_module_instances_" host_name = "sample" internet_charge_type = "PayByTraffic" password = "User@123" system_disk_category = "cloud_ssd" data_disks = [ { category = "cloud_ssd" name = "my_module_disk" size = "50" } ] }
說明在上述示例中,同時指定
associate_public_ip_address = true
和internet_max_bandwidth_out = 10
,因此會自動為實例分配一個公網(wǎng)IP。詳細的參數(shù)解釋請參見 參數(shù)說明。運行
terraform apply
開始創(chuàng)建。運行
terraform show
查看已創(chuàng)建的ECS實例。運行ssh root@<publicip>,并輸入密碼來訪問ECS實例。
操作樣例
provider "alicloud" {
region = "cn-beijing"
}
data "alicloud_zones" "default" {
available_resource_creation = "VSwitch"
}
resource "alicloud_vpc" "vpc" {
vpc_name = "tf_test_foo"
cidr_block = "172.16.0.0/12"
}
resource "alicloud_vswitch" "vsw" {
vpc_id = alicloud_vpc.vpc.id
cidr_block = "172.16.0.0/21"
zone_id = data.alicloud_zones.default.zones[0].id
}
resource "alicloud_security_group" "default" {
name = "default"
vpc_id = alicloud_vpc.vpc.id
}
resource "alicloud_security_group_rule" "allow_all_tcp" {
type = "ingress"
ip_protocol = "tcp"
nic_type = "intranet"
policy = "accept"
port_range = "1/65535"
priority = 1
security_group_id = alicloud_security_group.default.id
cidr_ip = "0.0.0.0/0"
}
data "alicloud_instance_types" "default" {
availability_zone = data.alicloud_zones.default.zones[0].id
cpu_core_count = 1
memory_size = 2
}
data "alicloud_images" "default" {
name_regex = "^ubuntu_[0-9]+_[0-9]+_x64*"
most_recent = true
owners = "system"
}
module "tf-instances" {
source = "alibaba/ecs-instance/alicloud"
region = "cn-beijing"
number_of_instances = 3
vswitch_id = alicloud_vswitch.vsw.id
group_ids = [alicloud_security_group.default.id]
private_ips = ["172.16.0.10", "172.16.0.11", "172.16.0.12"]
image_ids = [data.alicloud_images.default.images[0].id]
instance_type = data.alicloud_instance_types.default.instance_types[0].id
internet_max_bandwidth_out = 10
associate_public_ip_address = true
instance_name = "my_module_instances_"
host_name = "sample"
internet_charge_type = "PayByTraffic"
password = "User@123"
system_disk_category = "cloud_ssd"
data_disks = [
{
category = "cloud_ssd"
name = "my_module_disk"
size = "50"
}
]
}