Skip to content

Instantly share code, notes, and snippets.

@vkhatri
Created January 31, 2018 17:20
Show Gist options
  • Save vkhatri/7ea07699fe97dcd01460ab6b9a2b3faa to your computer and use it in GitHub Desktop.
Save vkhatri/7ea07699fe97dcd01460ab6b9a2b3faa to your computer and use it in GitHub Desktop.
list aws ec2 autoscaling group instances
#!/bin/bash
PEER_LIST_FILE=/etc/asg-peer-instances
SELF_IP=$(ifconfig eth0 | grep -o "inet .* netmask" | cut -f2 -d' ')
SELF_INSTANCE_ID=$(curl http://169.254.169.254/latest/meta-data/instance-id)
SELF_PLACEMENT=$(curl http://169.254.169.254/latest/meta-data/placement/availability-zone)
SELF_REGION="${SELF_PLACEMENT%?}"
SELF_ASG=$(aws ec2 describe-tags --filters "Name=resource-id,Values=${SELF_INSTANCE_ID}" --region $SELF_REGION --query 'Tags[?Key==`aws:autoscaling:groupName`].Value' --output text)
ASG_INSTANCE_IDS=$(aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name $SELF_ASG --region $SELF_REGION --query 'AutoScalingGroups[*].Instances[*].InstanceId' --output text | xargs)
AWS_INSTANCE_IPS=$(aws ec2 describe-instances --instance-ids $ASG_INSTANCE_IDS --region $SELF_REGION --query 'Reservations[*].Instances[*].PrivateIpAddress' --filters 'Name=instance-state-name,Values=running' --output text)
PEER_LIST=$(echo $AWS_INSTANCE_IPS | sed "s/${SELF_IP}//g" | xargs)
echo $PEER_LIST > $PEER_LIST_FILE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment