如何在 Terraform 中正确使用 Count 和 pick multiple az subnets

How to correctly use Count and pick multiple az subnets in Terraform

我正在尝试实现一个 模块,我试图在已经创建的子网中旋转多个实例(通过 terraform),但我不确定如何实际使用在模块中计数以及如何从 s3 存储桶数据源中选择值以在 multi-az 中旋转实例,这是我在模块目录中的资源的样子

resource "aws_instance" "ec2-instances" {
  count = "${var.count_num }"
  ami = "${data.aws_ami.ubuntu.id}"
  instance_type = "${var.machine_type}"
  key_name = "${var.key_name}"
  #vpc_security_group_ids = ["${aws_security_group.jumpbox-sec-group.id}"]
  vpc_security_group_ids =  ["${var.sec-group}"]
  disable_api_termination = "${var.is_production ? true : false}"
  subnet_id = "${element(var.es_stg_subnets, count.index)}" <--- This won't work , i need to use data-source as s3 
  tags {
    #Name = "${var.master_name}-${count.index+1}"
    Name = "${var.instance-tag}-${count.index+1}"
    Type = "${var.instance-type-tag}"
  }
  root_block_device {
    volume_size = "${var.instance-vol-size}"
    volume_type = "gp2"
  }
}

这是实际的模块:

module "grafana-stg" {
  source = "../../modules/services/gen-ec2"
  #ami_id = "${data.aws_ami.ubuntu.id}"
  instance_type = "${var.grafana_machine_type}"
  key_name = "jumpbox"
  vpc_security_group_ids = ["${aws_security_group.grafana-sec-group.id}"]

  #subnets = "${data.terraform_remote_state.s3_bucket_state.subnet-public-prod-1a}"

  subnet_id = ??????????????????

  disable_api_termination = "${var.is_production ? true : false}"


}

我会考虑使用 data source.

检索您的子网

Utilising Data Sources

Terraform 有数据源的概念。您可以从 AWS 提取资源所需的信息。在您的 gen-ec2.tf 文件中 -

// In order to get subnets, you need the VPC they belong to.
// Note you can filter on a variety of different tags.
data "aws_vpc" "selected" {
  tags {
    Name = "NameOfVPC"
  }
}

// This will then retrieve all subnet ids based on filter
data "aws_subnet_ids" "private" {
  vpc_id = "${data.aws_vpc.selected.id}"
  tags {
    Tier = "private*"
  }
}

resource "aws_instance" "ec2-instances" {
  count = "${length(data.aws_subnet_ids.private.ids)}"
  ami = "${data.aws_ami.ubuntu.id}"
  instance_type = "${var.machine_type}"
  key_name = "${var.key_name}"
  vpc_security_group_ids =  ["${var.sec-group}"]
  disable_api_termination = "${var.is_production ? true : false}"
  subnet_id = "${element(data.aws_subnet_ids.private.*.ids, count.index)}" 
  tags {
    Name = "${var.instance-tag}-${count.index+1}"
    Type = "${var.instance-type-tag}"
  }
  root_block_device {
    volume_size = "${var.instance-vol-size}"
    volume_type = "gp2"
  }
}

你的模块现在看起来像这样 -

module "grafana-stg" {
  source = "../../modules/services/gen-ec2"
  #ami_id = "${data.aws_ami.ubuntu.id}"
  instance_type = "${var.grafana_machine_type}"
  key_name = "jumpbox"
  vpc_security_group_ids = ["${aws_security_group.grafana-sec-group.id}"]
  disable_api_termination = "${var.is_production ? true : false}"
}

对我来说,因为我使用的是 Terraform v0.12.5,下面的代码片段运行良好

data "aws_subnet_ids" "public_subnet_list" {
  vpc_id = "${var.vpc_id}"
  tags = {
    Tier = "Public"
  }
}


resource "aws_instance" "example" {
  count         = 2
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  subnet_id     = tolist(data.aws_subnet_ids.public_subnet_list.ids)[count.index]
}