1
votes

I am trying to list all of my ec2 Instances and the Image that is used to spin that instance.

aws ec2 describe-instances \
    --query 'Reservations[*].Instances[*].[InstanceId,ImageId]' \
    --output=table

This give me details just fine. But I have many instances and Images. Would like to group my instances based on ImageId and give a count of number of instances against it. Any suggestions?

2

2 Answers

0
votes

There isn't an easy way to do this directly through AWS's CLI that I know of. However, AWS exports a JSON (unless you change the output, as you're doing in the command above), and JSON is pretty easy to manipulate in most languages. For example, here is an example using bash's "jq" utility to do what you'd like:

aws ec2 describe-instances \
    --region=us-east-1 \
    | jq -s 'map(.Reservations[].Instances[].ImageId) \
    | group_by(.) \
    | map({Image: .[0], Count: length})'

This is using the most recent version of jq, which you can get either with apt-get install jq or yum install jq depending on your linux OS.

What it's doing is:

  1. Retrieving all instance information through the command line, in full JSON (not filtering using their CLI at all, though you could if you'd like)

  2. Using JQ to essentially parse out all the ImageIDs (you'll recognize that part of the filter, since amazon's CLI essentially uses JQ's filter syntax)

  3. Group all the images into arrays by Image ID

  4. Create a map of the image ID to the length of the array (which will then return a JSON object mapping ImageID to it's count.

0
votes

I used awk command to output the result

aws ec2 describe-instances \
    --query 'Reservations[*].Instances[*].[ImageId]' \
    --output text \
    | grep -oP 'ami-\K\w+' \
    | awk '{count[$1]++}END{for(j in count) print j,count[j]}'

I also added a grep -oP to remove the ami repeat which was troubling me sort