Last active
April 16, 2023 05:57
-
-
Save MatteoGioioso/ec5f30a4996a381b10f81f1e748a07fe to your computer and use it in GitHub Desktop.
Export AWS SSO credentials
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
#!/bin/bash | |
echo "unsetting previous aws credentials..." | |
unset AWS_ACCESS_KEY_ID | |
unset AWS_SECRET_ACCESS_KEY | |
unset AWS_SESSION_TOKEN | |
aws sso login && | |
aws sts get-caller-identity > /dev/null && # this refresh the cache | |
echo "sourcing aws credentials" | |
JSON_BASEPATH="${HOME}/.aws/cli/cache" # the cache folder contains the cached credentials from the sessions | |
json_file=$(ls -tr "${JSON_BASEPATH}" | tail -n1) # list in reverse cronological order and tail the last line | |
export AWS_ACCESS_KEY_ID=$(cat ${JSON_BASEPATH}/${json_file} | jq -r '.Credentials.AccessKeyId') | |
export AWS_SECRET_ACCESS_KEY=$(cat ${JSON_BASEPATH}/${json_file} | jq -r '.Credentials.SecretAccessKey') | |
export AWS_SESSION_TOKEN=$(cat ${JSON_BASEPATH}/${json_file} | jq -r '.Credentials.SessionToken') | |
echo "done!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
AWS_PROFILE=myprofile
.zshrc
, ex: