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
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;
- 1,690
- 5
- 18
- 29
-
upvoting with all my hands – Jameel Grand Sep 28 '17 at 06: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 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
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 '.[]'
-
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
-
Yes, thanks. I have updated my answer. The second version is much faster – ALex_hha Dec 13 '17 at 23:51
-
upvoted 'cause I think this is the most correct answer depending on needs, actually can't you just put `--output text` on the outer command to get it to give you a plain list? – xenoterracide Dec 19 '17 at 21:13
-
I suggest adding -r to the xargs command otherwise the ec2 describe instances will return all IPs within the region if the auto scaling group is not found – Matt Ball Jul 11 '19 at 22:34
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.
- 3,066
- 20
- 21
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
- 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
- 1,476
- 2
- 12
- 26
-
-
-
-
-
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
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
- 8,561
- 21
- 31
- 47
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.)
- 27
- 5
-
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 09:49
-
-
$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
}
- 1
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/'))
- 101
- 3