我想在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 =
我想要一个解决方案
- 动态
- 不使用不必要的标签
- 同时使用 ASG 和其他 AWS 资源
- 只需将 ASG 上的所有标签传播到启动的实例:
propagate_on_launch = true
因此,解决方案必须采用现有标记并向其添加propagate_at_launch
密钥,然后再将其添加到 ASG。
你现在可以使用 Terraform 的dynamic "tag"
块来完成此操作。首先,创建一个数据资源:
data "aws_default_tags" "tags" {}
接下来,将此数据资源插入到目标资源中(如aws_autoscaling_group
:
resource "aws_autoscaling_group" "foo" {
desired_capacity = 1
force_delete = true
health_check_grace_period = 300
launch_configuration = aws_launch_configuration.foo.name
max_size = 4
min_size = 1
name = var.resource_name
placement_group = aws_placement_group.foo.name
vpc_zone_identifier = [aws_subnet.private[*].id]
dynamic "tag" {
for_each = data.aws_default_tags.tags.tags
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
您可以创建包含共享标签的本地地图...
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"))}"
}
道具给dwmkerr的想法。
然后,您可以利用aws_ec2_tag
资源,该资源也适用于非 ec2 资源,并结合提供程序属性ignore_tags
。有关更多详细信息,请参阅我就该主题所做的另一个答案。