我们如何制作terraform创建的launch_template资源,将标签添加到创建的EBS卷并将其附加到使用的AMI?
将
tag_specifications
与 resource_type
的 volume
一起使用。
resource "aws_launch_template" "foo" {
name = "foo"
block_device_mappings {
device_name = "/dev/sda1"
ebs {
volume_size = 20
}
}
image_id = "ami-test"
instance_type = "t2.micro"
key_name = "test"
tag_specifications {
resource_type = "instance"
tags = {
Name = "test"
}
}
tag_specifications {
resource_type = "volume"
tags = {
Name = "test"
}
}
}
如果您想要标记多个资源,您可以使用此方法而不是重复代码。
locals {
launch_template_tags = ["instance", "volume"]
}
dynamic "tag_specifications" {
for_each = toset(local.launch_template_tags)
content {
resource_type = tag_specifications.key
tags = {
Name = "worker-node"
Environment = "dev"
}
}
}