1 minute read

AWS CLI powered with JQ

With growing list of companies moving to AWS PAAS solutions, deploying services to cloud is easy, and within matter of days you might grow your servers (EC2) in cloud and AWS console is one way of looking at your resource or use CLI to list.

In this blog I want to show how I used AWS CLI and JQ to solve one of the problem I encounter daily and how I solved it.

jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text

AWS CLI to list EC2 resources is nice one, but in my case they are too many and unless I put filters in with the CLI command there is no way to filter resources and even if I could filter them CLI response is overwhelming in terms of elements it returns and no way to see them in a nice table like structure.

That’s when I realized the power of JQ which helped me to filter my components in a tabular format I wanted with the fields I choose to.

echo "Describing EC2 instances that are running, and having tags ABC=XYZ and TUV=123"
  aws ec2 describe-instances --filters Name=tag:Purpose,Values="123" Name=tag:ABC,Values=XYZ | 
  jq '.Reservations[].Instances[] | {status: .State.Name, IP: .PrivateIpAddress, SDLC: (.Tags[] | 
  select(.["Key"] == "SDLC")| .Value) } | select(.status == "running") | .IP + " " + .SDLC' | tr -d '"'

JQ here is helping me to parse json and displaying it as a little table. With the above example I am able to display private IP for the component I filter based on the tags.

mymachine:~ myid$ ec2 ABC
Describing EC2 instances that are running, and having tags AGS=XYX and Purpose=ABC
90.162.71.179 DEVELOPMENT
90.162.73.173 INTEGRATION
90.162.70.246 INTEGRATION

Tags: ,

Categories:

Updated: