Find all EC2 Instances using in-house AMIs

Building off my last post GNU Parallel and AWS CLI, today I used parallel with more AWS to get a count of all AMIs being used in my infrastructure using any in-house AMI (non-Amazon, non-Marketplace).

The Problem

I need to know how many of my instances are running Chef clients. Conveniently we only run Chef client on instances that use images we have created in-house. So I can limit my search. While I could have done this with a well thought out one-liner I thought it would be more usable as a bash script.

set -euo pipefail


declare -a AMIS
declare -a INSTANCES
declare -a PROFILES
REGIONS=(us-east-1 us-west-2 eu-west-2 eu-west-1)

# Load the profiles
echo -n "Loading profiles ... "
PROFILES+=($(cat ~/.aws/credentials | grep devopsadmin | cut -d '[' -f2 | cut -d ']' -f1 | grep -v ^role_arn))
echo "done"

# Get AMIs
echo -n "Collecting AMIs ... "
AMIS+=($(parallel "aws --profile {2} --region {1} ec2 describe-images --owners self | jq -r .Images[].ImageId" ::: "${REGIONS[@]}" ::: "${PROFILES[@]}"))
echo "done"

# Get Instances
echo -n "Collecting INSTANCES ... "
INSTANCES+=($(parallel "aws --region {1} --profile {2} ec2 describe-instances | jq -r .Reservations[].Instances[].ImageId"  ::: "${REGIONS[@]}" ::: "${PROFILES[@]}"))
echo "done"

for ami in "${AMIS[@]}"; do
  for ins in "${INSTANCES[@]}"; do
    if [ $ins = $ami ]; then
      echo $ins $ami;
echo "$count"

I am really starting to love using parallel while working with AWS, saves me lots of waiting time.