#!/bin/bash
# helper to get an overview of all aws instances
set -ex

outfile=/tmp/instances_$(date +%Y-%m-%d).json
summary=${outfile}.summary

aws ec2 describe-instances > $outfile

cat $outfile | jq '.Reservations | map({
  "name": .["Instances"][0].Tags | map(select(.["Key"] == "Name")["Value"])[0],
  "id": .["Instances"][0].InstanceId, 
  "type": .["Instances"][0].InstanceType
})' > $summary
                                                                                                                                                                                                      
# on "Instances" field per list entry
cat $outfile | jq '.Reservations | map(.Instances | length)'

python<<EOF
from petl.fluent import fromjson
x = fromjson('$summary').sort('name').cut('id', 'name', 'type').convertall(lambda x: x.encode('utf-8'))
print x.lookall()
print "Total: {}".format(x.nrows())
print x.aggregate('type', len).rename({'key': 'type', 'value': 'count'}).sort('count', True).lookall()
EOF


