gpt4 book ai didi

amazon-web-services - 如何从 Terraform data.aws_instance 获取特定标签值

转载 作者:行者123 更新时间:2023-12-04 08:05:06 25 4
gpt4 key购买 nike

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

最佳答案

如果找到了使用模板渲染绕过 map 问题列表的有效解决方案:

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
]

关于amazon-web-services - 如何从 Terraform data.aws_instance 获取特定标签值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45722076/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com