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.

9 Answers 9

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;
3
  • 1
    upvoting with all my hands Sep 28, 2017 at 6:33
  • downvote because it is unwise to parse json with grep and awk Dec 12, 2017 at 23:26
  • you can use jq command to parse json
    – Chase T.
    Apr 11, 2019 at 15:48
23

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 '.[]'

8
  • this creates many "describe-instances" requests, mine only creates one I mean, you could make it only create twom, but you'd have to stop using xargs Dec 13, 2017 at 22:52
  • this creates many "describe-instances" requests and? As far as I understood your query will work only with tags it's not universal, imho
    – ALex_hha
    Dec 13, 2017 at 23:29
  • Yours isn't Universal either I can't actually use the name in my queries it's a generated name that's part of an automated system. Point is this creates a 10 plus 1 RPC requests therefore it will be slow depending on how many instances you have Dec 13, 2017 at 23:33
  • Actually OP didn't specify by what criteria he need to make a search, so it's a grey area :)
    – ALex_hha
    Dec 13, 2017 at 23:45
  • Yes I know so I was just suggesting that you can make the queries more efficient Dec 13, 2017 at 23:48
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.

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
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
5
  • JFYI: aws cli alredy can filter json via --query option.
    – ALex_hha
    Dec 12, 2017 at 23:32
  • Doesn't seem to work exactly the same way Dec 12, 2017 at 23:48
  • but yes, that's another way to do it. Dec 13, 2017 at 15:42
  • just take a look at my version
    – ALex_hha
    Dec 13, 2017 at 21:29
  • I'm going to leave this here, because even if it's not the best answer, I think jq is a useful tool, and leaving it here may let ops people discover it, even if with amazon you can do it all with the aws command Dec 19, 2017 at 21:18
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.)

3
  • Yes, But I want the complete list, I have ASG with more than 100 instances and want a list of IP's to apply some stuff. Jul 11, 2015 at 9:49
  • Below is the small script that I wrote to get the IP list. Jul 13, 2015 at 8:03
  • in that case i would use cli, you are correct.
    – mindblowwn
    Jul 13, 2015 at 17:31
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
}
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/'))
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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .