如何将标签映射应用于 aws_autoscaling_group?
How do I apply a map of tags to aws_autoscaling_group?
https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html#propagate_at_launch
我这样做是为了将标签应用于 aws 资源:
tags = "${merge(
local.common_tags, // reused in many resources
map(
"Name", "awesome-app-server",
"Role", "server"
)
)}"
但是 asg 需要 propagate_at_launch 字段。
我已经在许多其他资源中使用了我的标签地图,我想将其重新用于 asg 资源。很确定我将始终将 propagate_at_launch 设置为 true。如何将其添加到地图的每个元素并将其用于 tags
字段?
我使用空资源执行此操作并将其输出作为标记,示例如下 -
data "null_data_source" "tags" {
count = "${length(keys(var.tags))}"
inputs = {
key = "${element(keys(var.tags), count.index)}"
value = "${element(values(var.tags), count.index)}"
propagate_at_launch = true
}
}
resource "aws_autoscaling_group" "asg_ec2" {
..........
..........
lifecycle {
create_before_destroy = true
}
tags = ["${data.null_data_source.tags.*.outputs}"]
tags = [
{
key = "Name"
value = "awesome-app-server"
propagate_at_launch = true
},
{
key = "Role"
value = "server"
propagate_at_launch = true
}
]
}
您可以将 var.tags
替换为 local.common_tags
。
IMPORTANT UPDATE for Terraform 0.12+. It now supports dynamic nested
blocks and for-each. If you are on 0.12+ version, use below code -
resource "aws_autoscaling_group" "asg_ec2" {
..........
..........
lifecycle {
create_before_destroy = true
}
tag {
key = "Name"
value = "awesome-app-server"
propagate_at_launch = true
}
tag {
key = "Role"
value = "server"
propagate_at_launch = true
}
dynamic "tag" {
for_each = var.tags
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
}
https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html#propagate_at_launch
我这样做是为了将标签应用于 aws 资源:
tags = "${merge(
local.common_tags, // reused in many resources
map(
"Name", "awesome-app-server",
"Role", "server"
)
)}"
但是 asg 需要 propagate_at_launch 字段。
我已经在许多其他资源中使用了我的标签地图,我想将其重新用于 asg 资源。很确定我将始终将 propagate_at_launch 设置为 true。如何将其添加到地图的每个元素并将其用于 tags
字段?
我使用空资源执行此操作并将其输出作为标记,示例如下 -
data "null_data_source" "tags" {
count = "${length(keys(var.tags))}"
inputs = {
key = "${element(keys(var.tags), count.index)}"
value = "${element(values(var.tags), count.index)}"
propagate_at_launch = true
}
}
resource "aws_autoscaling_group" "asg_ec2" {
..........
..........
lifecycle {
create_before_destroy = true
}
tags = ["${data.null_data_source.tags.*.outputs}"]
tags = [
{
key = "Name"
value = "awesome-app-server"
propagate_at_launch = true
},
{
key = "Role"
value = "server"
propagate_at_launch = true
}
]
}
您可以将 var.tags
替换为 local.common_tags
。
IMPORTANT UPDATE for Terraform 0.12+. It now supports dynamic nested blocks and for-each. If you are on 0.12+ version, use below code -
resource "aws_autoscaling_group" "asg_ec2" {
..........
..........
lifecycle {
create_before_destroy = true
}
tag {
key = "Name"
value = "awesome-app-server"
propagate_at_launch = true
}
tag {
key = "Role"
value = "server"
propagate_at_launch = true
}
dynamic "tag" {
for_each = var.tags
content {
key = tag.key
value = tag.value
propagate_at_launch = true
}
}
}