Overview and What You Will Learn
A Cloud Monitoring alert policy watches a metric and opens an incident when a condition is met, but it only tells a person if a notification channel is attached. A policy with no channel still creates incidents in the console, so it looks healthy while nobody is paged. Dashboards are a separate tool: they visualize metrics for people who are already looking, while alerts reach people who are not.
In this lab you will:
- Create an email notification channel
- Create an alert policy from a YAML file that references that channel
- Build a small dashboard for the same VM
- Trigger the alert on purpose and confirm that a notification arrives
- Audit a project for policies that have no channel attached
Why This Matters in Production
A common outage pattern is an alert that exists but notifies nobody. A team configures a disk space policy, never attaches a channel, and finds out days later when the disk is full, with a long history of incidents sitting unread in the console. On a payments platform at a company like Razorpay, a silent alert is worse than no alert, because everyone believes the risk is covered.
Core Principles
Monitoring has three connected parts, and a gap in any one of them breaks the chain. The policy decides when something is wrong, the incident records that it happened, and the channel decides who finds out.
threshold] --> P[Alert policy
condition met] P --> I[Incident opened
always logged] I -->|channel attached| C[Notification channel] C --> H([Person is told]) I -->|no channel| S([Silent failure]) classDef step fill:#DBEAFE,color:#1E293B classDef good fill:#10B981,color:#FFFFFF classDef bad fill:#EF4444,color:#FFFFFF class M,P,I,C step class H good class S bad
| Concept | What it does |
|---|---|
| Alert policy | Defines the condition and who to notify |
| Incident | The record created when a condition is met |
| Notification channel | Email, SMS, Slack, PagerDuty or webhook destination |
| Dashboard | Charts for humans who are actively watching |
Detailed Step-by-Step Practical Lab
Milestone 1: Create a Project and a VM to Monitor
Compute Engine needs billing on the project. Use a suffix so the project ID is unique.
export BILLING_ACCOUNT=0A1B2C-3D4E5F-6A7B8Cexport PROJECT_ID=monitoring-lab-$(date +%s | tail -c 6) gcloud projects create $PROJECT_ID --name="Monitoring Lab"gcloud billing projects link $PROJECT_ID --billing-account=$BILLING_ACCOUNTgcloud config set project $PROJECT_ID gcloud services enable compute.googleapis.com monitoring.googleapis.com gcloud compute instances create vm-monitor-test \ --zone=asia-south1-a \ --machine-type=e2-medium \ --image-family=debian-12 \ --image-project=debian-cloudMilestone 2: Create the Notification Channel
The channel is the destination. Create it first so the policy can reference it.
gcloud monitoring channels create \ --display-name="Ops Team Email" \ --type=email \ --channel-labels=email_address=ops-team@devops.in export CHANNEL=$(gcloud monitoring channels list \ --filter='displayName="Ops Team Email"' \ --format='value(name)')echo $CHANNEL# projects/monitoring-lab-12345/notificationChannels/1234567890123456789If your gcloud version does not recognize gcloud monitoring, use gcloud beta monitoring or gcloud alpha monitoring with the same arguments. In the Cloud console you can also use Send test notification on the channel to confirm delivery before relying on it.
Milestone 3: Create the Alert Policy from a File
Writing the policy as a file is clearer and more repeatable than a long list of flags, and it can be stored in version control. CPU utilization is reported as a fraction between 0 and 1, so 0.85 means 85 percent.
# high-cpu-policy.yamldisplayName: High CPU on vm-monitor-testcombiner: ORconditions: - displayName: CPU above 85 percent for 5 minutes conditionThreshold: filter: 'resource.type = "gce_instance" AND metric.type = "compute.googleapis.com/instance/cpu/utilization"' comparison: COMPARISON_GT thresholdValue: 0.85 duration: 300s aggregations: - alignmentPeriod: 60s perSeriesAligner: ALIGN_MEANnotificationChannels: - CHANNEL_NAMEdocumentation: mimeType: text/markdown content: | CPU has stayed above 85 percent for 5 minutes on a production VM. Check recent deploys, then the runbook at https://runbooks.devops.in/high-cpu# Substitute the real channel name and create the policysed "s|CHANNEL_NAME|$CHANNEL|" high-cpu-policy.yaml > policy-rendered.yamlgcloud monitoring policies create --policy-from-file=policy-rendered.yaml # Verify the channel is attachedgcloud monitoring policies list \ --format="table(displayName, notificationChannels)"The documentation block is included in the notification, so whoever receives it sees what to do next.
Milestone 4: Build a Dashboard
A dashboard gives people a place to look during and after an incident. Define it as a file as well.
# vm-dashboard.yamldisplayName: vm-monitor-test overviewmosaicLayout: columns: 12 tiles: - width: 6 height: 4 widget: title: CPU utilization xyChart: dataSets: - timeSeriesQuery: timeSeriesFilter: filter: 'metric.type="compute.googleapis.com/instance/cpu/utilization" resource.type="gce_instance"' aggregation: alignmentPeriod: 60s perSeriesAligner: ALIGN_MEAN - xPos: 6 width: 6 height: 4 widget: title: Network received bytes xyChart: dataSets: - timeSeriesQuery: timeSeriesFilter: filter: 'metric.type="compute.googleapis.com/instance/network/received_bytes_count" resource.type="gce_instance"' aggregation: alignmentPeriod: 60s perSeriesAligner: ALIGN_RATEgcloud monitoring dashboards create --config-from-file=vm-dashboard.yamlgcloud monitoring dashboards list --format="table(displayName, name)"Milestone 5: Trigger the Alert and Confirm the Notification
Test the entire path, not just the configuration. Generate CPU load, wait for the condition to hold for five minutes, and check your inbox and the Alerting page.
# Start one busy loop per vCPU (e2-medium has 2)gcloud compute ssh vm-monitor-test --zone=asia-south1-a --tunnel-through-iap \ --command='for i in 1 2; do (yes > /dev/null &); done' # Wait roughly 6 to 8 minutes, then open Monitoring > Alerting in the console.# You should see an open incident and receive an email at ops-team@devops.in. # Stop the load afterwardsgcloud compute ssh vm-monitor-test --zone=asia-south1-a --tunnel-through-iap \ --command='pkill yes'The incident closes automatically after the metric returns to normal, and a resolved notification is sent.
Milestone 6: Reproduce and Detect the Silent Failure
Create a policy without any channel to see the failure mode, then learn to find it.
# Same policy file with the channel list removedgrep -v -e 'notificationChannels' -e 'CHANNEL_NAME' high-cpu-policy.yaml \ | sed 's/displayName: High CPU on vm-monitor-test/displayName: Silent test (no channel)/' \ > silent-policy.yaml gcloud monitoring policies create --policy-from-file=silent-policy.yaml # Find every policy that has no notification channelgcloud monitoring policies list \ --filter='NOT notificationChannels:*' \ --format="table(displayName, enabled)"This silent policy still evaluates and opens incidents under the same conditions. The only difference is that no one is told. Run the audit command regularly, or in CI against your project, so an unattended alert is caught before an incident exposes it.
Milestone 7: Clean Up
gcloud monitoring policies list --format="value(name)" | \ while read policy; do gcloud monitoring policies delete "$policy" --quiet done gcloud monitoring dashboards list --format="value(name)" | \ while read dash; do gcloud monitoring dashboards delete "$dash" --quiet done gcloud monitoring channels delete "$CHANNEL" --quietgcloud compute instances delete vm-monitor-test --zone=asia-south1-a --quietgcloud projects delete $PROJECT_ID --quietProduction Best Practices & Common Pitfalls
- Always attach a channel, then test it. A configured alert is not a working alert until a real notification has arrived.
- Route by severity. Page the on-call engineer for customer-impacting outages, and send low-urgency warnings to a team inbox or chat room.
- Use sensible durations. A five minute window absorbs harmless CPU bursts and avoids repeated false alarms.
- Write documentation in the alert. Include the likely cause, the dashboard link and the runbook.
- Alert on missing data. For critical metrics, add an absence condition so a dead agent or stopped service also triggers.
- Define alerts as code. YAML files or Terraform keep policies reviewable and repeatable.
- Monitor what matters to users. Use uptime checks and error-rate or latency alerts, not only machine metrics.
Common MistakeCreating a policy and assuming it is complete without checking its notification channels. The policy fires and logs correctly either way, and the only symptom of a missing channel is silence.
TipDisk and memory metrics are not collected by default on Compute Engine. Install the Ops Agent on the VM to get them, then alert on disk percent used before the disk fills.
RememberDashboards help people who are looking. Alert policies reach people who are not.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud monitoring channels create |
Create a notification channel |
gcloud monitoring channels list |
List channels |
gcloud monitoring policies create --policy-from-file=FILE |
Create a policy from YAML or JSON |
gcloud monitoring policies list |
List policies |
gcloud monitoring dashboards create --config-from-file=FILE |
Create a dashboard |
| Symptom | Likely cause and fix |
|---|---|
| Incident exists but no email | No channel attached, update the policy with a channel |
| Alert never fires | Wrong metric filter, or threshold in the wrong unit (fraction vs percent) |
| Too many repeated alerts | Duration too short, lengthen it or add aggregation |
| Disk or memory metrics missing | Ops Agent not installed on the VM |
gcloud monitoring not found |
Older gcloud, update it or use gcloud beta monitoring |