More Info:
Ensure that a Microsoft Azure activity log alert is fired whenever a ‘Power Off Virtual Machine’ event is triggered within your cloud account. An Azure activity log alert fires each time the action event that matches the condition defined in the alert configuration is triggered. The alert condition that this conformity rule checks for isWhenever the Administrative Activity Log 'Power Off Virtual Machine (Microsoft.Compute/virtualMachines)' has 'any' level, with 'any' status and event is initiated by 'any'
Risk Level
HighAddress
SecurityCompliance Standards
CBP, CIS Microsoft Azure FoundationsTriage and Remediation
- Remediation
Remediation
Using Console
Using Console
Below are step‑by‑step instructions to set up alerts for VM power‑off events in Azure using the portal. This uses Activity Log alerts, which are the most reliable way to capture “Power off” operations.
1. Identify the Event You Want to Alert On
Azure records VM power operations in the Activity log with operations like:Power off virtual machineDeallocate virtual machine- (and their “Succeeded” status)
2. Go to Azure Monitor
- Sign in to the Azure portal.
- In the left-hand menu (or search bar), go to Monitor.
- In Monitor, select Alerts from the left pane.
- Click + Create → Alert rule.
3. Select the Scope (Which VMs / Subscription / RG)
- Under Scope, click Select resource.
- Decide the level:
- To monitor all VMs in a subscription: choose the Subscription.
- To monitor all VMs in a resource group: choose the Resource group.
- To monitor a single VM: change filter to Resource type = Virtual machines, then select the VM.
- Click Done.
4. Choose the Signal Type (Activity Log)
- Under Condition, click Add condition.
- In the “Select a signal” pane:
- Make sure Signal type is set to Activity log (not Metrics).
- Look for Administrative category events such as:
Power off virtual machineDeallocate virtual machine
- If you see multiple relevant operations (depends on your environment), you may need multiple rules:
- One for
Power off virtual machine - One for
Deallocate virtual machine
- One for
- Click the
Power off virtual machine(or the operation you want) signal.
5. Configure the Condition
After selectingPower off virtual machine:- You’ll see Configure signal logic.
- Under Filter, ensure:
- Operation name is
Power off virtual machine. - Status (if available) is
Succeededso you only alert on completed power-offs.
- Operation name is
- Leave other filters as default unless you need to further narrow by caller, IP, etc.
- Click Done (or Apply) to confirm the condition.
Deallocate virtual machine if you also want alerts when VMs are deallocated.6. Create or Select an Action Group (Who Gets Notified)
- Under Actions, click Add action groups.
- Either:
- Select an existing Action group, or
- Click Create action group:
- Define:
- Subscription
- Resource group
- Action group name
- Display name
- On the Notifications tab:
- Click + Add notification.
- Choose Email/SMS/Push/Voice (or other methods such as Webhook, Logic App).
- Enter email addresses or phone numbers as needed.
- Review and Create the action group.
- Define:
- Ensure the action group is selected and click Apply.
7. Define Alert Rule Details
- Under Alert rule details:
- Alert rule name: e.g.,
Alert - VM Power Off - <scope description>. - Description: e.g.,
Alert when any VM in this subscription is powered off (Activity log). - Severity: choose (e.g., Sev 2 or Sev 3) based on your policy.
- Alert rule name: e.g.,
- Ensure Enable rule upon creation is checked.
8. Review and Create
- Click Review + create.
- Validate settings, then click Create.
Deallocate virtual machine if you want to catch both power-off behaviors.9. (Optional) Test the Alert
- Go to one of the target VMs.
- In the VM blade, click Stop (this will typically deallocate it; depending on how it’s done you may see
Power offorDeallocatein the Activity log). - Wait a few minutes; verify:
- The event appears under Monitor → Activity log with
Power off virtual machineorDeallocate virtual machineand statusSucceeded. - The configured action (email/SMS/etc.) is received.
- The event appears under Monitor → Activity log with
Using CLI
Using CLI
Below is a minimal, end‑to‑end Azure CLI remediation to alert whenever a VM is powered off or deallocated.Get the VM resource ID (optional, for scoping):
Capture the Action Group ID:
If you want to scope it to a single VM instead:
Once in place, whenever a VM is powered off or deallocated under the defined scope, you’ll receive an email from the Action Group.
1. Set variables
You can scope the alert to the whole subscription, a resource group, or a single VM. Below I show subscription‑scope (recommended) and note how to change it.
2. Create an Action Group (where alerts are sent)
3. Create Activity Log Alert for VM Power Off / Deallocate
These operations appear in the Activity Log as:Microsoft.Compute/virtualMachines/deallocate/actionMicrosoft.Compute/virtualMachines/powerOff/action
4. Verify
List the activity log alerts:Using Python
Using Python
Below is a concise, step‑by‑step way to set up Azure alerts when a VM is powered off (deallocated/stopped) using Python and the Azure SDK.We’ll create an Activity Log Alert that fires when a VM receives a PowerOff or Deallocate operation.
1. Prerequisites
- Python packages
- Authentication
az login(for local dev withDefaultAzureCredential)- Managed Identity (if running in Azure)
- Collect:
subscription_idresource_group_name(for the alert resource)- (Optional)
vm_nameor target resource if you want the alert limited to a specific VM.
2. Understand the operations to monitor
For VM power‑off events, the common Activity Log operations are:Microsoft.Compute/virtualMachines/powerOff/actionMicrosoft.Compute/virtualMachines/deallocate/action
3. Python code – Create Activity Log Alert
This example:- Creates an Activity Log Alert at the subscription level.
- Filters on the two operations above.
- Sends an email via an action group (if you already have one), or you can just create the alert without actions.
4. What this remediation achieves
- Any time a VM is powered off or deallocated:
- Azure emits an Activity Log event.
- The Activity Log Alert(s) above match that event.
- Actions are triggered (e.g., notifications via Action Group), enabling you to detect and investigate unexpected VM shutdowns.
- Subscription‑wide vs single‑VM monitoring, and
- Email/SMS/Teams/webhook actions,
Using Terraform
Using Terraform
terraform plan should show creation of azurerm_monitor_action_group.vm_poweroff_ag and azurerm_monitor_activity_log_alert.vm_poweroff_alert with the specified criteria and no changes to your existing azurerm_windows_virtual_machine/azurerm_linux_virtual_machine resources.
