Skip to content

Instantly share code, notes, and snippets.

@ajayk
Forked from johntoups/jq_examples.md
Created September 26, 2019 23:22
Show Gist options
  • Save ajayk/938c1251cb8013912ad0eec6f5764eb2 to your computer and use it in GitHub Desktop.
Save ajayk/938c1251cb8013912ad0eec6f5764eb2 to your computer and use it in GitHub Desktop.
aws-cli + jq

If you're coming to this page in search of jq related AWS commands, but come from xpath or e4x (jsonpath) domains, then you may find this article helpful: https://github.com/stedolan/jq/wiki/For-JSONPath-users.

Another good learning site: https://shapeshed.com/jq-json/

The jq playground (live testing): https://jqplay.org

A lovely tutorial: https://programminghistorian.org/lessons/json-and-jq

Using aws-cli and jq

autoscaling api

list all instances for all ASGs:


aws autoscaling describe-auto-scaling-instances \
  | jq '.AutoScalingInstances[] 
  | {AutoScalingGroupName, InstanceId}'

outputs

{
  "AutoScalingGroupName": "production-john",
  "InstanceId": "i-0a2ba7899a72d9e4f"
}

list instance IDs for a particular ASG:

aws autoscaling describe-auto-scaling-instances \
  | jq '.AutoScalingInstances[] 
  | select(.AutoScalingGroupName=="test-main").InstanceId'

outputs

"i-0a2ba7899a72d9e4f"

get the current count of instances on all groups, without any identification:

aws autoscaling describe-auto-scaling-groups \
  | jq '.AutoScalingGroups[] 
  | .Instances 
  | length' 

outputs

0
0
1

where only one of my three test asgs had a running instance at that time, and only one of those....

use the select() function in jq to get objects that meet a condition, and the contains() function for fuzzy matching (note, regex is also possible) and build a pretty object {} out of it. The parenteticals around '(.Instances | length)' are unnecessary, and are only there for stylistic reasons:

aws autoscaling describe-auto-scaling-groups \
  | jq -c  '.AutoScalingGroups[] 
  | select(.AutoScalingGroupName | contains("prod")) 
  | {name: .AutoScalingGroupName, instances: (.Instances | length)}'

outputs

{"name":"production-david","instances":0}
{"name":"production-demo","instances":0}
{"name":"production-john","instances":1}

or, with a different filter:

aws autoscaling describe-auto-scaling-groups \
  | jq -c  '.AutoScalingGroups[] 
  | select(.AutoScalingGroupName | contains("john")) 
  | {name: .AutoScalingGroupName, instances: (.Instances | length)}'

outputs

{"name":"production-john","instances":1}

for a single aws filtered autoscaling group, output the current count of instances as a single return value (unformatted, undescribed), otherwise, if you don't want aws to filter, use a select() | contains() as above:

aws autoscaling describe-auto-scaling-groups --auto-scaling-group-name production-john \
  | jq -c '.AutoScalingGroups[] 
  | .Instances 
  | length'
  
or

aws autoscaling describe-auto-scaling-groups  \
  | jq -c '.AutoScalingGroups[] 
  | select(.AutoScalingGroupName | contains("john")) 
  | .Instances 
  | length'

outputs

1

Output the last 20 'activities' for all ASGs, after outputting some pretty column names and separators, and munge the jq output as into a tsv, then split that a bit more prettily with the unix column command:

aws autoscaling describe-scaling-activities --max-items 20 \
  | jq -r '["Name", "Percent", "Status", "End Time", "Activity"], 
    ["-------------------","--------","------", "--------", "-------"], 
    (.Activities[] | [.AutoScalingGroupName, .Progress, .StatusCode, .EndTime, .Description]) 
  | @tsv' \
  | column -t -s $'\t'

outputs

Name                 Percent   Status      End Time              Activity
-------------------  --------  ------      --------              -------
production-john      100       Successful  2018-01-02T23:45:39Z  Terminating EC2 instance: i-014c61d15d7b5ddf9
production-demo      100       Successful  2018-01-02T22:57:56Z  Terminating EC2 instance: i-064ed50b7bdcf45bf
production-john      100       Successful  2018-01-02T22:57:29Z  Launching a new EC2 instance: i-0a2ba7899a72d9e4f
production-demo      100       Successful  2018-01-02T22:29:57Z  Launching a new EC2 instance: i-064ed50b7bdcf45bf
production-john      100       Successful  2018-01-02T22:29:58Z  Terminating EC2 instance: i-0ae537515d5f450a5
production-john      100       Successful  2018-01-02T22:29:54Z  Terminating EC2 instance: i-0d3b509bca0309e5c
production-john      100       Successful  2017-12-27T18:49:32Z  Launching a new EC2 instance: i-014c61d15d7b5ddf9
production-john      100       Successful  2017-12-27T18:47:59Z  Launching a new EC2 instance: i-0d3b509bca0309e5c
production-john      100       Successful  2017-12-27T18:47:58Z  Launching a new EC2 instance: i-0ae537515d5f450a5
production-john      100       Successful  2017-12-22T21:11:51Z  Terminating EC2 instance: i-0c99269e3f50cd600
production-john      100       Successful  2017-12-22T21:11:57Z  Terminating EC2 instance: i-0bb30e8302659d1a1
production-demo      100       Successful  2017-12-22T21:11:35Z  Terminating EC2 instance: i-0b360ceef3cd7caa3
production-john      100       Successful  2017-12-22T21:06:29Z  Launching a new EC2 instance: i-0c99269e3f50cd600
production-john      100       Successful  2017-12-22T21:06:29Z  Launching a new EC2 instance: i-0bb30e8302659d1a1
production-john      100       Successful  2017-12-22T21:06:09Z  Terminating EC2 instance: i-0df7f4811095f2c1e
production-john      100       Successful  2017-12-22T20:50:09Z  Terminating EC2 instance: i-0486ee8f98d2b337f
production-john      100       Successful  2017-12-22T20:43:19Z  Launching a new EC2 instance: i-0df7f4811095f2c1e
production-john      100       Successful  2017-12-22T20:43:18Z  Launching a new EC2 instance: i-0486ee8f98d2b337f
production-demo      100       Successful  2017-12-22T20:23:00Z  Launching a new EC2 instance: i-0b360ceef3cd7caa3
production-demo      100       Successful  2017-12-22T20:20:36Z  Terminating EC2 instance: i-001aaa29e71f0c72f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment