Skip to content

Instantly share code, notes, and snippets.

@robert-mcdermott
Created July 23, 2022 00:52
Show Gist options
  • Save robert-mcdermott/90c5afe9d25c59ddcb97a720eb6fd077 to your computer and use it in GitHub Desktop.
Save robert-mcdermott/90c5afe9d25c59ddcb97a720eb6fd077 to your computer and use it in GitHub Desktop.
import boto3
def main():
organizations_client = boto3.client("organizations")
list_accounts_object = organizations_client.list_accounts()
all_accounts = []
accounts = list_accounts_object["Accounts"]
while "NextToken" in list_accounts_object:
list_accounts_object = organizations_client.list_accounts(NextToken=list_accounts_object["NextToken"])
accounts.extend(list_accounts_object["Accounts"])
for account in accounts:
all_accounts.append(account["Name"])
return(all_accounts)
if __name__ == "__main__":
accounts = main()
for account in accounts:
print(account)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment