Skip to content

Instantly share code, notes, and snippets.

@bruj0
Last active August 14, 2017 18:18
Show Gist options
  • Save bruj0/86312c2651d8c732d4b01e03b72b4838 to your computer and use it in GitHub Desktop.
Save bruj0/86312c2651d8c732d4b01e03b72b4838 to your computer and use it in GitHub Desktop.
Create a token configuration for aws-cli from bash
#!/bin/bash
export AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXX
export AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXX
export AWS_REGION=us-east-1
data=`aws sts get-session-token --duration-seconds 129600 --serial-number arn:aws:iam::XXXXXXXXXXXX:mfa/XXXXXXXXX --token-code $1`
declare -A myarray
while IFS="=" read -r key value
do
myarray[$key]="$value"
done < <(echo $data | jq -cr '.[]' |jq -r "to_entries|map(\"\(.key)=\(.value)\")|.[]")
#declare -p myarray
echo "
[default]
region = us-east-1
output = json
aws_access_key_id = ${myarray[AccessKeyId]}
aws_secret_access_key = ${myarray[SecretAccessKey]}
aws_session_token = ${myarray[SessionToken]}
"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment