HiveBrain v1.2.0
Get Started
← Back to all entries
patternterraformMinor

Terraform: Is there a concise syntax for specifying multiple tags for a resource?

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
specifyingresourceconcisesyntaxtagsformultiplethereterraform

Problem

To attach multiple tags to a resource, I currently use multiple tag blocks:

resource "aws_autoscaling_group" "instance" {
  ...

  tag {
    key                 = "Name"
    value               = "${var.cluster_prefix}"
    propagate_at_launch = true
  }

  tag {
    key                 = "Owner"
    value               = "${var.tag_Owner}"
    propagate_at_launch = true
  }

  tag {
    key                 = "Project"
    value               = "${var.tag_Project}"
    propagate_at_launch = true
  }
}


It works, but I wonder if there is a more concise syntax. It is also a bit error prone as you have to specify propagate_at_launch for each tag.

Update: The question was written before Terrafrom v0.12 came out. Since then the support has improved with Dynamic Nested Blocks (see my answer below).

Solution

For an auto-scaling-group this is the most concise syntax available.

For most other resources you use the tags syntax which looks like:

tags {
  Key1 = "value1"
  Key2 = "value2"
}

Code Snippets

tags {
  Key1 = "value1"
  Key2 = "value2"
}

Context

StackExchange DevOps Q#2115, answer score: 9

Revisions (0)

No revisions yet.