How to automatically tag each ec2 under ASG with unique 'Name'?

1,196 views
Skip to first unread message

egul...@gmail.com

unread,
Jun 16, 2017, 11:07:35 AM6/16/17
to Terraform
Hello everyone,

I'm provisioning ec2 instances through ASG and would like to customize NAME TAG for each of these and have trouble figuring it out.
I tried using count.index and custom parameters but no luck.
As of now, each ec2 instance gets the same name which is not perfect. I would love to get something like:

 CACHE-1
 CACHE-2

based on the count of instances that I want to provision through ASG.



Here's how my code looks like so far:

resource "aws_autoscaling_group" "main_asg" {
  #count         = "${var.asg_number_of_instances}"
  instance_count = "{var.asg_number_of_instances}"
  depends_on = ["aws_launch_configuration.launch_config"]
  name       = "${var.asg_name}"
  availability_zones = ["${split(",", var.asg_azs)}"]
  vpc_zone_identifier = ["${split(",", var.asg_subnets)}"]
  launch_configuration = "${aws_launch_configuration.launch_config.id}"
  max_size                  = "${var.asg_number_of_instances}"
  min_size                  = "${var.asg_minimum_number_of_instances}"
  load_balancers            = ["${var.load_balancers}"]
  desired_capacity          = "${var.asg_number_of_instances}"
  health_check_grace_period = "${var.asg_health_check_grace_period}"
  health_check_type         = "${var.asg_health_check_type}"
  tag {
    key                 = "Name"
    value               = "${var.name}"
    #value               = "${var.name}-${count.index}"
    #value               = "${var.name}-${asg_number_of_instances.index}"
    propagate_at_launch = true
  }
}

Has anyone had similar issue and solved it somehow?

Any advice really appreciated.

Thank you,
Ernest

David Adams

unread,
Jun 16, 2017, 2:45:39 PM6/16/17
to terrafo...@googlegroups.com
There's no way to do this directly. It would have to happen on the AWS side. Each tag is simply copied (if propagate_at_launch is true) when the instance is launched. There's no way to build a template or anything. AWS support would probably suggest creating a Lambda that listens to an SNS topic that receives notifications of new ASG launches, and which sets the Name tag on the new instance according to whatever pattern you would like.

That's a bunch of pieces, and you can set up the entire workflow with Terraform if this feature is really important to you. But, I would recommend just using instance ID or something as a well-known, easy to identify, and globally unique identifier instead of a Name tag.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/05e95eea-f770-45e4-af8e-49405c4796cf%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Phil S

unread,
Jun 18, 2017, 1:33:13 AM6/18/17
to Terraform
This actually doesn't make sense in practice if you think it through.  What do you want it to do if it destroys and replaces a dead instance?  How will it know which number to assign?

If you want to have a unique name, you could have the instance do it itself.  e.g. give the instance a role that allows it to update its tags, then in the userdata script, or in other automation you have, use the CLI to update the tag to whatever you want.

Phil
Reply all
Reply to author
Forward
0 new messages