def get_all_organization_accounts()

in aws_sra_examples/utils/aws_control_tower/helper_scripts/list_config_recorder_status.py [0:0]


def get_all_organization_accounts(account_info: bool, exclude_account_id: str):
    """
    Gets a list of active AWS Accounts in the AWS Organization
    :param account_info: True = return account info dict, False = return account id list
    :param exclude_account_id
    :return: accounts dict or account_id list
    """
    accounts = []  # used for create_members
    account_ids = []  # used for disassociate_members

    try:
        organizations = boto3.client("organizations")
        paginator = organizations.get_paginator("list_accounts")

        for page in paginator.paginate(PaginationConfig={"PageSize": 20}):
            for acct in page["Accounts"]:
                if (exclude_account_id and acct["Id"] not in exclude_account_id) or not exclude_account_id:
                    # if acct["Status"] == "ACTIVE":  # Store active accounts in a dict
                    account_record = {"AccountId": acct["Id"], "Email": acct["Email"]}
                    accounts.append(account_record)
                    account_ids.append(acct["Id"])
    except ClientError as ce:
        LOGGER.error(f"get_all_organization_accounts error: {ce}")
        raise ValueError("Error getting accounts")
    except Exception as exc:
        LOGGER.error(f"get_all_organization_accounts error: {exc}")
        exit(1)

    if account_info:
        return accounts

    return account_ids