首页 > 解决方案 > 使用带有多个资源的 tag_secifications 设置启动模板

问题描述

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

  tag_specifications {
    resource_type = "instance"

    tags = {
      Name = "test"
    }
  }

在此处输入图像描述

标签: amazon-web-servicesterraformterraform-provider-aws

解决方案


您要么指定它两次,要么使用动态块。动态块的示例是:

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"
    }
  }

}

推荐阅读