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.


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

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'`
aws ec2 describe-instances --instance-ids $i | grep -i PrivateIpAddress | awk '{ print $2 }' | head -1 | cut -d"," -f1
  • upvoting with all my hands – Jameel Grand Sep 28 '17 at 6:33
  • downvote because it is unwise to parse json with grep and awk – xenoterracide Dec 12 '17 at 23:26
  • you can use jq command to parse json – Chase T. Apr 11 '19 at 15:48
  • Perfect answer, thank you. – John Humphreys - w00te Jul 12 '19 at 17:38

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

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"

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

| jq -r '.[]'

  • 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 – xenoterracide Dec 13 '17 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 '17 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 – xenoterracide Dec 13 '17 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 '17 at 23:45
  • Yes I know so I was just suggesting that you can make the queries more efficient – xenoterracide Dec 13 '17 at 23:48

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.


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.

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);
    if [ "${ID}" == ${INSTANCE_ID} ] ; then
    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

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'
  • JFYI: aws cli alredy can filter json via --query option. – ALex_hha Dec 12 '17 at 23:32
  • Doesn't seem to work exactly the same way – xenoterracide Dec 12 '17 at 23:48
  • but yes, that's another way to do it. – xenoterracide Dec 13 '17 at 15:42
  • just take a look at my version – ALex_hha Dec 13 '17 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 – xenoterracide Dec 19 '17 at 21:18

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.)

  • 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. – Ramesh Kumar Jul 11 '15 at 9:49
  • Below is the small script that I wrote to get the IP list. – Ramesh Kumar Jul 13 '15 at 8:03
  • in that case i would use cli, you are correct. – mindblowwn Jul 13 '15 at 17:31
$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

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 | sed 's/\(.*\)[a-z]/\1/'))

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.