Arcones
Arcones

Reputation: 4692

Get public DNS for aws_autoscaling_group instances

I want to output the public DNS of the EC2s that make up my auto scaling group:

resource "aws_launch_configuration" "instances" {
  image_id        = "ami-0fad7824ed21125b1"
  instance_type   = "${var.instance_type}"
  security_groups = ["${aws_security_group.security_group_ec2.id}"]
  key_name        = "${var.key_pair_name}"

  user_data = "${data.template_file.user_data.rendered}"

  lifecycle {
    create_before_destroy = true
  }
}

resource "aws_autoscaling_group" "scaling_group" {
  launch_configuration = "${aws_launch_configuration.instances.id}"
  availability_zones   = ["${var.availability_zones_names}"]

  load_balancers    = ["${var.elb_id}"]
  health_check_type = "ELB"

  min_size = "${var.min_size}"
  max_size = "${var.max_size}"

  tags = {
    key                 = "Name"
    value               = "terraformUpAndRunning-${var.cluster_name}"
    propagate_at_launch = true
  }

  wait_for_capacity_timeout = "5m"
}

I have checked the auto scaling group attributes in official Terraform documentation but can't think in any of those to get my aim... Is there any way?

Upvotes: 1

Views: 769

Answers (1)

thevilledev
thevilledev

Reputation: 2387

Instances that are managed by an auto-scaling group are not managed by Terraform. Therefore it's unwise for Terraform to keep track of those ephemeral instances in it's state files. Ephemeral instances will come and go by their nature.

However, if you really want to use Terraform for this purpose there is a data source called aws_instances. This gives you the ability to query and list multiple instances.

You could also use awscli to print out the instance details. Here's an example of that: Getting a list of instances in an EC2 auto scale group?.

Upvotes: 2

Related Questions