This topic describes how to use a Terraform module to create multiple Elastic Compute Service (ECS) instances at a time.
Prerequisites
An Alibaba Cloud account and an AccessKey pair are created. You can go to the AccessKey Pair page of the Alibaba Cloud Management Console to create or view your AccessKey pair.
Terraform is installed and configured. For more information, see Install and configure Terraform in the local PC and Use Terraform in Cloud Shell.
Procedure
Create a virtual private cloud (VPC) and a vSwitch.
Create the terraform.tf file, enter the following content, and then save the file to the current working directory.
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 }Run the
terraform applycommand to create the VPC and vSwitch.Run the
terraform showcommand to view the created VPC and vSwitch.You can also log on to the VPC console to view the attributes of the VPC and vSwitch.
Create a security group for the VPC and add a security group rule to allow access from all IP addresses.
In the terraform.tf file, add the following content:
resource "alicloud_security_group" "default" { security_group_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" }Run the
terraform applycommand to create the security group and security group rule.Run the
terraform showcommand to view the created security group and security group rule.You can also log on to the ECS console to view the security group and security group rule.
Use a module to create multiple ECS instances. In this example, three ECS instances are created.
ImportantIn this topic, a module is used to create multiple resources at a time. Terraform uses Git to obtain the source code of the module. Therefore, you must install Git in advance.
To use the alibaba/ecs-instance/alicloud module, the version of Terraform must be 0.13.0 or later.
In the terraform.tf file, add the following content:
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" } ] }NoteIn the preceding sample code,
associate_public_ip_address = trueandinternet_max_bandwidth_out = 10are specified. In this case, a public IP address is assigned to each ECS instance. For more information about the parameters, go to the ecs-instance page.Run the
terraform applycommand to create the ECS instances.Run the
terraform showcommand to view the created ECS instances.Run the ssh root@<publicip> command and enter the password to access the ECS instances.
Sample code
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" {
security_group_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"
}
]
}