使用具有多个资源的 tag_secifications 设置启动模板

Setup launch template with tag_secifications with multiple resources

根据下面给出的文档示例,我看到了实例类型的标签集。但是如果我想将相同的标签应用于多个资源,那么我将如何设置它 https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template

  tag_specifications {
    resource_type = "instance"

    tags = {
      Name = "test"
    }
  }

您可以指定两次或使用 dynamic blocks。动态块的示例是:

variable "to_tag" {
  default = ["instance", "volume"]
}


resource "aws_launch_template" "foo" {
  name = "foo"

  image_id = data.aws_ami.server.id

  instance_type = "t2.micro"
  
  dynamic "tag_specifications" {
    for_each = toset(var.to_tag)
    content {
       resource_type = tag_specifications.key
       tags = {
         Name = "test"
       }
    }
  } 
}

或者简单地指定两次:

resource "aws_launch_template" "foo" {
  name = "foo"

  image_id = data.aws_ami.server.id

  instance_type = "t2.micro"
  

  tag_specifications {
     resource_type = "instance"
     tags = {
       Name = "test"
    }
  } 

  tag_specifications {
     resource_type = "volume"
     tags = {
       Name = "test"
    }
  }

}