Created
August 8, 2022 18:37
-
-
Save mrballcb/cbce4202937e4eed4b534e0809565f17 to your computer and use it in GitHub Desktop.
Multiple account awless search/filter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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