From 3da5abce920b5025198ef4500b05bf3464286050 Mon Sep 17 00:00:00 2001 From: Iain Learmonth Date: Wed, 22 Jun 2022 16:34:18 +0100 Subject: [PATCH] alarm: proxy_cloudfront reads max quota from config --- app/terraform/alarms/proxy_cloudfront.py | 26 ++++++++++++++++-------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app/terraform/alarms/proxy_cloudfront.py b/app/terraform/alarms/proxy_cloudfront.py index fb8ec0d..92ac05f 100644 --- a/app/terraform/alarms/proxy_cloudfront.py +++ b/app/terraform/alarms/proxy_cloudfront.py @@ -1,4 +1,3 @@ -import datetime from typing import Tuple import boto3 @@ -14,17 +13,26 @@ from app.terraform import BaseAutomation def _cloudfront_quota() -> None: - alarm = get_or_create_alarm( - BRN.from_str(f"brn:{current_app.config['GLOBAL_NAMESPACE']}:0:mirror:cloudfront:quota/distributions"), - "quota-usage" - ) - alarm.last_updated = datetime.datetime.utcnow() + # It would be nice to learn this from the Service Quotas API, however + # at the time of writing this comment, the current value for this quota + # is not available from the API. It just doesn't return anything. + max_count = int(current_app.config.get('AWS_CLOUDFRONT_MAX_DISTRIBUTIONS', 200)) deployed_count = len(Proxy.query.filter( Proxy.destroyed.is_(None)).all()) - message = f"{deployed_count} distributions deployed" - if deployed_count > 370: + message = f"{deployed_count} distributions deployed of {max_count} quota" + alarm = get_or_create_alarm( + BRN( + group_id=0, + product="mirror", + provider="cloudfront", + resource_type="quota", + resource_id="distributions" + ), + "quota-usage" + ) + if deployed_count > max_count * 0.9: alarm.update_state(AlarmState.CRITICAL, message) - elif deployed_count > 320: + elif deployed_count > max_count * 0.75: alarm.update_state(AlarmState.WARNING, message) else: alarm.update_state(AlarmState.OK, message)