'Get Private IP from ASG

I need to output private IP from Auto Scaling Group of EC2 instance that configuration by Terraform. How can i get private IP from ASG?

# Launch config for ASG
resource "aws_launch_configuration" "asg_conf" {
  name            = "ASG-Conf-${var.env}"
  image_id        = data.aws_ami.ubuntu.id                # Get image from data
  instance_type   = var.instance_type                     # use t2.micro ad default
  security_groups = [var.sg_app.id, var.sg_alb.id]        # SG for APP
  key_name        = var.ssh_key                           # SSH key for connection to EC2
  user_data       = file("./modules/ec2/shell/apache.sh") # install apache

  lifecycle {
    create_before_destroy = true
  }
}

# Auto Scaling Group
resource "aws_autoscaling_group" "asg" {
  count                = length(var.private_subnet_id) # count numbers of private subnets
  name                 = "ASG-${var.env}-${count.index + 1}"
  vpc_zone_identifier  = [var.private_subnet_id[count.index]]
  launch_configuration = aws_launch_configuration.asg_conf.name
  target_group_arns    = [var.alb_target.arn]

  min_size         = 1 # Min size of creating EC2
  max_size         = 1 # Max size of creating EC2

  health_check_grace_period = 120
  health_check_type         = "ELB" 
  force_delete              = true  

  lifecycle {
    create_before_destroy = true
  }

  tag {
    key                 = "Name"
    value               = "Webserver-ec2-${count.index + 1}"
    propagate_at_launch = true
  }
}

output {
  # How can i get this???
  value = aws_autoscaling_group.asg.private_ip
}

My infrastructure create 1 EC2 instance in 2 private AZ by ASG and i need output IP from this ASG



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source