How to get Autoscaling group Instances IP adresses

Solution 1:

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;

Solution 2:

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

Solution 3:

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.