ASG 和 EFS 等其他资源上的 terraform 动态标签 [英] terraform dynamic tags on ASG and other resources like EFS

查看:24
本文介绍了ASG 和 EFS 等其他资源上的 terraform 动态标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 aws_autoscaling_group 资源,但与文档中的示例不同,我现在还不知道会有多少,并且标签 必须与其他资源共享.在下面的示例中,这将是 EFS,但基本上所有其他 aws 资源都会受到影响.

I would like to get dynamic tags on an aws_autoscaling_group resource, but unlike the example in the documentation I do not yet now how many there will be and the tags must be shared with other resources. In the example below that would be EFS, but basically all other aws resources will be affected.

所以设置了以下几行:

variable "tags" {
  type = "map"

  default = {
    tag1 = "value1"
    tag2 = "value2"
  }
}

resource "aws_autoscaling_group" "asg" {
  name                 = "test-asg"
  launch_configuration = "test-lc"
  min_size             = 1
  max_size             = 1
  min_elb_capacity     = 1
  vpc_zone_identifier  = ["subnet-1234"]
  tags                 = <code_here>
}

resource "aws_efs_file_system" "foo" {
  creation_token = "my-product"
  tags           = "${var.tags}"
}

我想出了一个最多支持 x 动态标签的解决方案.不幸的是,它使用虚拟标签来填充未提供的标签,直到 x.

I've come up with a solution that will support up to x dynamic tags. Unfortunately it uses dummy tags to fill up not provided tags up to x.

data "template_file" "test" {
  count    = "9"
  template = "key:@:$${key}:@:value:@:$${value}:@:propagate_at_launch:@:true"

  vars {
    key   = "${element(concat(keys(var.tags), list("unusedtag1", "unusedtag2","unusedtag3","unusedtag4","unusedtag5","unusedtag6","unusedtag7","unusedtag8","unusedtag9")), count.index)}"
    value = "${element(concat(values(var.tags), list("", "","","","","","","","")), count.index)}"
  }
}

locals{
  tag0 = "${split(":@:", data.template_file.test.0.rendered)}"
  tag1 = "${split(":@:", data.template_file.test.1.rendered)}"
  tag2 = "${split(":@:", data.template_file.test.2.rendered)}"
  tag3 = "${split(":@:", data.template_file.test.3.rendered)}"
  tag4 = "${split(":@:", data.template_file.test.4.rendered)}"
  tag5 = "${split(":@:", data.template_file.test.5.rendered)}"
  tag6 = "${split(":@:", data.template_file.test.6.rendered)}"
  tag7 = "${split(":@:", data.template_file.test.7.rendered)}"
  tag8 = "${split(":@:", data.template_file.test.8.rendered)}"

  tags = "${list(
          map(local.tag0[0],local.tag0[1],local.tag0[2],local.tag0[3],local.tag0[4],local.tag0[5]),
          map(local.tag1[0],local.tag1[1],local.tag1[2],local.tag1[3],local.tag1[4],local.tag1[5]),
          map(local.tag2[0],local.tag2[1],local.tag2[2],local.tag2[3],local.tag2[4],local.tag2[5]),
          map(local.tag3[0],local.tag3[1],local.tag3[2],local.tag3[3],local.tag3[4],local.tag3[5]),
          map(local.tag4[0],local.tag4[1],local.tag4[2],local.tag4[3],local.tag4[4],local.tag4[5]),
          map(local.tag5[0],local.tag5[1],local.tag5[2],local.tag5[3],local.tag5[4],local.tag5[5]),
          map(local.tag6[0],local.tag6[1],local.tag6[2],local.tag6[3],local.tag6[4],local.tag6[5]),
          map(local.tag7[0],local.tag7[1],local.tag7[2],local.tag7[3],local.tag7[4],local.tag7[5]),
          map(local.tag8[0],local.tag8[1],local.tag8[2],local.tag8[3],local.tag8[4],local.tag8[5]),
          )}"
}

通过 ASG 中的这段代码,我可以使用 tags = ["${local.tags}"].通过示例输入,资源被标记为

With this code in the ASG I can use tags = ["${local.tags}"]. With the example input the resources are tagged with

tag1 = value1
tag2 = value2
unusedtag1 = 
unusedtag2 = 
unusedtag3 = 
unusedtag4 = 
unusedtag5 =
unusedtag6 = 
unusedtag7 =  

我想有一个解决方案是

  • 动态
  • 不使用不必要的标签
  • 同时使用 ASG 和其他 AWS 资源
  • 简单地将 ASG 上的所有标签传播到启动的实例:propagate_on_launch = true

因此,解决方案必须在将其添加到 ASG 之前,获取现有标签并为其添加 propagate_at_launch 键.

So the solution has to take the existing tags and add the propagate_at_launch key to it before adding it to the ASG.

推荐答案

然后,您可以利用 aws_ec2_tag 资源,该资源也适用于非 ec2 资源,并与提供程序属性 ignore_tags.请参阅我就该主题所做的另一个答案了解更多详细信息.

You can then leverage the aws_ec2_tag resource, which works on non-ec2 resources as well, on conjunction with the provider attribute ignore_tags. Please refer to another answer I made on the topic for more detail.

这篇关于ASG 和 EFS 等其他资源上的 terraform 动态标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆