Last active
January 20, 2017 16:14
-
-
Save timdp/0a6b9c1d076b3dfa02bc9d0f5ef1954c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -e | |
NAME=$1 | |
[[ -z $NAME ]] && { | |
echo "Usage: $0 INSTANCE_NAME" >&2 | |
exit 1 | |
} | |
REGIONS='us-east-1 us-west-1 eu-west-1' | |
declare -a instance_ids | |
declare -a instance_ips | |
declare -a instance_regions | |
count=0 | |
for region in $REGIONS; do | |
while read key value; do | |
[[ $key = 'INSTANCES' ]] || continue | |
set -- $value | |
shift 8 | |
[[ $7 =~ ^[0-9.]+$ ]] || continue | |
(( ++count )) | |
instance_regions[count - 1]=$region | |
instance_ids[count - 1]=$1 | |
instance_ips[count - 1]=$7 | |
done < <( aws ec2 describe-instances --filters "Name=tag:Name,Values=$NAME" --region $region --output text ) | |
done | |
(( count == 0 )) && { | |
echo "Instance named $NAME not found" >&2 | |
exit 2 | |
} | |
(( count > 1 )) && { | |
echo "Multiple instances named $NAME found:" >&2 | |
for (( i = 0; i < count; ++i )); do | |
echo "${instance_regions[i]} ${instance_ids[i]} ${instance_ips[i]}" >&2 | |
done | |
exit 3 | |
} | |
echo "${instance_regions[i]} ${instance_ids[i]} ${instance_ips[i]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment