Home > Enterprise >  What do I need to alter for my multi-subscription Bicep alert rule deployment to succeed?
What do I need to alter for my multi-subscription Bicep alert rule deployment to succeed?

Time:07-06

the code below is my attempt at creating a template allowing easy deployment of a static Alert rule to multiple Azure subscriptions. To achieve this, I am looping through an array containing the subscriptions I want to deploy to, in order to update the name of the alert and scope of the deployment.

I'm encountering the following error when I attempt to run a deployment using my code:

{"code":"InvalidTemplateDeployment","details":[{"code":"BadRequest","target":"/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/Testing_RG/providers/Microsoft.Insights/metricAlerts/ExampleName_mytestalert","message":"Error converting value \"/subscriptions/11111111-1111-1111-1111-111111111111\" to type 'System.Collections.Generic.IList`1[System.String]'. Path 'scopes'."}],"message":"The template deployment 'Microsoft.Template-20220704132314' is not valid according to the validation procedure. The tracking id is 'fe912e5c-5fd5-4180-b24a-f49fa486b704'. See inner errors for details."}

Any pointers as to how I can fix this error would be much appreciated! My code is as follows:

@description('Defines how often the alert criteria is evaluated')
@allowed([
  'PT1M'
  'PT5M'
  'PT15M'
  'PT30M'
  'PT1H'
])
param evaluationFrequency string = 'PT5M'


@description('Defines the size of the window over which collected values are aggregated.')
@allowed([
  'PT5M'
  'PT15M'
  'PT30M'
  'PT1H'
])
param windowSize string = 'PT30M'


@description('Defines the threshhold value at which the alert will trigger')
param threshold int = 90


@description('Defines the resource type to be evaluated')
param metricNamespace string = 'microsoft.compute/virtualmachines'


@description('Defines the metric signal to be monitored. E.g. Percentage CPU, Disk Read Bytes etc.')
param metricName string = 'Percentage CPU'


@description('The operator used to compare the metric value against the threshhold')
@allowed([
  'GreaterThan'
  'GreaterThanOrEqualTo'
  'LessThanOrEqualTo'
  'LessThan'
])
param operator string = 'GreaterThan'


@description('Defines the aggregation function to apply to datapoints')
@allowed([
  'Average'
  'Maximum'
  'Minimum'
  'Total'
  'Count'
])
param timeAggregation string = 'Average'


@description('Defines the subscriptions to which alerts will be scoped. An alert can only be scoped to a single subscription. In the event of multiple subscriptions being selected, an alert will be created for each.')
param subscriptions array = [
  {
    scope: '/subscriptions/11111111-1111-1111-1111-111111111111'
    name: 'ExampleName'
  }
  { scope: '/subscriptions/22222222-2222-2222-2222-222222222222'
    name: 'ExampleName2'
  }
  {
    scope: '/subscriptions/33333333-3333-3333-3333-333333333333'
    name: 'ExampleName3'
  }
  {
    scope: '/subscriptions/44444444-4444-4444-4444-444444444444'
    name: 'ExampleName4'
  }
] 

      
@description('Defines the action group(s) to be notified when the alert triggers')
param actionGroups_example string = '/subscriptions/11111111-1111-1111-1111-111111111111/resourceGroups/Example-Example-UKSouth/providers/microsoft.insights/actionGroups/ExampleActionGroup'

resource alertName_resource 'microsoft.insights/metricAlerts@2018-03-01' = [ for (subscription, i) in subscriptions: {
  name: '${subscription.name}_mytestalert'
  location: 'global'
  properties: {
    criteria: {
      allOf: [
        {
          threshold: threshold
          name: 'Metric1'
          metricNamespace: metricNamespace
          metricName: metricName
          operator: operator
          timeAggregation: timeAggregation
          criterionType: 'StaticThresholdCriterion'
        }
      ]
        'odata.type': 'Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria'
    }
    enabled: true
    evaluationFrequency: evaluationFrequency
    scopes: subscription.scope
    severity: 3
    windowSize: windowSize
    autoMitigate: true
    targetResourceType: 'microsoft.compute/virtualMachines'
    targetResourceRegion: 'uksouth'
    actions: [
      {
        actionGroupId: actionGroups_example 
      }
    ]
  }
}]
output alertnameoutput array = [for (subscription, i) in subscriptions: {
myalertname: alertName_resource[i].name
}]

CodePudding user response:

Looking at the documentation:

scopes: the list of resource id's that this metric alert is scoped to. string[] (required)

So in you case you should have something like that:

scopes: [
  subscription.scope
]
  • Related