Repository: Netflix/consoleme
File: consoleme/celery/celery_tasks.py (Python)

Code before:
        if config.region == config.get("celery.active_region"):
            accounts_d: list = aws.get_account_ids_to_names()
            for account_id in accounts_d.keys():
                iam_summary = _get_delivery_channels(
                    account_number=account_id,
                    assume_role=config.get("policies.role_name"),
                    region=config.region,
                )
                log_data = {
                    "function": function,
                    "message": "Total number of IAM roles and IAM roles quota.",
                    "iam_roles": iam_summary["SummaryMap"]["Roles"],
                    "iam_quota": iam_summary["SummaryMap"]["RolesQuota"],
                    "aws_account": account_id,
                }
                stats.count(f"{function}.success", tags={"total_iam_roles": iam_summary["SummaryMap"]["Roles"],
                                                         "role_quota": iam_summary["SummaryMap"]["RolesQuota"],
                                                         "account_id": account_id})
                log.debug(log_data)

    log_data = {
        "function": function,
        "message": "Completed : looking for IAM Role limits",
    }
    log.debug(log_data)
    stats.count(f"{function}.success")
    return True


schedule_30_minute = timedelta(seconds=1800)
schedule_45_minute = timedelta(seconds=2700)
schedule_6_hours = timedelta(hours=6)
schedule_minute = timedelta(minutes=1)
schedule_5_minutes = timedelta(minutes=5)
schedule_24_hours = timedelta(hours=24)

if config.get("development", False):
    # If debug mode, we will set up the schedule to run the next minute after the job starts
    time_to_start = datetime.utcnow() + timedelta(minutes=1)
    dev_schedule = crontab(hour=time_to_start.hour, minute=time_to_start.minute)
    schedule_30_minute = dev_schedule
    schedule_45_minute = dev_schedule
    schedule_6_hours = dev_schedule

schedule = {
    "alert_on_group_changes": {
        "task": "consoleme.celery.celery_tasks.alert_on_group_changes",
        "options": {"expires": 600},
        "schedule": schedule_30_minute,
    },
    "cache_roles_across_accounts": {

Review comment (refactor):
You can remove this, thanks to @patricksanders we emit the necessary health metrics automatically (The function that does this is `report_succeessful_task`

Code after:
            num_accounts = len(accounts_d.keys())
            num_roles = 0
            for account_id in accounts_d.keys():
                try:
                    iam_summary = _get_delivery_channels(
                        account_number=account_id,
                        assume_role=config.get("policies.role_name"),
                        region=config.region,
                    )
                    num_iam_roles = iam_summary["SummaryMap"]["Roles"]
                    iam_role_quota = iam_summary["SummaryMap"]["RolesQuota"]
                    iam_role_quota_ratio = num_iam_roles / iam_role_quota

                    num_roles += num_iam_roles
                    log_data = {
                        "function": function,
                        "message": "IAM role quota for account",
                        "num_iam_roles": num_iam_roles,
                        "iam_role_quota": iam_role_quota,
                        "iam_role_quota_ratio": iam_role_quota_ratio,
                        "account_id": account_id,
                    }
                    stats.count(
                        f"{function}.quota_ratio",
                        tags={
                            "num_iam_roles": num_iam_roles,
                            "iam_role_quota": iam_role_quota,
                            "iam_role_quota_ratio": iam_role_quota_ratio,
                            "account_id": account_id,
                        },
                    )
                    log.debug(log_data)
                except ClientError as e:
                    log_data = {
                        "function": function,
                        "message": "Error retrieving IAM quota",
                        "account_id": account_id,
                        "error": e,
                    }
                    stats.count(f"{function}.error", tags={"account_id": account_id})
                    log.error(log_data, exc_info=True)
                    config.sentry.captureException()

    log_data = {
        "function": function,
        "num_accounts": num_accounts,
        "num_roles": num_roles,
        "message": "Task successfully completed",
    }
    log.debug(log_data)
    return log_data