Auto Scaling Group和EFS上具有动态标签的模块

时间:2018-03-02 08:12:39

标签: amazon-web-services terraform terraform-provider-aws

我想在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 个答案:

答案 0 :(得分:0)

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

如果您通过tfvars覆盖extra_tags,例如:

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上的标签以及完全在模块外部定义的可选动态标签。