This is a python script created to simplify the usage of MFA with AWS access keys.
If you have ever tried using AWS Access Keys with MFA then you probably know how painful it is, especially if you have to work with many AWS accounts. We work a lot with AWS CLI, develop with boto3 and use different tools that require AWS access keys. After failing to find a good solution to satisfy all our needs, this tool was created.
- Create a temporary session with MFA token.
- Assume into a specified role via named profiles using MFA token.
- Generate a shell command to export Access Keys into environment variables (for development with boto3, for example).
- Python 3
- AWS CLI configured
Your AWS credentials
and config
files should be set as in aws_cli_config.md. This is not totally necessary, but the script was tested using a configuration of this format.
It is important to set the source_profile = mfa
for each of your named profiles.
pip3 install -r requirements.txt
This script has the following logic:
- Create a MFA session and write it to
~/.aws/credentials
. This step is skipped if MFA session exists and still active. - Use temporary credentials to assume into the role provided as an input parameter.
- Write temporary credentials for the role to
~/.aws/credentials
.
This allows you to enter the MFA token once a day and then assume into roles for up to 1 hour (this is a limitation of AWS).
Create an alias in your ~/.profile
to simplify usage of the script.
alias simple-mfa='python3 /path/to/aws-simple-mfa/simple-mfa.py'
After this you can do the following things:
- Generate temporary Access Keys with your MFA token:
$ simple-mfa
Enter your MFA code:
123456
- Use AWS CLI with an account from
profile-0
in your CLI config:
$ aws s3 ls --profile profile-0
- Assume into a role defined in
profile-1
in your CLI config:
$ simple-mfa -r profile-1
- Imagine you are developing code with boto3 that is supposed to run in an account defined in
profile-2
in your CLI config. You can use simple-mfa to export access secrets into environment variables:
$ simple-mfa -e role -r profile-2
INFO:root:The MFA token is still valid
Copy and paste the following to your terminal
export AWS_ACCESS_KEY_ID="<access-key>" && export AWS_SECRET_ACCESS_KEY="<secret-access-key>" && export AWS_SESSION_TOKEN="<session-token>"
The script was tested using the following environments:
- MacOS High Sierra 10.13.6 with Python 3.7
- Windows support.
- Support assuming into a role using long-term CLI credentials. This will give users an opportunity to assume into roles for up to 12 hours, but will require you to enter a MFA token for every role you assume into.
- Unit tests.
- Test on other platforms: Windows, Ubuntu...
If you found a bug or have a suggestion for improvement, the best way to make it happen is to create a pull request. Alternatively, you can create an issue and we will do our best to resolve it.
This project is licensed under the terms of the GNU General Public License v3.0 license.