code-dumps/aws/aws-org-dump.py

29 lines
743 B
Python
Raw Normal View History

2022-05-14 11:33:56 +08:00
#!/usr/bin/python3
2022-05-13 23:24:11 +08:00
import boto3
2022-05-14 11:33:56 +08:00
# import json
2022-05-13 23:24:11 +08:00
def recurseChildren(ouid, level):
children = client.list_organizational_units_for_parent(ParentId=ouid).get('OrganizationalUnits')
if len(children) == 0:
2022-05-15 02:03:29 +08:00
childAccounts = client.list_accounts_for_parent(ParentId=ouid).get('Accounts')
2022-05-13 23:24:11 +08:00
for c in childAccounts:
2022-05-15 02:03:29 +08:00
print('.' * level, c.get('Name'), c.get('Id'))
2022-05-13 23:24:11 +08:00
return
else:
for c in children:
print('.' * level, c.get('Name'), c.get('Id'))
recurseChildren(c.get('Id'), level + 1)
client = boto3.client('organizations')
response = client.list_roots()
2022-05-15 02:03:29 +08:00
# print(json.dumps(response))
2022-05-14 11:33:56 +08:00
rootId = response['Roots'][0]['Id']
print('Root ', rootId)
2022-05-13 23:24:11 +08:00
recurseChildren(rootId, 1)