23

How can I get private IP adresses of all the instances which are part of an AutoScaling group. I am trying to do some operation on all the instances which are part of an autoscaling group.

Ramesh Kumar
  • 1,790

9 Answers9

26

I have written a small script like below to get the IP list:

#!/bin/bash
for i in `aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name ASGName | grep -i instanceid  | awk '{ print $2}' | cut -d',' -f1| sed -e 's/"//g'`
do
aws ec2 describe-instances --instance-ids $i | grep -i PrivateIpAddress | awk '{ print $2 }' | head -1 | cut -d"," -f1
done;
Ramesh Kumar
  • 1,790
24

As alternative, my version without any jq/awk/sed/cut

$ aws autoscaling describe-auto-scaling-instances --region us-east-1 --output text \
--query "AutoScalingInstances[?AutoScalingGroupName=='ASG-GROUP-NAME'].InstanceId" \
| xargs -n1 aws ec2 describe-instances --instance-ids $ID --region us-east-1 \
--query "Reservations[].Instances[].PrivateIpAddress" --output text
10.228.43.71
10.230.178.160
10.228.15.171
10.233.160.163
10.228.18.123
10.225.222.195
10.237.149.97
10.136.163.109
10.152.35.71
10.233.157.230

More optimized version

# aws ec2 describe-instances --region us-east-1 --instance-ids \
$(aws autoscaling describe-auto-scaling-instances --region us-east-1 --output text \
--query "AutoScalingInstances[?AutoScalingGroupName=='ASG_NAME'].InstanceId") \
--query "Reservations[].Instances[].PrivateIpAddress"
[
    "10.230.178.160",
    "10.152.35.71",
    "10.233.157.230",
    "10.237.149.97",
    "10.228.15.171",
    "10.136.163.109",
    "10.225.222.195",
    "10.233.160.163",
    "10.228.43.71",
    "10.228.18.123"
]

If you need just a plain list in the output you can add another pipeline

| jq -r '.[]'

kristi
  • 205
ALex_hha
  • 7,415
4

Take a look at the fine documentation for the AWS API. E.g. the aws-cli tools aws autoscaling describe-auto-scaling-instances and aws ec2 describe-instances.

mschuett
  • 3,216
1

Similar to Ramesh's answer here is a nice little script based on the current instance and its group. Make sure to set your region and in this case I skip the current instance (used for clustering). You can also change PrivateIpAddress to Public if required.

#!/bin/bash
wget http://s3.amazonaws.com/ec2metadata/ec2-metadata
sudo chmod u+x ec2-metadata
INSTANCE_ID=$(./ec2-metadata | grep instance-id | awk 'NR==1{print $2}')
AG_NAME=$(aws autoscaling describe-auto-scaling-instances --instance-ids ${INSTANCE_ID} --region eu-west-1 --query AutoScalingInstances[].AutoScalingGroupName --output text)
for ID in $(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-names ${AG_NAME} --region eu-west-1 --query AutoScalingGroups[].Instances[].InstanceId --output text);
do
    if [ "${ID}" == ${INSTANCE_ID} ] ; then
        continue;
    fi
    IP=$(aws ec2 describe-instances --instance-ids $ID --region eu-west-1 --query Reservations[].Instances[].PrivateIpAddress --output text)
    # Do what you want with ${IP} here
done
7wonders
  • 111
1

you can also use jq to parse the output, it is a bad idea to use awk, grep, or sed, etc, to parse a node structure, similar to it being a bad idea to use regular expressions to parse html.

$ aws ec2 describe-instances \
--instance-ids $(aws autoscaling describe-auto-scaling-groups \
    |jq -r '.AutoScalingGroups[]| select( .Tags[].Value == "playground").Instances[].InstanceId' \
    |paste -s -d" ") \
| jq -r '.Reservations[].Instances[].PrivateIpAddress'
192.169.0.202
192.169.0.177
192.169.0.160
0

Use a unique tag for your AutoScaling and check the option apply on Instances.

For i.e. Key: Prod Value: Server

Your AutoScaling instances will have this unique Tag. Run this below command to list all PrivateIP address:

aws ec2 describe-instances --filters Name=tag:Prod,Values=Server --region us-east-1 \
  --query 'Reservations[*].Instances[*][PrivateIpAddress]' --output text
0

You can also look in AWS web console UI under EC2 -> Auto Scaling Groups -> Instances Tab. You will see all the instances under current ASG, you can then click on each instance-ID to get the IP(It will redirect you to different view.)

mindblowwn
  • 27
  • 5
0
$instanceIPs = aws ec2 describe-instances --filters "Name=tag:Name,Values=<name-of-your-auto-scaling-group>" --query 'Reservations[].Instances[].PrivateDnsName' --output text

$instanceIPsArray = $instanceIPs.Trim() -split("`t")
foreach($ip in $instanceIPsArray)
{
    //Do something
}
Jeremy
  • 1
0

This will return all of the private ips of instances in an ASG

PRIVATEIPS=$(aws ec2 describe-instances --filters "Name=tag:aws:autoscaling:groupName,Values=$(aws autoscaling describe-auto-scaling-instances --instance-ids="$(ec2metadata --instance-id)" | jq -r '.AutoScalingInstances[].AutoScalingGroupName')" --query 'Reservations[].Instances[].PrivateIpAddress' --output text --region $(curl -s http://169.254.169.254/latest/meta-data/placement/availability-zone | sed 's/\(.*\)[a-z]/\1/'))
Josh
  • 101