Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tag instances launched from autoscaling using Terraform?

I'm using Terraform to setup a ECS cluster. This is my launch configuration:

resource "aws_launch_configuration" "launch_config" {
  name_prefix = "my_project_lc"
  image_id = "ami-ff15039b"
  instance_type = "t2.medium"
  user_data = "${data.template_file.user_data.rendered}"
  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "autoscaling_group" {
  name = "my_project_asg"
  max_size = 2
  min_size = 1
  launch_configuration = "${aws_launch_configuration.launch_config.name}"
  vpc_zone_identifier = ["${aws_subnet.public.id}"]
}

It's working fine but EC2 instance has no name (tag "Name"). How can I change my config in order to give instance a meaningful name? A prefix or something...

Thanks

like image 752
Héctor Avatar asked Oct 26 '25 10:10

Héctor


1 Answers

Yes, it is possible. See the documentation for aws_autoscaling_group resource. Example code:

resource "aws_autoscaling_group" "bar" {
  name = "my_project_asg"
  max_size = 2
  min_size = 1
  launch_configuration = "${aws_launch_configuration.launch_config.name}"
  vpc_zone_identifier = ["${aws_subnet.public.id}"]

  tag {
    key                 = "Name"
    value               = "something-here"
    propagate_at_launch = true
  }

  tag {
    key                 = "lorem"
    value               = "ipsum"
    propagate_at_launch = false
  }
}

Alternatively, you can use terraform-aws-autoscaling module which implements different types of tags.

like image 65
Anton Babenko Avatar answered Oct 29 '25 01:10

Anton Babenko



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!