Last active
June 6, 2023 01:28
-
-
Save z11i/2b9c0c7777993edccc0ecb2d904655d7 to your computer and use it in GitHub Desktop.
Auto check AWS SSO credentials validity, and login if invalid. You can set this as a cronjob.
This file contains hidden or 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 | |
PROFILE=${PROFILE:-my-profile-to-use} | |
AWS=/opt/homebrew/bin/aws | |
SSO_ACCOUNT=$($AWS sts get-caller-identity --query "Account" --profile "$PROFILE") | |
# kill existing pending sessions | |
pgrep -f 'aws sso login' | xargs kill | |
if [ ${#SSO_ACCOUNT} -eq 14 ]; then # 14 because 2 quotes + 12-digit account number | |
exit 0 | |
else | |
$AWS sso login --profile "$PROFILE" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment