简体   繁体   中英

looping over multiple aws profiles with boto3

I cant seem to find a really good way to initiate multiple sessions with boto3. If I have 10 accounts and want to lets say, make a new IAM user, I cant seem to change the boto3.session.Session with new calls.

So example code:

    for user in usernames:
       for acct in accounts:
           boto3.session.Session(profile_name=acct)
           print 'trying account: %s' % acct
           try:
               uname = IAM.create_user(UserName=user)
               uname
               print uname
               print row_template % header
               print row_template % tuple(['-' * len(h) for h in header])
               print row_template % (user, acct)
           except botocore.exceptions.ClientError as e:
               print e

However, it will only create a session for the default session and will not change it. I cant seem to find a way to close the session either.

Any help would be greatly appreciated.

You are not using the session your boto3.session() returns. Instead you are using the same default session. You can develop from the following code snippet:

   for acct in accounts:
       session = boto3.Session(profile_name=acct)
       iam = session.client('iam')
       for user in usernames:
           iam.create_user(UserName=user)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM