我试图按名称抓取子网列表,过滤掉以"d"结尾的子网;(对于"d";可用区),然后获取Terraform v0.11.14中其余子网的id。下面是我创建子网的方法:
resource "aws_subnet" "private" {
vpc_id = "${var.vpc_id}"
cidr_block = "${element(split(",", var.cidrs), count.index)}"
availability_zone = "${element(split(",", var.azs), count.index)}"
count = "${length(split(",", var.cidrs))}"
tags { Name = "${var.name}.${element(split(",", var.azs), count.index)}" }
lifecycle { create_before_destroy = true }
}
我知道如何实现我的结果在Terraform v1.0.0:
output "subnet_ids" { value = "${join(",", list(for subnet in aws_subnet.private.*: subnet.id if substr(subnet.name, -1, 1) != "d" ))}" }
我试图在Terraform v0.11.14中做同样的事情,只是切断最后一个子网,希望子网在默认情况下按名称排序,但它们不是:
output "subnet_ids" { value = "${join(",", slice(aws_subnet.private.*.id, 0, length(aws_subnet.private.*.id) - 1))}" }
有人知道如何实现我想做什么与Terraform v0.11.14?
您可以检查以下内容。我理解你的过滤是基于AZ名的最后一个字母。因此,也许您可以按AZ排序,并删除最后一个,正如您所尝试的:
locals {
sorted_az = "${sort(aws_subnet.private.*.availability_zone)}"
length_az_minus_1 = "${length(local.sorted_az)-1}"
azs_without_last = "${slice(local.sorted_az, 0, local.length_az_minus_1)}"
}
output "subnet_ids" {
value = "${ matchkeys(aws_subnet.private.*.id, aws_subnet.private.*.availability_zone, local.azs_without_last)}"
}