источник данных terraform также поддерживает count .
Это функция скрытия, которая никогда не будет задокументирована (https://github.com/hashicorp/terraform/pull/8635)
Произведите некоторые корректировки на вашем dashboard.json
, затем используйте приведенные ниже коды для генерации количества ресурсов источника данных template_file.
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 = "${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.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"
}