Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup launch template with tag_secifications with multiple resources

base on the documentation example given below I see the tag set for instance type. But If I want the same tag to be applied to multiple resources then how would I set it up https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template

  tag_specifications {
    resource_type = "instance"

    tags = {
      Name = "test"
    }
  }

enter image description here

like image 520
kumar Avatar asked Jan 20 '26 09:01

kumar


1 Answers

You either specify it twice or use dynamic blocks. Example with dynamic blocks is:

variable "to_tag" {
  default = ["instance", "volume"]
}


resource "aws_launch_template" "foo" {
  name = "foo"

  image_id = data.aws_ami.server.id

  instance_type = "t2.micro"
  
  dynamic "tag_specifications" {
    for_each = toset(var.to_tag)
    content {
       resource_type = tag_specifications.key
       tags = {
         Name = "test"
       }
    }
  } 
}

or simply specify it twice:

resource "aws_launch_template" "foo" {
  name = "foo"

  image_id = data.aws_ami.server.id

  instance_type = "t2.micro"
  

  tag_specifications {
     resource_type = "instance"
     tags = {
       Name = "test"
    }
  } 

  tag_specifications {
     resource_type = "volume"
     tags = {
       Name = "test"
    }
  }

}
like image 60
Marcin Avatar answered Jan 23 '26 01:01

Marcin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!