Skip to content

Instantly share code, notes, and snippets.

@MJ111
Created April 9, 2019 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MJ111/3a8e45f2474d54ae8734df70f6764325 to your computer and use it in GitHub Desktop.
Save MJ111/3a8e45f2474d54ae8734df70f6764325 to your computer and use it in GitHub Desktop.
[Get elasticbeanstalk's ec2 private ip list]
#!/usr/bin/env bash
beantalkEnvName="$1"
echo "target beanstalk env name: ${beantalkEnvName}"
## fetch ec2 list
getEc2IpList () {
ec2List=$(aws elasticbeanstalk describe-environment-resources --environment-name ${beantalkEnvName} | python -m json.tool | python -c 'import sys, json; li = json.load(sys.stdin)["EnvironmentResources"]["Instances"]; ec2List = [ dat.values() for dat in li ]; flat_list = [item for sublist in ec2List for item in sublist]; strlist = map(str, flat_list); print " ".join(strlist)')
echo "${beantalkEnvName}'s ec2 id list"
ipListStr=$(aws ec2 describe-instances --instance-ids $ec2List --query "Reservations[*].Instances[*].PrivateIpAddress")
ipListWithDelimeter=$(echo $ipListStr | python -m json.tool | python -c 'import sys, json; li = json.load(sys.stdin); flat_list = [item for sublist in li for item in sublist]; strlist = map(str, flat_list); print ", ".join(strlist)')
IFS=', ' read -r -a ip2Array <<< "$ipListWithDelimeter"
for ec2ip in "${ip2Array[@]}"
do
:
echo $ec2ip
# do whatever on $i
done
}
getEc2IpList
# ex) user@ip-192-31-35-19:~$ ./getPrivateIpList.sh dsp2
# target beanstalk env name: eb1
# eb1's ec2 id list
# 172.39.22.111
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment