def get_all_organization_accounts()

in aws_sra_examples/solutions/macie/macie_org/lambda/src/app.py [0:0]


def get_all_organization_accounts(exclude_account_id: str = "111"):
    """
    Gets a list of active AWS Accounts in the AWS Organization
    :param exclude_account_id: account id to exclude
    :return: accounts dict, 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": PAGE_SIZE}):
            for acct in page["Accounts"]:
                if exclude_account_id and exclude_account_id != acct["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 Exception as exc:
        logger.error(f"get_all_organization_accounts error: {exc}")
        raise ValueError("Unexpected error getting accounts")

    return accounts, account_ids