通过Terraform本地列表中的资源值列表进行迭代



我试图在Terraform Template_file数据字段中获取一系列数组:

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

,但我没有找到适当的方法来获得我想要的东西。我有一个AWS_INSTANCE资源,计数为3,并且我正在尝试根据每个资源计数在本地内生成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数据源也支持计数。

这是一个隐藏功能,切勿记录(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 Resources

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

样本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"
            }

最新更新