代码之家  ›  专栏  ›  技术社区  ›  Jonathan

自动缩放组和EFS上带有动态标记的模块

  •  0
  • Jonathan  · 技术社区  · 6 年前

    我想在 aws_autoscaling_group 资源,但与文档中的示例不同,我现在还不知道将有多少。

    因此,设置以下行:

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

    我想出了一个解决方案,可以支持多达 x 动态标记。不幸的是,它使用虚拟标签来填充未提供的标签 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}"] . 通过示例输入,资源被标记为

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

    我想有一个解决方案

    • 动态
    • 是否标记其他标记

    编辑:返回到不清楚但更简单的问题,并在上打开新问题 terraform dynamic tags on ASG and other resources like EFS

    1 回复  |  直到 6 年前
        1
  •  0
  •   ydaetskcoR    6 年前

    这个 example given in the docs 显示了越来越动态地向自动缩放组添加标记的不同方式。

    很明显,可以使用完全静态 tag 像这样的诗节:

    resource "aws_autoscaling_group" "bar" {
      ...
    
      tag {
        key                 = "lorem"
        value               = "ipsum"
        propagate_at_launch = false
      }
    }
    

    或者您可以使用更动态的 tags 节,使您能够使用 concat :

    variable extra_tags {
      default = [
        {
          key = "Foo"
          value = "Bar"
          propagate_at_launch = true
        },
        {
          key = "Baz"
          value = "Bam"
          propagate_at_launch = true
        },
      ]
    }
    
    resource "aws_autoscaling_group" "bar" {
      ...
    
      tags = [
        {
          key                 = "explicit1"
          value               = "value1"
          propagate_at_launch = true
        },
        {
          key                 = "explicit2"
          value               = "value2"
          propagate_at_launch = true
        },
      ]
    
      tags = ["${concat(
        list(
          map("key", "interpolation1", "value", "value3", "propagate_at_launch", true),
          map("key", "interpolation2", "value", "value4", "propagate_at_launch", true)
        ),
        var.extra_tags)
      }"]
    }
    

    如果未覆盖默认值,则第二个输出将为ASG标记:

    explicit1 = value1
    explicit2 = value2
    interpolation1 = value3
    interpolation2 = value4
    Foo = Bar
    Baz = Bam
    

    如果要覆盖 extra_tags 通过TFVAR,例如:

    extra_tags = [
      {
        key = "CustomTag1"
        value = "CustomValue1"
        propagate_at_launch = true
      },
      {
        key = "CustomTag2"
        value = "CustomValue3"
        propagate_at_launch = true
      },
      {
        key = "CustomTag3"
        value = "CustomValue3"
        propagate_at_launch = true
      },
      {
        key = "CustomTag4"
        value = "CustomValue4"
        propagate_at_launch = true
      },
    ]
    

    然后,您的ASG将被标记为:

    explicit1 = value1
    explicit2 = value2
    interpolation1 = value3
    interpolation2 = value4
    CustomTag1 = CustomValue1
    CustomTag2 = CustomValue2
    CustomTag3 = CustomValue3
    CustomTag4 = CustomValue4
    

    这使您可以选择在ASG上始终存在一些标记,以及完全在您的模块或其他模块之外定义的可选动态标记。