Skip to content

Instantly share code, notes, and snippets.

@subfission
Last active October 2, 2018 21:59
Show Gist options
  • Select an option

  • Save subfission/aa53a9fd6eeac8a80bebe2e55b7c69df to your computer and use it in GitHub Desktop.

Select an option

Save subfission/aa53a9fd6eeac8a80bebe2e55b7c69df to your computer and use it in GitHub Desktop.
AWSCLI | Python3
#!/bin/bash
# AWSCLI | Python3 (aws cli command pipe to python3)
#
# This magic one-liner will build a beautiful table view of your AWS ec2 assets.
# Requires Python3 & Tabulate
BOLD="$(tput bold)"
ULINE="$(tput sgr 0 1)"
END="$(tput sgr0)"
echo
echo "${ULINE}Written by Zach Jetson${END}"
echo
[ -z $1 ] && TABLE_FMT="fancy-grid" || TABLE_FMT="$1"
aws ec2 describe-instances \
--query 'Reservations[*].Instances[*].[Placement.AvailabilityZone,InstanceId,Platform,PrivateDnsName,PrivateIpAddress,PublicDnsName,PublicIpAddress,State.Name,Tags]' \
--output json | \
python3 -c $'import json,sys,tabulate\nj=json.load(sys.stdin)\ndef f(b=None):return b["Key"]+":"+b["Value"] if b else list()\nprint(tabulate.tabulate([(x[0][0],x[0][1],x[0][2],x[0][3],x[0][4],x[0][5],x[0][6],x[0][7],"\\n".join([f(m) for m in x[0][8] if x[0][8] is not None] if x[0][8] else list()) ) for x in j if j is not None ], headers=["Zone","InstanceId","Platform","PrivateDnsName","PrivateIpAddress","PublicDnsName","PublicIpAddress","State","Tags"], tablefmt="'$TABLE_FMT'"));'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment