在Terraform 0.11.14中,可以通过以下循环遍历先前在数据变量中找到的不同子网(参见https://www.terraform.io/docs/providers/aws/d/subnet_ids.html):
data "aws_subnet_ids" "private" {
vpc_id = "${var.vpc_id}"
tags = {
Tier = "Private"
}
}
resource "aws_instance" "app" {
count = "3"
ami = "${var.ami}"
instance_type = "t2.micro"
subnet_id = "${element(data.aws_subnet_ids.private.ids, count.index)}"
}
但是,由于我迁移到Terreform 0.12,因此此语法导致以下错误:
Error: Error in function call
on ..\..\modules\elk\es-proxy-server.tf line 21, in resource "aws_spot_instance_request" "kibana_proxy":
21: subnet_id = "${element(data.aws_subnet_ids.private.ids, count.index)}"
|----------------
| count.index is 0
| data.aws_subnet_ids.private.ids is set of string with 2 elements
Call to function "element" failed: cannot read elements from set of string.
我试图使用tolist函数并弄清楚如何利用以下https://www.terraform.io/upgrade-guides/0-12.html#working-with-count-on-resources取得成功。
答案 0 :(得分:1)
您应该能够:
subnet_id = "${tolist(data.aws_subnet_ids.private.ids)[count.index]}"