src/account_inventory.py
def list_aws_accounts_for_ou(ou_id):
    """
    Gets the current list of AWS Accounts in an OU from AWS Organizations.

    :return The current dict of all AWS Accounts.
    """
    log.info(f"Searching for accounts in OU {ou_id}")
    account_list = []

    # first add accounts directly in the ou
    try:
        # max limit of 20 accounts per listing
        # use paginator to iterate through each page
        lafp_paginator = org_client.get_paginator('list_accounts_for_parent')
        lafp_page_iterator = lafp_paginator.paginate(ParentId=ou_id)
        for page in lafp_page_iterator:
            for acct in page['Accounts']:
                account_list.append(acct)
    except org_client.exceptions.ClientError as error:
        log.error(f'Error: {error}')

    # next add accounts in child ous
    try:
        # max limit of 20 children per listing
        # use paginator to iterate through each page
        lc_paginator = org_client.get_paginator('list_children')
        ou_page_iterator = lc_paginator.paginate(
            ParentId=ou_id,
            ChildType='ORGANIZATIONAL_UNIT'
        )
        for page in ou_page_iterator:
            for child in page['Children']:
                # recurse over child ous and add the accounts they contain
                log.info(f"Adding accounts from child OU {child['Id']}")
                account_list += list_aws_accounts_for_ou(child['Id'])
    except org_client.exceptions.ClientError as error:
        log.error(f'Error: {error}')

    log.info(f"Found {len(account_list)} accounts in OU {ou_id}")

    return account_list

    Gets the current list of AWS Accounts in an OU from AWS Organizations.

    :return The current dict of all AWS Accounts.
    
Gets the current list of AWS Accounts in an OU from AWS Organizations.
:return The current dict of all AWS Accounts.
Gets the current list of AWS Accounts in an OU from AWS Organizations . : return The current dict of all AWS Accounts .
def list_aws_accounts_for_ou(ou_id):
    log.info(f"Searching for accounts in OU {ou_id}")
    account_list = []
    try:
        lafp_paginator = org_client.get_paginator('list_accounts_for_parent')
        lafp_page_iterator = lafp_paginator.paginate(ParentId=ou_id)
        for page in lafp_page_iterator:
            for acct in page['Accounts']:
                account_list.append(acct)
    except org_client.exceptions.ClientError as error:
        log.error(f'Error: {error}')
    try:
        lc_paginator = org_client.get_paginator('list_children')
        ou_page_iterator = lc_paginator.paginate(
            ParentId=ou_id,
            ChildType='ORGANIZATIONAL_UNIT'
        )
        for page in ou_page_iterator:
            for child in page['Children']:
                log.info(f"Adding accounts from child OU {child['Id']}")
                account_list += list_aws_accounts_for_ou(child['Id'])
    except org_client.exceptions.ClientError as error:
        log.error(f'Error: {error}')
    log.info(f"Found {len(account_list)} accounts in OU {ou_id}")
    return account_list
def list_aws_accounts_for_ou ( ou_id ) : log . info ( f"Searching for accounts in OU {ou_id}" ) account_list = [ ] try : lafp_paginator = org_client . get_paginator ( 'list_accounts_for_parent' ) lafp_page_iterator = lafp_paginator . paginate ( ParentId = ou_id ) for page in lafp_page_iterator : for acct in page [ 'Accounts' ] : account_list . append ( acct ) except org_client . exceptions . ClientError as error : log . error ( f'Error: {error}' ) try : lc_paginator = org_client . get_paginator ( 'list_children' ) ou_page_iterator = lc_paginator . paginate ( ParentId = ou_id , ChildType = 'ORGANIZATIONAL_UNIT' ) for page in ou_page_iterator : for child in page [ 'Children' ] : log . info ( f"Adding accounts from child OU {child['Id']}" ) account_list += list_aws_accounts_for_ou ( child [ 'Id' ] ) except org_client . exceptions . ClientError as error : log . error ( f'Error: {error}' ) log . info ( f"Found {len(account_list)} accounts in OU {ou_id}" ) return account_list
Gets the current list of AWS Accounts in an OU from AWS Organizations.
Gets the current list of AWS Accounts in an OU from AWS Organizations .
"""
    Gets the current list of AWS Accounts in an OU from AWS Organizations.

    :return The current dict of all AWS Accounts.
    """ # first add accounts directly in the ou # max limit of 20 accounts per listing # use paginator to iterate through each page # next add accounts in child ous # max limit of 20 children per listing # use paginator to iterate through each page # recurse over child ous and add the accounts they contain
ou_id