Skip to content

Instantly share code, notes, and snippets.

@nicka
Last active May 26, 2017 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicka/19d4a0a30a1debed2c287b5fe94ff66d to your computer and use it in GitHub Desktop.
Save nicka/19d4a0a30a1debed2c287b5fe94ff66d to your computer and use it in GitHub Desktop.
Assumes an AWS IAM role and exports temporary AWS_* credentials.
#!/bin/bash
#### Description: Assumes an AWS IAM role and export temporary AWS_* credentials.
#### Written by: Nick den Engelsman - nick@bandlab.com on 2017-05
####
#### Setup:
#### 1. npm install -g aws-auth-helper
#### 2. save this script as ~/.aws/mfa.sh
#### 3. chmod +x ~/.aws/mfa.sh
####
#### Edit your .bash_rc (or something else):
#### alias aws-auth="source ~/.aws/mfa.sh $1"
####
#### Usage:
#### aws-auth [PROFILE]
# Make sure previous sessions are cleaned
AWS_ACCESS_KEY_ID=""
AWS_SECRET_ACCESS_KEY=""
AWS_SESSION_TOKEN=""
# Configure the aws auth helper
AWS_CONFIG_FILE="~/.aws/config"
AUTH_HELPER=$(which aws-auth-helper)
# Obtain credentials
eval `$AUTH_HELPER $1`
# Export newly obtained credentials
export AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
export AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
export AWS_SESSION_TOKEN=$AWS_SESSION_TOKEN
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment