通过 Terraform 标记由 launch_template 和自动缩放组创建的 EBS 卷

问题描述 投票:0回答:2

我们如何制作terraform创建的launch_template资源,将标签添加到创建的EBS卷并将其附加到使用的AMI?

amazon-web-services terraform
2个回答
11
投票

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

0
投票

如果您想要标记多个资源,您可以使用此方法而不是重复代码。

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"
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.