gpt4 book ai didi

terraform - 如何将标签映射应用到 aws_autoscaling_group?

转载 作者:行者123 更新时间:2023-12-03 00:02:20 25 4
gpt4 key购买 nike

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字段。

我已经在许多其他资源中使用了标签 map ,并且我想将其重新用于 asg 资源。很确定我将始终将propagate_at_launch设置为true。如何将其添加到 map 的每个元素并将其用于 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
}
}

}

关于terraform - 如何将标签映射应用到 aws_autoscaling_group?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53698758/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com