Skip to content

Instantly share code, notes, and snippets.

@mrballcb
Created August 8, 2022 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrballcb/cbce4202937e4eed4b534e0809565f17 to your computer and use it in GitHub Desktop.
Save mrballcb/cbce4202937e4eed4b534e0809565f17 to your computer and use it in GitHub Desktop.
Multiple account awless search/filter
#!/usr/bin/env bash
set -eu -o pipefail
FILTER=${1-.*}
REGIONS="us-east-1 us-east-2 us-west-1 us-west-2"
tempwork=$(mktemp -d)
# When the script finishes or is cancelled, clean up the temp dir
trap \
"{ rm -rf ${tempwork}; }" \
SIGINT SIGTERM ERR EXIT
echo "Searching..."
PROFILES=$(cat ~/.aws/credentials | tr -d '[' | tr -d ']')
# In parallel, loop through each region, grep for the search pattern, and write the output to a temp file
for PROFILE in $PROFILES; do
(
echo $PROFILE
for REGION in $REGIONS; do
awless --aws-profile $PROFILE --aws-region $REGION list instances | grep -Ei "${FILTER}" || true
done
echo
) > ${tempwork}/${PROFILE} &
done
# Wait for all backgrounded awless commands above to complete
wait
# Check if any of the output captured has more than 2 lines of output, and print if they do
FOUND=0
for PROFILE in $PROFILES; do
COUNT=$( wc -l ${tempwork}/${PROFILE} | awk '{print $1}')
if [ $COUNT -gt 2 ]; then
cat ${tempwork}/${PROFILE}
FOUND=$(( $FOUND + 1 ))
fi
done
# None of the files had output, specifically state that and show what was the search pattern
if [ $FOUND == 0 ]; then
echo "No results match search pattern: ${FILTER}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment