Skip to content

Instantly share code, notes, and snippets.

@guyromm
Created January 17, 2019 11:44
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 guyromm/12927aa40aaa06c54f08e0123def6c7a to your computer and use it in GitHub Desktop.
Save guyromm/12927aa40aaa06c54f08e0123def6c7a to your computer and use it in GitHub Desktop.
AWS instance state collection & merge script. requires python3 & awscli
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
REG=$(aws configure get region)
OPDIR=$DIR/$REG/
function retrieve() {
mkdir -p $OPDIR && \
cd $OPDIR && \
(aws ec2 describe-volumes | tee $OPDIR/ec2-describe-volumes.json | wc) && \
(aws ec2 describe-instances | tee $OPDIR/ec2-describe-instances.json | wc) && \
(aws ec2 describe-addresses | tee $OPDIR/ec2-describe-addresses.json | wc) && \
(aws elb describe-load-balancers | tee $OPDIR/elb-describe-load-balancers.json | wc) && \
echo 'all done retrieving!'
}
function cat_instances() {
cat $OPDIR/ec2-describe-instances.json | jq -c '.Reservations[].Instances|.[]|.'
}
function instance_names() {
jq -c '.Tags|map(select(.Key | contains ("Name")))[].Value'
}
function instance_table_header() {
echo '["InstanceId","InstanceName","InstanceType","SecurityGroup","NetGroup"]'
}
function instance_table_body() {
jq -r '[.InstanceId,
(.Tags|map(select(.Key | contains ("Name")))[].Value)
,.InstanceType
,([.SecurityGroups[].GroupName]|join(";"))
,([.NetworkInterfaces[].Groups[].GroupName]|join(";"))
]'
}
function load_balancers() {
cat $OPDIR/elb-describe-load-balancers.json \
| jq '.LoadBalancerDescriptions[]' \
| jq -c '.Instances[].InstanceId as $i | .LoadBalancerName as $lb | [$i,$lb]'
}
function instance_table() {
(instance_table_header && \
cat_instances \
| instance_table_body) \
| jq -r '@tsv'
}
function balancers_instances_table_header() {
echo '["InstanceId","InstanceName","InstanceType","SecurityGroup","NetGroup","Balancers"]'
}
function balancers_instances_table() {
(
balancers_instances_table_header && \
balancers_instances \
| python3 $DIR/join.py) \
| jq -r '@tsv'
}
function balancers_instances() {
echo "{"'"'"balancers"'"'":$(load_balancers | jq -r -s '@json'),"'"'"instances"'"'":$(cat_instances | instance_table_body | jq -s -r '@json')}"
}
# function instances() {
# jq -c '.Tags' | jq -c '.Groups' #,map(select(.Key | contains ("Name")))[].Value'
# }
function analyze() {
cd $OPDIR && \
echo '*** BALANCERS' && \
(cat elb-describe-load-balancers.json \
| jq '.LoadBalancerDescriptions[]|[.LoadBalancerName,.Instances[].InstanceId]' \
| tee balancers-digest.txt) && \
echo '*** IMAGE TYPES' && \
( cat ec2-describe-instances.json \
| jq '.Reservations[].Instances|.[]|.' \
| grep -i imageid \
| sort \
| uniq -c \
| sort -n \
| tee imagetypes.txt) && \
echo '*** NETWORK GROUPS' && \
( cat ec2-describe-instances.json \
| jq -c '.Reservations[].Instances|.[]|.NetworkInterfaces[].Groups' \
| sort \
| uniq -c \
| sort -n \
| tee networkgroups.txt) && \
echo '*** SECURITY GROUPS' && \
( cat ec2-describe-instances.json \
| jq -c '.Reservations[].Instances|.[]|.SecurityGroups' \
| sort \
| uniq -c \
| sort -n \
| tee securitygroups.txt) && \
echo '*** overview' && \
( balancers_instances_table > $OPDIR/$REG.tsv ) && \
echo 'all done analyzing.' && \
cd -
}
function run() {
retrieve && analyze
}
if [[ "$0" != "-bash" ]] && [[ "$0" != "bash" ]] ; then
run
else
echo "not running - script was sourced."
fi
#!/usr/bin/env python
import json,sys,collections
i = json.loads(sys.stdin.read())
balancers = collections.defaultdict(list)
for iid,bname in i['balancers']:
balancers[iid].append(bname)
for row in i['instances']:
row.append(','.join(balancers[row[0]]))
print(json.dumps(row))
#print(json.dumps(i['instances']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment