4

我正在尝试从 Terraform 获取实例名称,data.aws_instance.foo.tags给我一个包含名称作为标签之一的地图列表,但我没有成功从中获取键 Name 的值。

4

2 回答 2

1

如果找到了使用模板渲染绕过地图问题列表的可行解决方案:

resource "aws_instance" "k8s_master" {
  count                       = "${var.master_count}"
  ami                         = "${var.ami}"
  instance_type               = "${var.instance_type}"
  vpc_security_group_ids      = ["${aws_security_group.k8s_sg.id}"]
  associate_public_ip_address = false
  subnet_id                   = "${element(var.subnet_ids,count.index % length(var.subnet_ids))}"
  user_data                   = "${file("${path.root}/files/user_data.sh")}"
  iam_instance_profile        = "${aws_iam_instance_profile.master_profile.name}"

  tags = "${merge(
    local.k8s_tags,
    map(
      "Name", "k8s-master-${count.index}",
      "Environment", "${var.environment}"
    )
  )}"
}

data "template_file" "k8s_master_names" {
  count    = "${var.master_count}"
  template = "${lookup(aws_instance.k8s_master.*.tags[count.index], "Name")}"
}

output "k8s_master_name" {
  value = [
    "${data.template_file.k8s_master_names.*.rendered}",
  ]
}

这将导致以下输出:

k8s_master_name = [
    k8s-master-0,
    k8s-master-1,
    k8s-master-2
]
于 2018-05-18T09:21:14.087 回答
0

我在 an 上找到了一个类似的解决方案,aws_db_instance只使用lookupon this。

这是一个标签正文:

tags = {
  TAG_KEY = "TAG_VALUE"
}

如何找回:

output "TAG_VALUE" {
  value = "${lookup(aws_db_instance.this.tags, "TAG_KEY", "default")}"
}

为了

Outputs:

TAG_VALUE = TAG_KEY
于 2019-05-24T16:09:17.697 回答