Create a CPU usage alert for an Azure VM

Introduction

This project focuses on creating an Azure Monitor CPU usage alert for an Azure Virtual Machine (VM). By configuring alerts, we can receive notifications if the VM’s CPU usage exceeds a certain threshold. This is crucial for real-world scenarios where proactive monitoring prevents downtime, saves costs, and ensures optimal performance.

Why is it useful?

  • Proactive Monitoring: Automatically catch issues before they escalate.

  • Resource Optimization: Know when to scale up or down based on usage.

  • Cost-Effective: Azure Monitor offers free built-in features for basic alerts, and we only pay for advanced usage if we exceed free limits.

Prerequisites

  • Azure Account (Free Tier) Sign up at https://azure.microsoft.com/free. Use the free tier to avoid additional charges.

  • Azure CLI Installed Install from https://docs.microsoft.com/cli/azure/install-azure-cli if you want to use the terminal method. Confirm installation by running az version.

  • Azure Subscription Permissions Must have at least Contributor or Owner rights on the subscription.

  • An Existing Azure VM If one doesn’t exist, create a free-tier eligible VM (e.g., B1s or other low-tier SKU) in Azure. Ensure the VM is running and the Azure Monitor features are enabled by default.

  • Azure Monitor / Alerts Built into Azure Portal, no separate sign-up needed. Basic usage is free for standard metrics like CPU.

Step-by-Step Implementation

Below, both console (GUI) and terminal (CLI) instructions are provided together for each major step.

Create (or Use) a Resource Group and a VM

(If you already have a VM you want to monitor, you can skip to Step 3.2.)

Console (GUI) & Terminal (CLI) Steps

In the Portal Go to the Azure Portal. Click Resource groups+ Create. Subscription: Your free tier subscription. Resource group: e.g. MonitorRG. Region: Choose any region (e.g. East US). Click Review + create, then Create. Go to Virtual machines+ Create. Subscription: Same free tier subscription. Resource Group: MonitorRG. Name: e.g. MyVM. Region: East US. Image: e.g. Ubuntu LTS or Windows Server (whichever meets free usage constraints). Size: e.g. B1s (free eligible or low cost). Configure username/password or SSH keys. Click Review + createCreate.

In the Terminal

Create the Resource Group

Create a VM (e.g. Ubuntu on B1s size)

  • Explanation:
    az group create: Creates a resource group named MonitorRG in eastus.
    az vm create: Provisions a VM named MyVM with an Ubuntu image at the B1s size.
    --generate-ssh-keys: Automatically generates SSH keys if none exist locally.

Enable Monitoring and Metrics for the VM

Purpose: By default, Azure VMs have basic metrics enabled. We just confirm that CPU metrics collection is active.

Console (GUI) & Terminal (CLI) Steps

In the Portal Go to Virtual machines → select MyVM. Under Monitoring, click Insights (optional if you want advanced monitoring). Ensure it’s On if you need more detailed metrics. By default, CPU usage metric is enabled at the platform level.

In the Terminal

Check that Azure Monitor is collecting metrics :

az monitor metrics list-definitions \

--resource "/subscriptions/<sub_id>/resourceGroups/MonitorRG/providers/Microsoft.Compute/virtualMachines/MyVM" \

--query "[].name.value"

Explanation:

  • az monitor metrics list-definitions: Lists available metrics for a resource.
  • If you see Percentage CPU in the output, CPU usage metrics are available.

Create an Alert Rule for CPU Usage

Purpose: Set up a rule that triggers when CPU usage surpasses a chosen threshold (e.g. 70%) for a certain duration.

Console (GUI) & Terminal (CLI) Steps

In the Portal Go to Virtual machines → select MyVM. Under Monitoring, click Alerts+ Create Alert Rule. Scope: Should default to MyVM. If not, select the correct scope. Condition: Click Add condition. Search for CPU Percentage (may appear as Percentage CPU). Metric: Percentage CPU. Condition: e.g. “Greater than” 70. Aggregation: e.g. “Average” over the last 5 minutes. Action group: If you have an existing action group, select it. If not: Click Create action group. Action group name: e.g. EmailAlerts. Short name: alerts. Action type: Select Email/SMS message/Push/Voice, configure your email. Click OK. Alert rule details: Alert rule name: CPUHighAlert. Description: “Alert when CPU usage > 70% for 5 minutes.” Click Create alert rule.

In the Terminal

Create an action group

Create an alert rule for CPU usage

Explanation:

  • az monitor action-group create: Creates a notification group called EmailAlerts, which sends emails to the specified address.

  • az monitor metrics alert create: Defines a metric-based alert named CPUHighAlert, monitoring the average CPU usage metric over a default time window (often 5 minutes by default). If it goes above 70, the alert triggers the EmailAlerts group.

Verifying and Testing the Project

Force CPU Usage If Linux VM: SSH into the VM, run stress --cpu 1 (install the stress package if needed) or any CPU-intensive command. If Windows VM: Remote Desktop in, run a CPU-intensive task.

Check Alert Firing Wait until CPU usage is above the threshold for at least 5 minutes (depending on your alert condition). In the Portal, under AlertsAlert history, you should see the alert triggered. An email notification (or SMS) should arrive in the mailbox/phone number configured.

Confirm Alert on Azure Portal Under the VM’s AlertsHistory tab, you’ll see a Fired status next to CPUHighAlert once it’s triggered.

Common Issues and Troubleshooting

Alert Not Triggering
Check that the threshold is sufficiently low or run a CPU stress test to exceed the threshold.
Verify the time window (duration) is correct.

Email or SMS Not Received
Double-check the Action Group settings and verify you entered the correct email address or phone number.
Check your spam folder.

Permissions Error
If az monitor metrics alert create fails with an authorization error, ensure your account has at least Contributor permission on the resource group.

Metric Not Found
Confirm your VM is in a supported region and that CPU usage metrics are visible (Step 3.2).

Free Tier Limit Reached
Basic metric alerts are typically free. If you use advanced features (e.g., logs beyond free data allocation), you might incur charges. Monitor usage in Cost Management if uncertain.

Conclusion

We have successfully created an Azure Monitor CPU usage alert for an Azure VM without incurring any additional costs beyond the Azure free tier allowances. We set up the resource group and VM, verified that CPU metrics were collected, and defined an alert rule that notifies an email action group when the CPU usage exceeds a specified threshold.

In the process, we have learned how to:

Create and manage resources via Azure Portal and Azure CLI.

Configure Azure Monitor metrics and set an alert condition.

Tie that alert to an action group for notifications.

This ensures proactive monitoring for a crucial metric—CPU usage—helping us maintain healthy, cost-effective infrastructure.

What is Cloud Computing ?

Cloud computing delivers computing resources (servers, storage, databases, networking, and software) over the internet, allowing businesses to scale and pay only for what they use, eliminating the need for physical infrastructure.


  • AWS: The most popular cloud platform, offering scalable compute, storage, AI/ML, and networking services.
  • Azure: A strong enterprise cloud with hybrid capabilities and deep Microsoft product integration.
  • Google Cloud (GCP): Known for data analytics, machine learning, and open-source support.