Skip to content

GCP — alert when approaching a project quota

A GCP project quota (CPU, IPs, persistent disks) creeps toward the limit. The next terraform plan fails with RESOURCE_EXHAUSTED. Quota requests take 1–2 days — needs head start.

Recipe

bash
#!/usr/bin/env bash
# /etc/cron.d/gcp-quota
# 0 */6 * * * root /opt/gcp-quota.sh

PROJECT=${GCP_PROJECT}
REGION=${GCP_REGION:-europe-west1}
WARN_PCT=${WARN_PCT:-80}

# Compute quota for the region
HOTSPOTS=$(gcloud compute regions describe "$REGION" --project="$PROJECT" --format=json \
  | jq --argjson w "$WARN_PCT" '
      [.quotas[] | select(.limit > 0 and (.usage / .limit * 100) > $w) |
       {metric: .metric, used: .usage, limit: .limit,
        pct: ((.usage / .limit * 100) | floor)}]')

COUNT=$(echo "$HOTSPOTS" | jq 'length')

if [ "${COUNT:-0}" -gt 0 ]; then
  SUMMARY=$(echo "$HOTSPOTS" | jq -r '.[] | "\(.metric)=\(.pct)%"' | head -5 | tr '\n' ',')
  curl -fsS "$HEARTBEAT_URL" --data-urlencode "quota_warn=$COUNT,examples=$SUMMARY"
  exit 2
fi
echo "OK (no quotas above ${WARN_PCT}%)"

Same thing in Enterno.io

Wrap in an Enterno heartbeat — catch 80 % quota with 1–2 days lead time, time enough to open a GCP ticket.

Set up API monitor → ← All recipes

Related recipes