title: Module with dynamic tags on Auto Scaling Group and EFS
body: <p>I would like to get dynamic tags on an <a href="https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html" rel="nofollow noreferrer">aws_autoscaling_group</a> resource, but unlike the example in the documentation I do not yet now how many there will be.</p>
  
  <p>So the following lines are set:</p>
  
  <pre><code>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                 = &lt;code_here&gt;
  }
  </code></pre>
  
  <p>I've come up with a solution that will support up to <code>x</code> dynamic tags. Unfortunately it uses dummy tags to fill up not provided tags up to <code>x</code>.</p>
  
  <pre><code>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]),
            )}"
  }
  </code></pre>
  
  <p>With this code in the ASG I can use <code>tags = ["${local.tags}"]</code>. With the example input the resources are tagged with</p>
  
  <pre><code>tag1 = value1
  tag2 = value2
  unusedtag1 = 
  unusedtag2 = 
  unusedtag3 = 
  unusedtag4 = 
  unusedtag5 =
  unusedtag6 = 
  unusedtag7 =  
  </code></pre>
  
  <p>I would like to have a solution that is </p>
  
  <ul>
  <li>dynamic</li>
  <li>does tag extra tags</li>
  </ul>
  
  <hr>
  
  <p>Edit: revert to unclear but simpler question and opened a new question on <a href="https://stackoverflow.com/questions/51207505/terraform-dynamic-tags-on-asg-and-other-resources-like-efs">terraform dynamic tags on ASG and other resources like EFS</a></p>