Sometimes you need to assume role while working with aws cli, but role assuming condition is to be authenticated by MFA.
Here is script which makes it fun.
Script is available in my git repo
What we are doing here:
- We use normal profile to get session Token, with MFA authentication.
- We create temp profile for MFA authenticated session.
- We authenticated and assume desired role with that profile and add role profile to
.aws/credentials file
Then, we can start using it in any aws cli command with
--profile
argumentWhat information we need:
initial profile to use
temp profile name - which will be your profile with MFA authentication
role arn
role session name (anything you like to name it)
role profile name, which you can refer
MFA arn
current MFA token
- Now we are getting MFA authentication session object, and parsing it to extract:
aws_access_key_id
aws_secret_access_key
aws_session_token
I intentionally left sed
parse not optimized, for ease of understanding
- Using temporary MFA authenticated profile we are assuming role, and getting role session authentication object, which we parse and extract data as in step 2.
after writing temporary role profile to .aws/credentials
we can use it with --profile
argument in aws cli
Top comments (0)