在Terraform local中遍历列表中的资源值列表

在Terraform local中遍历列表中的资源值列表,terraform,Terraform,我正在尝试获取阵列阵列,以在地形模板文件数据字段中使用: data "template_file" "dashboard" { template = "${file("${path.module}/files/dashboard.json")}" vars { metrics = "${jsonencode(local.metrics)}" } } 但是我没有找到合适的方法来得到我想要的。我有一个计数为3的aws_实例资源,我正在尝试根据每个资源计数在本地内生成3个数组。

我正在尝试获取阵列阵列,以在地形模板文件数据字段中使用:

data "template_file" "dashboard" {
  template = "${file("${path.module}/files/dashboard.json")}"

  vars {
    metrics = "${jsonencode(local.metrics)}"
  }
}
但是我没有找到合适的方法来得到我想要的。我有一个计数为3的aws_实例资源,我正在尝试根据每个资源计数在本地内生成3个数组。到目前为止,我唯一想到的是:

locals {
  metrics = [
    "collectd", "GenericJMX.gauge.50thPercentile", "Host", "${aws_instance.instance.*.id}", "PluginInstance", "cassandra_client_request-latency"
  ]
}
显然,这样做的目的是将所有实例一个接一个地放在同一个数组中。我试图实现的结果数组如下所示:

["collectd", "GenericJMX.gauge.50thPercentile", "Host", "the id of instance 0", PluginInstance", "cassandra_client_request-latency"], 
["collectd", "GenericJMX.gauge.50thPercentile", "Host", "the id of instance 1", PluginInstance", "cassandra_client_request-latency"], 
["collectd", "GenericJMX.gauge.50thPercentile", "Host", "the id of instance 3", PluginInstance", "cassandra_client_request-latency"]
这将在模板${metrics}变量中展开


是否有任何方法可以在本地环境中实现我想要的,并使其在模板中可用?

terraform数据源也支持

这是一个隐藏功能,永远不会被记录()

dashboard.json
进行一些调整,然后使用下面的代码生成大量的模板文件数据源资源

data "template_file" "dashboard" {
  count = "${length(aws_instance.instance.*.id)}"
  template = "${file("${path.module}/files/dashboard.json")}"

  vars {
    metrics = "${element(aws_instance.instance.*.id, count.index)}"
  }
}
您可以将其引用为terraform count资源

count = "${length(aws_instance.instance.*.id)}"

${data.template_file.dashboard.*.rendered[count.index]}"
以下是完整的测试数据

$ cat main.tf
data "aws_ami" "ubuntu" {
  most_recent = true

  filter {
    name   = "name"
    values = ["ubuntu/images/hvm-ssd/ubuntu-trusty-14.04-amd64-server-*"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }

  owners = ["099720109477"] # Canonical
}

resource "aws_instance" "instance" {
  count         = 2
  ami           = "${data.aws_ami.ubuntu.id}"
  instance_type = "t2.micro"

  tags = {
    Name = "HelloWorld"
  }
}


data "template_file" "dashboard" {
  count    = "${length(aws_instance.instance.*.id)}"
  template = "${file("${path.module}/files/dashboard.json")}"

  vars {
    metric = "${element(aws_instance.instance.*.id, count.index)}"
  }
}

output "aws_instances" {
  value = "${length(aws_instance.instance.*.id)}"
}

$ cat files/dashboard.json
["collectd", "GenericJMX.gauge.50thPercentile", "Host", "${metric}", PluginInstance", "cassandra_client_request-latency"]
应用更改后,请检查tfstate文件,数据源为

data.template_file.dashboard.0
data.template_file.dashboard.1
样本状态:

            "data.template_file.dashboard.1": {
                "type": "template_file",
                "depends_on": [
                    "aws_instance.instance.*"
                ],
                "primary": {
                    "id": "8e05e7c115a8d482b9622a1eddf5ee1701b8cc4695da5ab9591899df5aeb703d",
                    "attributes": {
                        "id": "8e05e7c115a8d482b9622a1eddf5ee1701b8cc4695da5ab9591899df5aeb703d",
 # the date is here ==> "rendered": "[\"collectd\", \"GenericJMX.gauge.50thPercentile\", \"Host\", \"i-015961b744ff55da4\", PluginInstance\", \"cassandra_client_request-latency\"]\n",
                        "template": "[\"collectd\", \"GenericJMX.gauge.50thPercentile\", \"Host\", \"${metric}\", PluginInstance\", \"cassandra_client_request-latency\"]\n",
                        "vars.%": "1",
                        "vars.metric": "i-015961b744ff55da4"
                    },
                    "meta": {},
                    "tainted": false
                },
                "deposed": [],
                "provider": "provider.template"
            }
我想你需要这个: