Skip to content

Instantly share code, notes, and snippets.

@mbainter
Last active February 24, 2024 15:51
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mbainter/b38a4cb411c0b5c1bae6 to your computer and use it in GitHub Desktop.
Save mbainter/b38a4cb411c0b5c1bae6 to your computer and use it in GitHub Desktop.
Fish shell function to set your AWS credentials with MFA for use with Terraform
function aws_config
if not fgrep -q "[$argv]" ~/.aws/credentials
echo "Please specify a valid profile."
else
set -e AWS_ACCESS_KEY
set -e AWS_SECRET_KEY
set -g -x ATLAS_TOKEN (awk "/\[$argv\]/,/^\$/ { if (\$1 == \"atlas_token\") { print \$3 }}" ~/.aws/credentials)
set account (awk "/\[$argv\]/,/^\$/ { if (\$1 == \"account_id\") { print \$3 }}" ~/.aws/credentials)
set username (awk "/\[$argv\]/,/^\$/ { if (\$1 == \"username\") { print \$3 }}" ~/.aws/credentials)
set mfarn "arn:aws:iam::$account:mfa/$username"
echo "Please enter your MFA token:"
read -l mfa_token
set json (aws --profile=$argv sts get-session-token --serial-number="$mfarn" --token-code=$mfa_token)
set -g -x AWS_ACCESS_KEY_ID (echo $json | jq -r '.Credentials.AccessKeyId')
set -g -x AWS_SECRET_ACCESS_KEY (echo $json | jq -r '.Credentials.SecretAccessKey')
set -g -x AWS_SESSION_TOKEN (echo $json | jq -r '.Credentials.SessionToken')
end
end
@mbainter
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment