<p>For an <code>auto-scaling-group</code> this is the most concise syntax available.</p>

<p>For most other resources you use the <code>tags</code> syntax which looks like:</p>

<pre><code>tags {
  Key1 = "value1"
  Key2 = "value2"
}
</code></pre>

CC BY-SA 4.0
2023-03-21T05:43:23.947Z
The [documentation](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group#tag-and-tags) says that `tags` is deprecated and should be replaced by `tag`. So in this case, dynamic tagging seems to be the best solution as in the answer https://devops.stackexchange.com/a/8714/39631.
Romain
CC BY-SA 3.0
2017-09-22T08:36:54.087Z
2017-09-22T08:36:54.087Z
Briansbum
<p>Terraform v0.12 added support for Dynamic Nested Blocks. The following example is derived from their <a href="https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each" rel="noreferrer">blog post</a> about the new features (see section <a href="https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each#dynamic-nested-blocks" rel="noreferrer">Dynamic Nested Blocks</a>):</p>

<pre><code>locals {
  standard_tags = {
    Name    = var.cluster_prefix
    Owner   = var.tag_Owner
    Project = var.tag_Project
  }
}

resource "aws_autoscaling_group" "example" {
  # ...

  dynamic "tag" {
    for_each = local.standard_tags

    content {
      key                 = tag.key
      value               = tag.value
      propagate_at_launch = true
    }
  }
}
</code></pre>

CC BY-SA 4.0
2019-07-25T23:10:57.940Z
2019-07-25T23:10:57.940Z
Philipp Claßen
<p>Another option is to use the <a href="https://github.com/cloudposse/terraform-null-label" rel="nofollow noreferrer"><code>terraform-null-label</code></a> module. It supports passing a variable called <code>tags</code> as a standard Terraform map. The module then emits an output called <code>tags_as_list_of_maps</code> which contains the tags in the format you want. But an even better reason to use this module is to generate a consistent set of resource names that follow a fixed convention. </p>

<p>For example, you can do this:</p>

<pre><code>module "example" {
  source     = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
  namespace  = "eg"
  stage      = "prod"
  name       = "bastion"
  tags       = { 
                 "BusinessUnit" = "XYZ" 
                 "Snapshot" = "true"
               }
}
</code></pre>

<p>So from your example, we can then write something like this:</p>

<pre><code>resource "aws_autoscaling_group" "instance" {
  ...
  name = "${module.example.id}"
  ...
  tags = "${module.example.tags_as_list_of_maps}"
  ...
}
</code></pre>

<p><strong>NOTE:</strong> I added the <code>name</code> field to show how to generate co</p>

CC BY-SA 4.0
2019-06-27T15:27:41.617Z
What would be the best way to make "name" uniq to each ressource? if you use the null-label module every "name" on the ressources is the same. What if you want to name your subnets with a suffix like -private-subnet? Is there a way the null-label can handle that?
Izac
CC BY-SA 4.0
2018-07-27T03:17:15.333Z
2018-07-27T03:17:15.333Z
Erik Osterman
<p>The <code>aws_autoscaling_group</code> does support a list of tags now (<a href="https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html#tags" rel="nofollow noreferrer">https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html#tags</a>). But this looks a bit different to the syntax of other terraform resources:</p>

<pre><code>tags = [
  {
    key                 = "explicit1"
    value               = "value1"
    propagate_at_launch = true
  },
  {
    key                 = "explicit2"
    value               = "value2"
    propagate_at_launch = true
  },
]
</code></pre>

<p>This also allows to dynamically build tags via interpolation.</p>

CC BY-SA 3.0
2018-01-25T00:17:57.223Z
2018-01-25T00:17:57.223Z
Fionn
<p>Here is the best way to do now</p>
<p>To be able to</p>
<ol>
<li>Attach the same common tags to all resources</li>
<li>While still being able to add custom tags to individual resources.</li>
</ol>
<p>You can add the default tag section in terraform provider section</p>
<pre><code># Configure the AWS Provider
provider &quot;aws&quot; {
  region  = var.aws_region
  profile = var.aws_profile

  default_tags {
    tags = {
      environment = var.tag_environment
      created_by  = var.tag_created_by
    }
  }

}
</code></pre>
<p>You can check the read the docs <a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/resource-tagging#propagating-tags-to-all-resources" rel="nofollow noreferrer">here</a></p>

CC BY-SA 4.0
2023-03-05T18:47:16.090Z
2023-03-05T18:47:16.090Z
Shivam Anand
<p>To attach multiple tags to a resource, I currently use multiple <code>tag</code> blocks:</p>

<pre><code>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
  }
}
</code></pre>

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

<p><strong>Update:</strong> The question was written before Terrafrom v0.12 came out. Since then the support has improved with <a href="https://www.hashicorp.com/blog/hashicorp-terraform-0-12-preview-for-and-for-each#dynamic-nested-blocks" rel="noreferrer">Dynamic Nested Blocks</a> (see my <a href="https://devops.stackexchange.com/a/8714/2708">answer below</a>).</p>

<terraform>
Terraform: Is there a concise syntax for specifying multiple tags for a resource?
Philipp Claßen
# Terraform: Is there a concise syntax for specifying multiple tags for a resource?

(Asked by: username_0 on 2017-09-22)

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).

**Tags:** terraform


---

## 5 answers

### Answer by username_1 on 2017-09-22. Score: 9 | Accepted answer

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

> **Comments:**
> **username_2**: The [documentation](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group#tag-and-tags) says that `tags` is deprecated and should be replaced by `tag`. So in this case, dynamic tagging seems to be the best solution as in the answer https://devops.stackexchange.com/a/8714/39631.  

---

### Answer by username_0 on 2019-07-25. Score: 6

Terraform v0.12 added support for Dynamic Nested Blocks. The following example is derived from their blog post about the new features (see section Dynamic Nested Blocks):
locals {
  standard_tags = {
    Name    = var.cluster_prefix
    Owner   = var.tag_Owner
    Project = var.tag_Project
  }
}

resource "aws_autoscaling_group" "example" {
  # ...

  dynamic "tag" {
    for_each = local.standard_tags

    content {
      key                 = tag.key
      value               = tag.value
      propagate_at_launch = true
    }
  }
}


---

### Answer by username_3 on 2018-07-27. Score: 2

Another option is to use the terraform-null-label module. It supports passing a variable called tags as a standard Terraform map. The module then emits an output called tags_as_list_of_maps which contains the tags in the format you want. But an even better reason to use this module is to generate a consistent set of resource names that follow a fixed convention. 
For example, you can do this:
module "example" {
  source     = "git::https://github.com/cloudposse/terraform-null-label.git?ref=master"
  namespace  = "eg"
  stage      = "prod"
  name       = "bastion"
  tags       = { 
                 "BusinessUnit" = "XYZ" 
                 "Snapshot" = "true"
               }
}

So from your example, we can then write something like this:
resource "aws_autoscaling_group" "instance" {
  ...
  name = "${module.example.id}"
  ...
  tags = "${module.example.tags_as_list_of_maps}"
  ...
}

NOTE: I added the name field to show how to generate co

> **Comments:**
> **username_4**: What would be the best way to make "name" uniq to each ressource? if you use the null-label module every "name" on the ressources is the same. What if you want to name your subnets with a suffix like -private-subnet? Is there a way the null-label can handle that?  

---

### Answer by username_5 on 2018-01-25. Score: 1

The aws_autoscaling_group does support a list of tags now (https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html#tags). But this looks a bit different to the syntax of other terraform resources:
tags = [
  {
    key                 = "explicit1"
    value               = "value1"
    propagate_at_launch = true
  },
  {
    key                 = "explicit2"
    value               = "value2"
    propagate_at_launch = true
  },
]

This also allows to dynamically build tags via interpolation.


---

### Answer by username_6 on 2023-03-05. Score: 0

Here is the best way to do now
To be able to

Attach the same common tags to all resources
While still being able to add custom tags to individual resources.

You can add the default tag section in terraform provider section
# Configure the AWS Provider
provider "aws" {
  region  = var.aws_region
  profile = var.aws_profile

  default_tags {
    tags = {
      environment = var.tag_environment
      created_by  = var.tag_created_by
    }
  }

}

You can check the read the docs here


---

