我想在aws_autoscaling_group资源上获取动态标签,但与文档中的示例不同,我现在还不知道会有多少。
因此设置了以下行:
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>
}
我想出了一个解决方案,最多可支持 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 =
我想要一个解决方案
- 动态
- 是否标记额外的标签
编辑:恢复到不明确但更简单的问题,并在 ASG 和其他资源(如 EFS(上打开了一个关于地形动态标签的新问题
文档中给出的示例显示了可以越来越动态地向自动缩放组添加标记的不同方法。
显然,您可以像这样使用完全静态的tag
节:
resource "aws_autoscaling_group" "bar" {
...
tag {
key = "lorem"
value = "ipsum"
propagate_at_launch = false
}
}
或者,您可以使用更动态的tags
节,使您能够使用 concat
构建标签列表:
variable extra_tags {
default = [
{
key = "Foo"
value = "Bar"
propagate_at_launch = true
},
{
key = "Baz"
value = "Bam"
propagate_at_launch = true
},
]
}
resource "aws_autoscaling_group" "bar" {
...
tags = [
{
key = "explicit1"
value = "value1"
propagate_at_launch = true
},
{
key = "explicit2"
value = "value2"
propagate_at_launch = true
},
]
tags = ["${concat(
list(
map("key", "interpolation1", "value", "value3", "propagate_at_launch", true),
map("key", "interpolation2", "value", "value4", "propagate_at_launch", true)
),
var.extra_tags)
}"]
}
如果未覆盖默认值,则第二个输出将使用以下内容标记 ASG:
explicit1 = value1
explicit2 = value2
interpolation1 = value3
interpolation2 = value4
Foo = Bar
Baz = Bam
如果要通过 tfvar 覆盖extra_tags
,例如:
extra_tags = [
{
key = "CustomTag1"
value = "CustomValue1"
propagate_at_launch = true
},
{
key = "CustomTag2"
value = "CustomValue3"
propagate_at_launch = true
},
{
key = "CustomTag3"
value = "CustomValue3"
propagate_at_launch = true
},
{
key = "CustomTag4"
value = "CustomValue4"
propagate_at_launch = true
},
]
然后,您的 ASG 将被标记为:
explicit1 = value1
explicit2 = value2
interpolation1 = value3
interpolation2 = value4
CustomTag1 = CustomValue1
CustomTag2 = CustomValue2
CustomTag3 = CustomValue3
CustomTag4 = CustomValue4
这使您能够选择在 ASG 上始终存在一些标记,以及完全在模块外部定义的可选动态标记或其他内容。