AWS Management — Assuming role in AWS CLI with MFA authentication.
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
- Then, we can start using it in any aws cli command with
- What information we need:
a. initial profile to use
b. temp profile name — which will be your profile with MFA authentication
c. role session name (anything you like to name it)
d. role profile name, which you can refer
e. current MFA arn and token
- Now we are getting MFA authentication session object, and parsing it to extract:
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 in step 2
after writing temporary role profile to
.aws/credentials we can use it with
--profile argument in aws cli
Originally published at https://dev.to on August 6, 2020.