如何将子网列表传递给 'subnets' 属性?

How to pass a list of subnet to the 'subnets' attribute?

我正在重写我的子网代码以使其更灵活并由参数驱动:

resource "aws_subnet" "private" {
  count             = "${var.az_count}"
  cidr_block        = "${cidrsubnet(aws_vpc.ecs.cidr_block, 8, count.index)}"
  availability_zone = "${data.aws_availability_zones.available.names[count.index]}"
  vpc_id            = "${aws_vpc.ecs.id}"
}

这是将子网传递给

的旧代码
resource "aws_ecs_service" "service" {
    network_configuration {
        subnets = [ "${aws_subnet.subnet1.id}", "${aws_subnet.subnet2.id}" ]
        ...
}

如何将子网 ID 从 aws_subnet.private 传递到 subnets 属性?

我试过了

subnets = ${aws_subnet.priate[*].id}

但是出现错误:

 Expected the start of an expression, but found an invalid expression token

首先,您可以删除 ${},除非您正在插值。敲击那些东西,您将获得更多可读性:

resource "aws_subnet" "private" {
  count             = var.az_count
  cidr_block        = cidrsubnet(aws_vpc.ecs.cidr_block, 8, count.index)
  availability_zone = data.aws_availability_zones.available.names[count.index]
  vpc_id            = aws_vpc.ecs.id
}

你的 splat 参考语法是正确的,但你的拼写是错误的。试试这个:

subnets = aws_subnet.private[*].id

Splat 参考:https://www.terraform.io/docs/configuration/expressions.html#splat-expressions