Skip to content

Instantly share code, notes, and snippets.

@AstroTom
Created March 2, 2021 12:51
Show Gist options
  • Save AstroTom/559275649d1bfde2e39f46ef123feb04 to your computer and use it in GitHub Desktop.
Save AstroTom/559275649d1bfde2e39f46ef123feb04 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Tom R.
# based on https://gist.github.com/dukejones/36128decdb1e003ac5d77f5c4523f1f5
# Changed to use ~/.aws/config instead
#
set -e
usage () {
cat <<DOCUMENTATIONXX
Usage : $0 PROFILE_NAME
This version just prints the token ready to be exported
Examples
eval \$($0 Profile)
DOCUMENTATIONXX
}
if [ "$1" == "-h" -o "$1" == "--help" -o "$1" == "" ]; then
usage
exit 1
fi
source_profile_name=$1
role_arn=$(cat $HOME/.aws/config | grep -A 1 "$source_profile_name\]" | tail -n 1 | sed 's/role_arn = //')
session_name="${USER}-`hostname`-`date +%Y%m%d`"
sts=( $(
aws sts assume-role \
--role-arn "$role_arn" \
--role-session-name "$session_name" \
--query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' \
--output text
) )
echo "export AWS_ACCESS_KEY_ID=${sts[0]} AWS_SECRET_ACCESS_KEY=${sts[1]} AWS_SESSION_TOKEN=${sts[2]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment