This script helps generate AWS MFA credentials using OTP code stored in 1Password. It can be used in setups where you have a single AWS main account with multiple roles for other accounts and you want to use one MFA for all of them.
- 1Password CLI with
opcommand installed and configured; AWS MFA secret stored in 1Password - AWS CLI installed and configured
- Edit your
~/.aws/credentialsfile like thecredentialsfile provided in this Gist. You have to add the AWS key and secret, as well as change role ARNs to match your setup. Profile names can be anything you want as long as the mfa account profile is named the same as the main account profile with-mfasuffix. - Edit the
aws_auth.shscript and change the environment variables to match your setup. TheAWS_MAIN_ACCOUNThere needs to match the main account profile name in~/.aws/credentialsfile. TheONE_PASSWORD_ITEM_IDneeds to match the 1Password item ID that has your AWS OTP code stored in it. - Source the
aws_auth.shscript to generate the credentials for the main account and all the roles. You can also add this to your.bashrcor.zshrcfile to make it available in all your terminal sessions, e.g.source ~/aws_auth.sh
To make the usage even easier, you can create an alias in your .bashrc or .zshrc file, e.g. alias aco="aws_check_auth" and then you can just run aco to check and generate the credentials.
Afterwards, you can set the desired profile in your terminal session by running export AWS_PROFILE=profile_name and then you can use the AWS CLI as usual.