terraform - 迭代 Terraform 本地列表中的资源值列表

标签 terraform

我正在尝试获取要在 Terraform template_file 数据字段中使用的数组数组:

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

  vars {
    metrics = "${jsonencode(local.metrics)}"
  }
}

但我没有找到获得我想要的东西的正确方法。我有一个计数为 3 的 aws_instance 资源,我尝试根据每个资源计数在本地内部生成 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数据源支持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.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"
            }

关于terraform - 迭代 Terraform 本地列表中的资源值列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54950269/

相关文章:

amazon-web-services - Terraform:如何从 VPC id 获取 VPC CIDR?

json - 使用 Terraform 创建 Azure 策略

powershell - Windows 自定义不适用于 Terraform

terraform - 需要对 Terraform 资源进行多次计数?

terraform - 将变量乘以另一个变量,然后将其格式化为字符串

地形 : depends_on argument not creating the specified resource first

aws-cloudformation - terraform tf 文件或模块可以自动使资源过期吗?

amazon-web-services - Terraform AWS Athena将Glue目录用作数据库

azure - 在 Azure 上创建资源的服务主体

git - 如何使用 terraform 通过 git 在 azure 中自动部署