Last active
January 11, 2023 11:41
-
-
Save garryyao/e958557ce0dc5e0e42de2ccd6f1162fd to your computer and use it in GitHub Desktop.
export SSO temporary credentials as ENV variables
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
# AWS SSO Get temprory AWS credentials | |
# Add to your bash profile | |
# usage: AWS_PROFILE=<aws_profile> aws_sso_creds | |
aws_sso_creds() { | |
local account_id role_name access_token sso_region profile_region temp_creds | |
echo "Logging in to profile: ${AWS_PROFILE}" | |
aws sso login --profile ${AWS_PROFILE} | |
echo "Reading configuration from ~/.aws/config for ${AWS_PROFILE}" | |
account_id="$(aws configure get sso_account_id --profile ${AWS_PROFILE})" | |
role_name="$(aws configure get sso_role_name --profile ${AWS_PROFILE})" | |
sso_region="$(aws configure get sso_region --profile ${AWS_PROFILE})" | |
profile_region="$(aws configure get region --profile ${AWS_PROFILE})" | |
access_token="$(cat ${HOME}/.aws/sso/cache/$(ls -tr ${HOME}/.aws/sso/cache | tail -n1) | jq -r '.accessToken')" | |
echo "Getting credentials for ${account_id} ${role_name} ${sso_region}" | |
temp_creds="$(aws sso get-role-credentials \ | |
--account-id "${account_id}" \ | |
--role-name "${role_name}" \ | |
--region "${sso_region:-us-east-1}" \ | |
--access-token "${access_token}" \ | |
--output json \ | |
| jq -r '.roleCredentials')" | |
export AWS_PROFILE="${AWS_PROFILE}" | |
export AWS_REGION="${profile_region}" | |
export AWS_ACCESS_KEY_ID=$(jq -r '.accessKeyId' <<< ${temp_creds}) | |
export AWS_SECRET_ACCESS_KEY=$(jq -r '.secretAccessKey' <<< ${temp_creds}) | |
export AWS_SESSION_TOKEN=$(jq -r '.sessionToken' <<< ${temp_creds}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment