我正在重写我的子网代码,使其更加灵活并由参数驱动:
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
板片参考: https://www.terraform.io/docs/configuration/expressions.html#splat-expressions