我正在尝试从Terraform获取实例名称,data.aws_instance.foo.tags
给我一个包含名称作为其中一个标签的地图列表,但我没有成功从中获取键名的值
答案 0 :(得分: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
]
答案 1 :(得分:0)
我在aws_db_instance
上找到了与此类似的解决方案,方法是仅使用lookup
。
这是标签正文:
tags = {
TAG_KEY = "TAG_VALUE"
}
如何检索:
output "TAG_VALUE" {
value = "${lookup(aws_db_instance.this.tags, "TAG_KEY", "default")}"
}
对于
Outputs:
TAG_VALUE = TAG_KEY