Last active
September 6, 2021 14:56
-
-
Save elgehelge/4f5da5299bdbbfd93bba8268037c8ffa to your computer and use it in GitHub Desktop.
Bash script for logging into AWS with MFA (and into ECR)
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 | |
# Usage: `source login_aws_mfa.sh <account id> <name> <mfa code>` | |
# Example: `source login_aws_mfa.sh 454099695756 john 123456` | |
{ | |
AWS_ACCESS_KEY_ID= && \ | |
AWS_SECRET_ACCESS_KEY= && \ | |
AWS_SESSION_TOKEN= && \ | |
token=$(aws sts get-session-token --serial-number arn:aws:iam::$1:mfa/$2 --token-code $3) && \ | |
keyval=$(echo $token | sed 's/{ "Credentials"://' | awk '{gsub("\"|{|}| ", "")} {gsub(":", "=")} {print $0}' | tr ',' '\n') && \ | |
export AWS_ACCESS_KEY_ID=$(echo $keyval | tr ' ' '\n' | sed -n 's/^AccessKeyId=//p') && \ | |
export AWS_SECRET_ACCESS_KEY=$(echo $keyval | tr ' ' '\n' | sed -n 's/^SecretAccessKey=//p') && \ | |
export AWS_SESSION_TOKEN=$(echo $keyval | tr ' ' '\n' | sed -n 's/^SessionToken=//p') | |
} && { | |
echo "" | |
echo "Success! You are now logged in to AWS with MFA." | |
echo "You should now be able to login to our docker registry with the following command:" | |
echo "" | |
echo " aws ecr get-login-password --region eu-west-1 | docker login --username AWS --password-stdin 454099695756.dkr.ecr.eu-west-1.amazonaws.com" | |
echo "" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment