Skip to content

Instantly share code, notes, and snippets.

@z11i
Last active June 6, 2023 01:28
Show Gist options
  • Save z11i/2b9c0c7777993edccc0ecb2d904655d7 to your computer and use it in GitHub Desktop.
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.
#!/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