我想在aws_autoscaling_group资源上获取动态标签,但是与文档中的示例不同,我现在还没有多少标签,标签必须与其他资源共享。在下面的示例中将是EFS,但基本上所有其他AWS资源都会受到影响。
因此设置了以下几行:
variable "tags" {
type = "map"
default = {
tag1 = "value1"
tag2 = "value2"
}
}
resource "aws_autoscaling_group" "asg" {
name = "test-asg"
launch_configuration = "test-lc"
min_size = 1
max_size = 1
min_elb_capacity = 1
vpc_zone_identifier = ["subnet-1234"]
tags = <code_here>
}
resource "aws_efs_file_system" "foo" {
creation_token = "my-product"
tags = "${var.tags}"
}
我提出了一个解决方案,该解决方案最多支持x
个动态代码。不幸的是,它使用伪标签来填充未提供的标签,直到x
。
data "template_file" "test" {
count = "9"
template = "key:@:$${key}:@:value:@:$${value}:@:propagate_at_launch:@:true"
vars {
key = "${element(concat(keys(var.tags), list("unusedtag1", "unusedtag2","unusedtag3","unusedtag4","unusedtag5","unusedtag6","unusedtag7","unusedtag8","unusedtag9")), count.index)}"
value = "${element(concat(values(var.tags), list("", "","","","","","","","")), count.index)}"
}
}
locals{
tag0 = "${split(":@:", data.template_file.test.0.rendered)}"
tag1 = "${split(":@:", data.template_file.test.1.rendered)}"
tag2 = "${split(":@:", data.template_file.test.2.rendered)}"
tag3 = "${split(":@:", data.template_file.test.3.rendered)}"
tag4 = "${split(":@:", data.template_file.test.4.rendered)}"
tag5 = "${split(":@:", data.template_file.test.5.rendered)}"
tag6 = "${split(":@:", data.template_file.test.6.rendered)}"
tag7 = "${split(":@:", data.template_file.test.7.rendered)}"
tag8 = "${split(":@:", data.template_file.test.8.rendered)}"
tags = "${list(
map(local.tag0[0],local.tag0[1],local.tag0[2],local.tag0[3],local.tag0[4],local.tag0[5]),
map(local.tag1[0],local.tag1[1],local.tag1[2],local.tag1[3],local.tag1[4],local.tag1[5]),
map(local.tag2[0],local.tag2[1],local.tag2[2],local.tag2[3],local.tag2[4],local.tag2[5]),
map(local.tag3[0],local.tag3[1],local.tag3[2],local.tag3[3],local.tag3[4],local.tag3[5]),
map(local.tag4[0],local.tag4[1],local.tag4[2],local.tag4[3],local.tag4[4],local.tag4[5]),
map(local.tag5[0],local.tag5[1],local.tag5[2],local.tag5[3],local.tag5[4],local.tag5[5]),
map(local.tag6[0],local.tag6[1],local.tag6[2],local.tag6[3],local.tag6[4],local.tag6[5]),
map(local.tag7[0],local.tag7[1],local.tag7[2],local.tag7[3],local.tag7[4],local.tag7[5]),
map(local.tag8[0],local.tag8[1],local.tag8[2],local.tag8[3],local.tag8[4],local.tag8[5]),
)}"
}
有了ASG中的此代码,我可以使用tags = ["${local.tags}"]
。在示例输入中,资源被标记为
tag1 = value1
tag2 = value2
unusedtag1 =
unusedtag2 =
unusedtag3 =
unusedtag4 =
unusedtag5 =
unusedtag6 =
unusedtag7 =
我想要一个解决方案
propagate_on_launch = true
因此,解决方案必须采用现有标签并在其上添加propagate_at_launch
键,然后再将其添加到ASG中。
答案 0 :(得分:1)
然后,您可以将aws_ec2_tag
资源与提供者属性ignore_tags
结合使用,该资源也适用于非EC2资源。有关更多详细信息,请参阅我在该主题上所做的another answer。
答案 1 :(得分:0)
您可以创建一个包含共享标签的本地地图...
locals {
shared_tags = "${map(
"Foo", "1",
"Bar", "2"
)}"
}
...然后将它们(带有可选的合并更新)应用于标记资源的属性:
resource "aws_autoscaling_group" "asg" {
name = "test-asg"
launch_configuration = "test-lc"
min_size = 1
max_size = 1
min_elb_capacity = 1
vpc_zone_identifier = ["subnet-1234"]
tags = "${merge(local.shared_tags, map("Baz", "3"))}"
}
resource "aws_efs_file_system" "foo" {
creation_token = "my-product"
tags = "${merge(local.shared_tags, map("Qux", "4"))}"
}