0
0
GCPcloud~5 mins

Alerting policies in GCP - Commands & Configuration

Choose your learning style9 modes available
Introduction
Alerting policies help you get notified when something important happens in your cloud resources. They watch metrics or logs and send alerts so you can fix problems quickly.
When you want to know if your website is down or slow
When your server CPU usage goes too high for too long
When a storage bucket is almost full
When an error rate in your application increases suddenly
When you want to get notified by email or SMS about critical issues
Config File - alert_policy.yaml
alert_policy.yaml
displayName: "High CPU Usage Alert"
combiner: "OR"
conditions:
- displayName: "CPU usage above 80%"
  conditionThreshold:
    filter: "metric.type=\"compute.googleapis.com/instance/cpu/utilization\""
    comparison: COMPARISON_GT
    thresholdValue: 0.8
    duration: "300s"
    trigger:
      count: 1
notificationChannels:
- projects/my-project/notificationChannels/1234567890
enabled: true

displayName: The name of the alert policy.

combiner: How multiple conditions combine (OR means alert if any condition is true).

conditions: What metric to watch and the threshold to trigger alert.

notificationChannels: Where to send alerts (email, SMS, etc.).

enabled: Whether the alert is active.

Commands
This command creates an alerting policy in Google Cloud Monitoring using the configuration file. It sets up the alert to watch CPU usage and notify when it goes above 80%.
Terminal
gcloud monitoring policies create --policy-from-file=alert_policy.yaml
Expected OutputExpected
Created alerting policy: projects/my-project/alertPolicies/9876543210
--policy-from-file - Specifies the YAML file that defines the alerting policy
This command lists all alerting policies in your project so you can verify the new policy was created.
Terminal
gcloud monitoring policies list
Expected OutputExpected
NAME DISPLAY NAME ENABLED 9876543210 High CPU Usage Alert True
This command shows detailed information about the alerting policy to check its settings and conditions.
Terminal
gcloud monitoring policies describe 9876543210
Expected OutputExpected
name: projects/my-project/alertPolicies/9876543210 displayName: High CPU Usage Alert combiner: OR conditions: - displayName: CPU usage above 80% conditionThreshold: filter: metric.type="compute.googleapis.com/instance/cpu/utilization" comparison: COMPARISON_GT thresholdValue: 0.8 duration: 300s trigger: count: 1 notificationChannels: - projects/my-project/notificationChannels/1234567890 enabled: true
Key Concept

If you remember nothing else from this pattern, remember: alerting policies watch your cloud metrics and notify you automatically when something needs your attention.

Common Mistakes
Not enabling the alerting policy after creating it
The alert will not send notifications if the policy is disabled.
Make sure the 'enabled' field is set to true in the policy configuration.
Using incorrect metric filter syntax in the condition
The alert will never trigger because it is not watching the right metric.
Use the exact metric type string from Google Cloud Monitoring documentation, like "compute.googleapis.com/instance/cpu/utilization".
Not specifying notification channels
Alerts will trigger but no one will be notified without notification channels.
Create notification channels in Google Cloud Console and include their IDs in the policy.
Summary
Create an alerting policy YAML file defining the metric, threshold, and notifications.
Use gcloud CLI to create the alerting policy from the YAML file.
List and describe policies to verify the alert is set up correctly and enabled.