问题
I would like to get dynamic tags on an aws_autoscaling_group resource, but unlike the example in the documentation I do not yet now how many there will be and the tags must be shared with other resources. In the example below that would be EFS, but basically all other aws resources will be affected.
So the following lines are set:
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}"
}
I've come up with a solution that will support up to x
dynamic tags. Unfortunately it uses dummy tags to fill up not provided tags up to 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]),
)}"
}
With this code in the ASG I can use tags = ["${local.tags}"]
. With the example input the resources are tagged with
tag1 = value1
tag2 = value2
unusedtag1 =
unusedtag2 =
unusedtag3 =
unusedtag4 =
unusedtag5 =
unusedtag6 =
unusedtag7 =
I would like to have a solution that is
- dynamic
- does not use unnecessary tags
- works with ASG and other AWS resources at the same time
- simply propagates all tags on the ASG to the launched instances:
propagate_on_launch = true
So the solution has to take the existing tags and add the propagate_at_launch
key to it before adding it to the ASG.
回答1:
You can create a local map that holds the shared tags...
locals {
shared_tags = "${map(
"Foo", "1",
"Bar", "2"
)}"
}
...and then apply them (with optional merged updates) to tag attributes of resources:
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"))}"
}
Props to dwmkerr for the idea for this.
来源:https://stackoverflow.com/questions/51207505/terraform-dynamic-tags-on-asg-and-other-resources-like-efs