Quickly create an Alibaba Cloud ECS host

Specify the terraform version

Here we specify the Alibaba Cloud provider version information and set the terraform version requirements

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# mkdir aliyun-ecs-one && cd aliyun-ecs-one
# touch versions.tf
# vim versions.tf
terraform {
required_providers {
alicloud = {
source = "aliyun/alicloud"
version = "1.115.1"
}
}

required_version = ">= 0.12"
}

Configure variables

Here we mainly specify the key pair, cloud region, ECS account and image information

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# vim variables.tf
# Alibaba Cloud subaccount access_key
variable "alicloud_access_key" {
default = "LTAI4GBXXXXXXXXXXXXXXXXXXXXXX"
description = "The Alicloud Access Key ID to launch resources. Support to environment 'ALICLOUD_ACCESS_KEY'."
}

# Alibaba Cloud subaccount secret_key
variable "alicloud_secret_key" {
default = "4Z4gbl3dXXXXXXXXXXXXXXXXXXXXX"
description = "The Alicloud Access Secret Key to launch resources. Support to environment 'ALICLOUD_SECRET_KEY'."
}

# Alibaba Cloud region, here is Hangzhou area
variable "region" {
default = "cn-hangzhou"
description = "The Alicloud region resources. Support to environment 'REGION'."
}

# Set the available data center in the Alibaba Cloud Hangzhou region, here set to cn-hangzhou-i
variable "availability_zone" {
description = "The available zone to launch ecs instance and other resources."
default = "cn-hangzhou-i"
}

# Set the image version
variable "image_id" {
default = "ubuntu_18_04_64_20G_alibase_20190624.vhd"
}

# Set the ECS instance type, for
variable "ecs_type" {
default = "ecs.s6-c1m2.small"
}

# Specify the ECS instance password
variable "ecs_password" {
default = "Test12345"
}

# Specify the ECS instance disk type, here is a normal cloud disk
variable "disk_category" {
default = "cloud_efficiency"
}

# Set the disk size
variable "disk_size" {
default = "40"
}

# Set the Internet charge line, the default is PayByTraffic (charged by traffic)
variable "internet_charge_type" {
default = "PayByTraffic"
}

# The maximum outgoing bandwidth of the public network. From version 1.7, the default setting is greater than 0 and will automatically apply for a dedicated public IP address
variable "internet_max_bandwidth_out" {
default = 5
}

Since this is a test, you need to create vpc, vswitch, security group, and security group rules in advance to create an instance

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# vim main.tf
provider "alicloud" {
region = var.region
access_key = var.alicloud_access_key
secret_key = var.alicloud_secret_key
}

resource "alicloud_vpc" "vpc" {
name = "tf_test_foo"
cidr_block = "10.100.0.0/16"
}

resource "alicloud_vswitch" "vsw" {
vpc_id = alicloud_vpc.vpc.id
cidr_block = "10.100.0.0/24" availability_zone = var.availability_zone } 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" } resource "alicloud_instance" "wanzi_test" { # cn-hangzhou availability_zone = var.availability_zone security_groups = alicloud_security_group.default.*.id instance_type = var.ecs_type system_disk_category = var.disk_category image_id = var.image_id instance_name = "wanzi_tf001" vswitch_id = alicloud_vswitch.vsw.id password = var.ecs_password } ``Execute plan, simulate execution effect ``shell # terraform plan An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # alicloud_instance.wanzi_test will be created + resource "alicloud_instance" "wanzi_test" { + availability_zone = "cn-hangzhou-i" + credit_specification = (known after apply) + deletion_protection = false + dry_run = false + host_name = (known after apply) + id = (known after apply) + image_id = "ubuntu_18_04_64_20G_alibase_20190624.vhd" + instance_charge_type = "PostPaid" + instance_name = "wanzi_tf001" + instance_type = "ecs.s6-c1m2.small" + internet_max_bandwidth_in = (known after apply) + internet_max_band width_out = 0 + key_name = (known after apply) + password = (sensitive value) + private_ip = (known after apply) + public_ip = (known after apply) + role_name = (known after apply) + security_groups = (known after apply) + spot_strategy = "NoSpot" + status = "Running" + subnet_id = (known after apply) + system_disk_category = "cloud_efficiency"
+ system_disk_performance_level = (known after apply)
+ system_disk_size = 40
+ volume_tags = (known after apply)
+ vswitch_id = (known after apply)
}

Create a cloud host. This process will request the Alibaba Cloud API and generate a terraform state file locally.

1
2
3
# terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # alicloud_instance.wanzi_test will be created + resource "alicloud_instance" "wanzi_test" { + availability_zone = "cn-hangzhou-i" + credit_specification = (known after apply) + deletion_protection = false + dry_run = false + host_name = (known after apply) + id = (known after apply) + image_id = "ubuntu_18_04_64_20G_alibase_20190624.vhd" + instance_charge_type = "PostPaid" + instance_name = "wanzi_tf001" + instance_type = "ecs.s6 -c1m2.small" + internet_max_bandwidth_in = (known after apply) + internet_max_bandwidth_out = 0 + key_name = (known after apply) + password = (sensitive value) + private_ip = (known after apply) + public_ip = (known after apply) + role_name = (known after apply) + security_groups = (known after apply) + spot_strategy = "NoSpot" + status = "Running" + subnet_id = (known after apply) + system_disk_category = "cloud_efficiency" + system_disk_performance_level = (known after apply) + system_disk_size = 40 + volume_tags = (known after apply) + vswitch_id = (known after apply) } # alicloud_security_group.default will be created + resource "alicloud_security_group" "default" { + id = (known after apply) + inner_access = (known after apply) + inner_access_policy = (known after apply) + name = "default" + security_group_type = "normal" + vpc_id = (known after apply) } ...... ...... Plan: 5 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_vpc.vpc: Creating... alicloud_vpc.vpc: Creation complete after 9s [id=vpc-bp1kulcyygsi727aay4hd] alicloud_security_group.default: Creating... alicloud_vswitch.vsw : Creating... alicloud_security_group.default: Creation complete after 1s [id=sg-bp11s5pka9pxtj6pn4xq] alicloud_security_group_rule.allow_all_tcp: Creating... alicloud_security_group_rule.allow_all_tcp: Creation complete after 1s [id=sg-bp11s5pka9pxtj6pn4xq:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1] alicloud_vswitch.vsw: Creation complete after 4s [id=vsw-bp1wgpgz9z8y2lfsl2beo] alicloud_instance.wanzi_test: Creating... alicloud_instance.wanzi_test: Still creating... [10s elapsed] alicloud_instance.wanzi_test: Still creating... [20s elapsed] alicloud_instance.wanzi_test: Creation complete after 22s [id=i-bp1gt9mb9asadff9r2zr] Apply complete! Resources: 5 added, 0 changed, 0 destroyed.

Through the above operations, we can see that the resource creation has been completed. During this process, the current directory will also generate a corresponding tfstate file. This data is very important and must not be deleted. In addition, you can view our creation through terraform show later Resource data information.

Create multiple ECS cloud hosts in batches

Configure Module

Since there are many excellent modules on https://registry.terraform.io/, we will directly use alibaba/ecs-instance/alicloud module to operate.

For more information about the official ECS module, please refer to: https://github.com/terraform-alicloud-modules/terraform-alicloud-ecs-instance

The configuration of variables.tf and versions.tf here is still based on the first step configuration; here we are in main .tf adds module resources to batch create ECS configurations as follows:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
module "tf-instances" {
source = "alibaba/ecs-instance/alicloud"
region = "cn-hangzhou"
number_of_instances = "3"
vswitch_id = alicloud_vswitch. vsw.id
group_ids = [alicloud_security_group.default.id]
private_ips = ["10.100.0.10", "10.100.0.11", "10.100.0.12"]
image_ids = ["ubuntu_18_04_64_20G_alibase_20190624.vhd"]
instance_type = var.ecs_type
internet_max_bandwidth_out = 10
associate_public_ip_address = true
instance_name = "my_module_instances_"
host_name = "wanzi-cluster"
internet_charge_type = "PayByTraffic"
password = var.ecs_password
system_disk_category = "cloud_ssd"
data_disks = [
{
disk_category = "cloud_ssd"
disk_name = "my_module_disk"
disk_size = "50"
}
]
}

It should be noted that by default, after internet_max_bandwidth_out is configured, a dedicated public IP address will be automatically requested. If you do not have this requirement, you do not need to Configuration.

Create resources in batches

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
➜ terraform apply
alicloud_vpc.vpc: Refreshing state... [id=vpc-bp1kulcyygsi727aay4hd]
alicloud_vswitch.vsw: Refreshing state... [id=vsw-bp1wgpgz9z8y2lfsl2beo]
alicloud_security_group.default: Refreshing state... [id=sg-bp11s5pka9pxtj6pn4xq]
alicloud_security_group_rule.allow_all_tcp: Refreshing state... [id=sg-bp11s5pka9pxtj6pn4xq:ingress:tcp:1/65535:intranet:0.0.0.0/0:accept:1]
alicloud_instance.wanzi_test: Refreshing state... [id=i-bp1gt9mb9asadff9r2zr]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.tf-instances.alicloud_instance.this[0] will be created
  + resource "alicloud_instance" "this" {
      + availability_zone             = (known after apply)
      + credit_specification          = (known after apply)
      + deletion_protection           = false
      + description                   = "An ECS instance came from terraform-alicloud-modules/ecs-instance"
      + dry_run                       = false
      + host_name                     = "wanzi-cluster001"
      + id                            = (known after apply)
      + image_id                      = "ubuntu_18_04_64_20G_alibase_20190624.vhd"
      + instance_charge_type          = "PostPaid"
      + instance_name                 = "my_module_instances_001"
      + instance_type                 = "ecs.s6-c1m2.small"
      + internet_charge_type          = "PayByTraffic"
      + internet_max_bandwidth_in     = (known after apply)
      + internet_max_bandwidth_out    = 10
      + key_name                      = (known after apply)
      + password                      = (sensitive value)
      + private_ip                    = "10.100.0.10"
      + public_ip                     = (known after apply)
      + role_name                     = (known after apply)
      + security_enhancement_strategy = "Active"
      + security_groups               = [
          + "sg-bp11s5pka9pxtj6pn4xq",
        ]
      + spot_strategy                 = "NoSpot"
      + status                        = "Running"
      + subnet_id                     = (known after apply)
      + system_disk_category          = "cloud_ssd"
      + system_disk_performance_level = (known after apply)
      + system_disk_size              = 40
      + tags                          = {
          + "Name" = "my_module_instances_001"
        }
      + volume_tags                   = {
          + "Name" = "my_module_instances_001"
        }
      + vswitch_id                    = "vsw-bp1wgpgz9z8y2lfsl2beo"

      + data_disks {
          + category             = "cloud_efficiency"
          + delete_with_instance = true
          + encrypted            = false
          + name                 = "TF_ECS_Disk"
          + performance_level    = (known after apply)
          + size                 = 40
        }
    }

  # module.tf-instances.alicloud_instance.this[1] will be created
  + resource "alicloud_instance" "this" {
      + availability_zone             = (known after apply)
      + credit_specification          = (known after apply)
      + deletion_protection           = false
      + description                   = "An ECS instance came from terraform-alicloud-modules/ecs-instance"
      + dry_run                       = false
      + host_name                     = "wanzi-cluster002"
      + id                            = (known after apply)
      + image_id                      = "ubuntu_18_04_64_20G_alibase_20190624.vhd"
      + instance_charge_type          = "PostPaid"
      + instance_name                 = "my_module_instances_002"
      + instance_type                 = "ecs.s6-c1m2.small"
      + internet_charge_type          = "PayByTraffic"
      + internet_max_bandwidth_in     = (known after apply)
      + internet_max_bandwidth_out    = 10
      + key_name                      = (known after apply)
      + password                      = (sensitive value)
      + private_ip                    = "10.100.0.11"
      + public_ip                     = (known after apply)
      + role_name                     = (known after apply)
      + security_enhancement_strategy = "Active"
      + security_groups               = [
          + "sg-bp11s5pka9pxtj6pn4xq",
        ]
      + spot_strategy                 = "NoSpot"
      + status                        = "Running"
      + subnet_id                     = (known after apply)
      + system_disk_category          = "cloud_ssd"
      + system_disk_performance_level = (known after apply)
      + system_disk_size              = 40
      + tags                          = {
          + "Name" = "my_module_instances_002"
        }
      + volume_tags                   = {
          + "Name" = "my_module_instances_002"
        }
      + vswitch_id                    = "vsw-bp1wgpgz9z8y2lfsl2beo"

      + data_disks {
          + category             = "cloud_efficiency"
          + delete_with_instance = true
          + encrypted            = false
          + name                 = "TF_ECS_Disk"
          + performance_level    = (known after apply)
          + size                 = 40
        }
    }

  # module.tf-instances.alicloud_instance.this[2] will be created
  + resource "alicloud_instance" "this" {
      + availability_zone             = (known after apply)
      + credit_specification          = (known after apply)
      + deletion_protection           = false
      + description                   = "An ECS instance came from terraform-alicloud-modules/ecs-instance"
      + dry_run                       = false
      + host_name                     = "wanzi-cluster003"
      + id                            = (known after apply)
      + image_id                      = "ubuntu_18_04_64_20G_alibase_20190624.vhd"
      + instance_charge_type          = "PostPaid"
      + instance_name                 = "my_module_instances_003"
      + instance_type                 = "ecs.s6-c1m2.small"
      + internet_charge_type          = "PayByTraffic"
      + internet_max_bandwidth_in     = (known after apply)
      + internet_max_bandwidth_out    = 10
      + key_name                      = (known after apply)
      + password                      = (sensitive value)
      + private_ip                    = "10.100.0.12"
      + public_ip                     = (known after apply)
      + role_name                     = (known after apply)
      + security_enhancement_strategy = "Active"
      + security_groups               = [
          + "sg-bp11s5pka9pxtj6pn4xq",
        ]
      + spot_strategy                 = "NoSpot"
      + status                        = "Running"
      + subnet_id                     = (known after apply)
      + system_disk_category          = "cloud_ssd"
      + system_disk_performance_level = (known after apply)
      + system_disk_size              = 40
      + tags                          = {
          + "Name" = "my_module_instances_003"
        }
      + volume_tags                   = {
          + "Name" = "my_module_instances_003"
        }
      + vswitch_id                    = "vsw-bp1wgpgz9z8y2lfsl2beo"

      + data_disks {
          + category             = "cloud_efficiency"
          + delete_with_instance = true
          + encrypted            = false
          + name                 = "TF_ECS_Disk"
          + performance_level    = (known after apply)
          + size                 = 40
        }
    }

Plan: 3 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

module.tf-instances.alicloud_instance.this[2]: Creating...
module.tf-instances.alicloud_instance.this[1]: Creating...
module.tf-instances.alicloud_instance.this[0]: Creating...
module.tf-instances.alicloud_instance.this[1]: Still creating... [10s elapsed]
module.tf-instances.alicloud_instance.this[2]: Still creating... [10s elapsed]
module.tf-instances.alicloud_instance.this[0]: Still creating... [10s elapsed]
module.tf-instances.alicloud_instance.this[1]: Still creating... [20s elapsed]
module.tf-instances.alicloud_instance.this[0]: Still creating... [20s elapsed]
module.tf-instances.alicloud_instance.this[2]: Still creating... [20s elapsed]
module.tf-instances.alicloud_instance.this[0]: Creation complete after 21s [id=i-bp1hwbo4htk8sbwxtk6o]
module.tf-instances.alicloud_instance.this[1]: Creation complete after 21s [id=i-bp17lh41gywyih0xg6we]
module.tf-instances.alicloud_instance.this[2]: Creation complete after 22s [id=i-bp11zlrl6vxeaerz4ad0]

Apply complete! Resources: 3 added, 0 changed, 0 destroyed.

So far, the entire operation of creating multiple ECS instances has been completed. If there are any adjustments to the currently deployed ecs resources, perform basic write/plan /apply operation. This process will restart the Alibaba Cloud instance.