使用Terraform模块批量创建多台ECS实例

更新时间:

本文介绍如何使用Terraform模块批量创建多台ECS实例。

前提条件

在开始之前,请您确保完成以下操作:

操作步骤

  1. 创建专有网络和交换机。

    1. 创建terraform.tf文件,输入以下内容,保存在当前的执行目录中。

      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
      }
    2. 运行terraform apply开始创建。

    3. 运行terraform show查看已创建的专有网络和交换机。

      您也可以登录VPC控制台查看专有网络和交换机的属性。

  2. 在上一步创建的专有网络中创建一个安全组,并添加一个允许任何地址访问的安全组规则。

    1. terraform.tf文件中增加以下内容。

      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"
      }
    2. 运行terraform apply开始创建。

    3. 运行terraform show查看已创建的安全组和安全组规则。

      您也可以登录ECS控制台查看安全组和安全组规则。

  3. 使用Module创建多台ECS实例。在本示例中,创建3台ECS实例。

    重要
    • 在本教程中,通过module块实现资源批量创建,Terraform使用Git作为module块源代码的获取工具,所以需预先安装Git。

    • alibaba/ecs-instance/alicloud模块支持的Terraform版本需大于等于0.13.0。

    1. terraform.tf文件中增加以下内容。

      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 = trueinternet_max_bandwidth_out = 10,因此会自动为实例分配一个公网IP。详细的参数解释请参见 参数说明

    2. 运行terraform apply开始创建。

    3. 运行terraform show查看已创建的ECS实例。

    4. 运行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"
    }
  ]
}