Last active
July 21, 2021 18:19
-
-
Save jengo/ecdef6121e2c7f5386e3225911722e65 to your computer and use it in GitHub Desktop.
AWS Script for setting STS tokens when using MFA
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
#!/bin/bash | |
# Written by Jolene Engo <[email protected]> | |
# This is a script that you can use with your MFA token to get an STS token from AWS. | |
# Example way to run: source sts.sh <token> | |
# You MUST source this file or the environment varibles will not be set | |
export AWS_ACCESS_KEY_ID= | |
export AWS_DEFAULT_REGION= | |
export AWS_SECRET_ACCESS_KEY= | |
ACCOUNT_ID= | |
IAM_USER= | |
# If it is already set, it will fail to set a new one | |
unset AWS_SESSION_TOKEN | |
if [ $# -eq 1 ]; then | |
CREDS=$(aws sts get-session-token --serial-number arn:aws:iam::${ACCOUNT_ID}:mfa/${IAM_USER} --token-code $1) | |
export AWS_ACCESS_KEY_ID=$(echo $CREDS | jq -r .Credentials.AccessKeyId) | |
export AWS_SECRET_ACCESS_KEY=$(echo $CREDS | jq -r .Credentials.SecretAccessKey) | |
export AWS_SESSION_TOKEN=$(echo $CREDS | jq -r .Credentials.SessionToken) | |
echo "Temporary credentials setup" | |
else | |
echo "Pass your mfa token as an argument" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment