# vim variables.tf# Alibaba Cloud subaccount access_keyvariable "alicloud_access_key"{default="LTAI4GBXXXXXXXXXXXXXXXXXXXXXX"description="The Alicloud Access Key ID to launch resources. Support to environment 'ALICLOUD_ACCESS_KEY'."}# Alibaba Cloud subaccount secret_keyvariable "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 areavariable "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-ivariable "availability_zone"{description="The available zone to launch ecs instance and other resources."default="cn-hangzhou-i"}# Set the image versionvariable "image_id"{default="ubuntu_18_04_64_20G_alibase_20190624.vhd"}# Set the ECS instance type, forvariable "ecs_type"{default="ecs.s6-c1m2.small"}# Specify the ECS instance passwordvariable "ecs_password"{default="Test12345"}# Specify the ECS instance disk type, here is a normal cloud diskvariable "disk_category"{default="cloud_efficiency"}# Set the disk sizevariable "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 addressvariable "internet_max_bandwidth_out"{default=5}
Configure instance-related resources
Since this is a test, you need to create vpc, vswitch, security group, and security group rules in advance to create an instance
# terraform applyAn 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.
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:
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.
➜ 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.