Cheat Sheets

Filter AWS EC2 JSON with jq

aws ec2 describe-instances --filters "Name=instance-type,Values=m1.small,m1.medium" "Name=availability-zone,Values=us-west-2c"
you might need to do dozens of queries to find different infos. And in the end you still have to do the JSON parsing on all of the results, despite just wanting results like some IP or some tags or instance states... So why not issue
aws ec2 describe-instances >output.json
and use the mad jq syntax. Remember jq? The awesome command line tool that forgot all about XPath or jquery like DOM lookup syntax, that at least some people find intuitive and can use, and invented an even more sick filter language and has a manpage from which simply no one can extract any results from? Well it is still a useful CLI tool available in most Linux distros. So why not use it? For example: extract all external EC2 names for a given tag $mytag from our cached JSON:
cat output.json | jq -r ".Reservations[].Instances[] | select(.Tags | length > 0) | select( .Tags[].Value == \""$mytag"\") | {PublicDnsName} | flatten | .[0]"
If you analyze the query you might notice Easy right? No JSON parsing at all! If you dare to script using AWS CLI and jq check out the and the online test tool jqplay.

Comment on Disqus