2

I need to create multiple azure monitor alarms. I'm trying to do this following this website: https://www.azureblue.io/how-to-create-an-alert-rule-using-powershell-and-azure-cli/

This solution works for me if I have action group and target resources in the same subscription but generally I don't. I have one actiongroup and hundreds targets in almost 100 subscriptions.

I have:

#jump to subscription with actiongroup
$context = Get-AzSubscription -SubscriptionId xxx
Set-AzContext $context

$actionGroup = Get-AzActionGroup -name "xxx" -ResourceGroupName "xxx"
$actionGroupId = New-AzActionGroup -ActionGroupId $actionGroup.Id

#jump to subscription with target without this I can't ask for target.id
$context = Get-AzSubscription -SubscriptionId xx
Set-AzContext $context

# Creates a local criteria object that can be used to create a new metric alert
$condition = New-AzMetricAlertRuleV2Criteria `
    -MetricName "Data space used percent (Platform)" `
    -TimeAggregation Maximum `
    -Operator GreaterThan `
    -Threshold 0.8

$windowSize = New-TimeSpan -Minutes 30
$frequency = New-TimeSpan -Minutes 5 
$targetResourceId = (Get-AzResource -Name xxx).Id

#I was thinking that this jump to subscription will solve my issue but doesn't
$context = Get-AzSubscription -SubscriptionId cf653672-c304-49a2-b01b-171f6236bad6
Set-AzContext $context

# Adds or updates a V2 (non-classic) metric-based alert rule.
Add-AzMetricAlertRuleV2 `
    -Name "test" `
    -ResourceGroupName "xxx" `
    -WindowSize $windowSize `
    -Frequency $frequency `
    -TargetResourceId $targetResourceId `
    -Condition $condition `
    -ActionGroup $actionGroupId `
    -Severity 3

I got this error:

Add-AzMetricAlertRuleV2 : Exception type: ErrorResponseException, Message: Null/Empty, Code: Null, Status code:NotFound, Reason phrase: Not Found
At line:26 char:1
+ Add-AzMetricAlertRuleV2 `
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Add-AzMetricAlertRuleV2], PSInvalidOperationException
    + FullyQualifiedErrorId : Microsoft.Azure.Commands.Insights.Alerts.AddAzureRmMetricAlertRuleV2Command

From GUI there is no a problem to chose actiongroup from different subscription but you can't chose targets from multiple subscription.

If I will find solution for this one example I will try to read all my resources and run the code in a loop.

19Kris88
  • 31
  • 1

1 Answers1

0

I had an error like that recently.. It was due to an invalid input. In my particular case it was due to invalid characters in the alertName.

As for the main issue of multiple subscriptions...

Here are a couple of ideas:

  1. To apply an single metric alert to multiple resources of the same type as the same time. You can specify scope, region etc with Add-AzMetricAlertRuleV2 see: https://learn.microsoft.com/en-us/powershell/module/az.monitor/add-azmetricalertrulev2?view=azps-7.2.0. This is limited to only a few resources https://learn.microsoft.com/en-us/azure/azure-monitor/alerts/alerts-metric-overview#monitoring-at-scale-using-metric-alerts-in-azure-monitor.

Note that TargetResourceScope is a array of strings. So it seems possible in theory to apply the alert to multiple subscriptions with this.

  1. When other option are not possible: use Get-AzSubscription, and deploy the code to each subscription or to get the list of subscriptions you need to run the code with.

Your single action group.

I can validate that, you can have a single action group for any alerts inside a subscription. I don't see any reason why the the action group could not be in a separate subscription provided the actiongroup.id is correct and there are no RBAC/firewall issues. But I don't have a way to test this.

Lastly, here is a relevant conversation in an MS forum. https://techcommunity.microsoft.com/t5/azure-monitor/azure-monitor-multiple-subscriptions/m-p/1348362

Aaron C
  • 301
  • 1
  • 8