def get_all_organization_accounts()

in aws_sra_examples/solutions/guardduty/guardduty_org/lambda/src/app.py [0:0]


def get_all_organization_accounts(exclude_account_id: str):
    """
    Gets a list of active AWS Accounts in the AWS Organization
    :param exclude_account_id
    :return: accounts dict and account_id lists
    """
    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 acct["Id"] not in 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 Exception as exc:
        logger.error(f"get_all_organization_accounts error: {exc}")
        raise ValueError("Error error getting accounts")

    return accounts, account_ids