Héctor
Héctor

Reputation: 26034

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

Upvotes: 4

Views: 3312

Answers (1)

Anton Babenko
Anton Babenko

Reputation: 6636

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.

Upvotes: 5

Related Questions