'Boto3 Pagination with for looping through aws accounts
I am having trouble with looping through my 16 accounts, the for loop works in other scripts I have written, but with this one it will only display the last account in accounts users. This is only showing 4 of the 16 to truncate the code a bit. I grab the pagination code from another post on stackoverflow and added this for loop to it. Can anyone help?
import sys
import boto3
**accounts = ["prod","uat","dev","corp"]
for account in accounts:
boto3.setup_default_session(profile_name=account)**
iam = boto3.client("iam")
marker = None
while True:
if marker:
response_iterator = iam.list_users(
MaxItems=10,
Marker=marker
)
else:
response_iterator = iam.list_users(
MaxItems=20
)
print("Next Page : {} ".format(response_iterator['IsTruncated']))
for user in response_iterator['Users']:
print(user['UserName'])
try:
marker = response_iterator['Marker']
print(marker)
except KeyError:
sys.exit()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
