jq | aws cli

But I found a good tool for manipulating JSON from command line. JQ. JQ enables us to filter, map and count etc agaist JSON data. For example
$ cat sample.json
{
    "id": "123456",
    "name": "Kai Sasaki",
    "address": "Tokyo",
    "language": [
        "Japanese",
        "English",
        "Java"
    ]
}
You can filter “id” with
$ cat sample.json | jq '.id'
"123456"
Count the number of language
$ cat sample.json | jq '.language | length'
3
So you can filter and map the running instance IDs which is tagged as web api server to csv like this.
$ aws ec2 describe-instances \
  --filter 'Name=tag:Name,Values=api' 'Name=instance-state-name,Values=running' | \
  jq -r '.Reservations[].Instances[] | [.InstanceId, .PrivateIpAddress, .Tags[].Value] | @csv'
After converted csv or line based data, you can use UNIX command line tools as you like for checking statistics. Since JQ has a lot of useful options and functionalities, I want to be familiar with tool more. Please check the official documentation for more detail.

Comments

Popular posts from this blog

HAproxy logging

tomcat catalina coyote jasper cluster

NFS mount add in fstab _netdev instead of default | firewall-cmd --list-all