-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlist_org_accounts.py
35 lines (27 loc) · 1.11 KB
/
list_org_accounts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def list_org_accounts_old() -> list:
# import boto3
'''This returns a list of all the accounts in the org. Better to use the paginator.
make sure boto3 is imported
'''
account_list = []
org_client = boto3.client("organizations")
account_response = org_client.list_accounts()
account_list.extend([account["Id"] for account in account_response["Accounts"]])
# print(account_response)
while 'NextToken' in account_response:
account_response = org_client.list_accounts(NextToken=account_response['NextToken'])
account_list.extend([account["Id"] for account in account_response["Accounts"]])
# print(account_response)
return account_list
def list_org_accounts() -> list:
# import boto3
''' using the paginator
'''
org_client = boto3.client("organizations")
org_paginator = org_client.get_paginator('list_accounts')
response = org_paginator.paginate()
account_list = []
for page in response:
account_list.extend([account['Id'] for account in page['Accounts']])
# print(page)
return account_list