Skip to content

Instantly share code, notes, and snippets.

@Slakah
Last active July 31, 2018 14:26
Show Gist options
  • Save Slakah/d19472f1e84213f5279984960157e594 to your computer and use it in GitHub Desktop.
Save Slakah/d19472f1e84213f5279984960157e594 to your computer and use it in GitHub Desktop.
Assume a AWS STS role using the AWS credentials environment variables
#!/bin/bash
set -ue
readonly roleArn="$1"
readonly durationSeconds="3600" # 1 hours
readonly roleSessionName="$USER-local"
# Use default profile to log in
unset AWS_ACCESS_KEY_ID
unset AWS_SECRET_ACCESS_KEY
unset AWS_SESSION_TOKEN
unset AWS_PROFILE
readonly creds="$(aws sts assume-role --role-arn $roleArn --role-session-name $roleSessionName --duration-seconds $durationSeconds --output json)"
readonly accessKeyId=$(echo "$creds" | jq -r .Credentials.AccessKeyId)
readonly secretAccessKey=$(echo "$creds" | jq -r .Credentials.SecretAccessKey)
readonly sessionToken=$(echo "$creds" | jq -r .Credentials.SessionToken)
echo "export AWS_ACCESS_KEY_ID=\"$accessKeyId\""
echo "export AWS_SECRET_ACCESS_KEY=\"$secretAccessKey\""
echo "export AWS_SESSION_TOKEN=\"$sessionToken\""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment