Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are there any boto3 + MFA examples out there?

I found an example for boto + MFA:

http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_sample-code.html

But I cannot find an example of how to do it with boto3. Any equivalent boto3 examples?

Thanks!

like image 315
Bo Qiang Avatar asked Mar 09 '17 21:03

Bo Qiang


1 Answers

The code below works but you have to use ~/.boto file with the correct credentials. SerialNumber is your MFA device serial or the full AWS ARN of it

#!/usr/bin/env python

import boto3

mfa_TOTP = input("Enter the MFA code: ")

client=boto3.client('sts')

response = client.assume_role(
    RoleArn='arn:aws:iam::123456789:role/admin_full',
    RoleSessionName='mysession',
    DurationSeconds=3600,
    SerialNumber='arn:aws:iam::987654321:mfa/myaccount',
    TokenCode=mfa_TOTP,
)
like image 67
Zoltan Szabo Avatar answered Sep 20 '22 18:09

Zoltan Szabo