aws-all.sh 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/bin/bash
  2. # Runs the same aws CLI command in "most" of the defined profiles
  3. # in $HOME/.aws/config
  4. #
  5. # You can pass in via an environment variable a "profile set"
  6. # of either "commercial", "govcloud", or "both". Default is "both"
  7. #
  8. # Does an "aws sts get-caller-identity" to confirm that your AssumeRole
  9. # and other necessities are properly set up before attempting to call the
  10. # actual AWS command.
  11. #
  12. # PROFILE_SET=commercial aws-all.sh ec2 describe-instances
  13. #
  14. set -eu -o pipefail
  15. AWS=${AWS:-/usr/local/bin/aws}
  16. PROFILE_SET=${PROFILE_SET:-both}
  17. ALL_PROFILES=$( egrep "\[profile" ~/.aws/config | \
  18. awk '{ print $2 }' | \
  19. sed "s/\]//" | \
  20. egrep -v "default|commercial|govcloud" )
  21. COMMERCIAL_PROFILES=""
  22. GOVCLOUD_PROFILES=""
  23. for i in $ALL_PROFILES; do
  24. if [[ "$i" =~ -gov$ ]]; then
  25. GOVCLOUD_PROFILES="$GOVCLOUD_PROFILES $i"
  26. else
  27. COMMERCIAL_PROFILES="$COMMERCIAL_PROFILES $i"
  28. fi
  29. done
  30. case $PROFILE_SET in
  31. both)
  32. PROFILES="$COMMERCIAL_PROFILES $GOVCLOUD_PROFILES"
  33. ;;
  34. govcloud)
  35. PROFILES="$GOVCLOUD_PROFILES"
  36. ;;
  37. commercial)
  38. PROFILES="$COMMERCIAL_PROFILES"
  39. ;;
  40. esac
  41. for i in $PROFILES; do
  42. echo "======================================================================================"
  43. export AWS_PROFILE=$i
  44. set +e
  45. ${AWS} sts get-caller-identity > /dev/null 2>&1
  46. RC=$?
  47. set -e
  48. if [[ $RC -eq 0 ]]; then
  49. echo "GetCallerIdentity (AssumeRole Test) for $i OK"
  50. aws $*
  51. else
  52. echo "GetCallerIdentity (AssumeRole Test) for $i FAILED"
  53. fi
  54. done