Home > OS >  How to combine multi-subscription and multi-region functionality for Azure alert creation in Bicep
How to combine multi-subscription and multi-region functionality for Azure alert creation in Bicep

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 am intending to adapt this template to also support deployment of alerts to multiple regions. I have achieved this previously for a single subscription, but I am unsure of the syntax required to combine the multi subscription and multi-region functionality. Any pointers would be much appreciated!

@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:

Ideally you would create nested loops but it s not supported for the moment as explained here.

By creating a module, you will be able to create an alert per subscription per region.

alerts-per-region.bicep file. It s the same as your current implementation, I've just added a region parameter:

param region string
param evaluationFrequency string
param windowSize string
param threshold int
param metricNamespace string
param metricName string
param operator string
param timeAggregation string
param subscriptions array
param actionGroups_example string

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: region
    actions: [
      {
        actionGroupId: actionGroups_example
      }
    ]
  }
}]


output alertnameoutput array = [for (subscription, i) in subscriptions: {
  myalertname: alertName_resource[i].name
}]

Then in your main.bicep, you can invoke it like that. We have a new regions array parameter.

param regions array = [
  'ukwest'
  'uksouth'
]

param evaluationFrequency string
param windowSize string
param threshold int
param metricNamespace string
param metricName string
param operator string
param timeAggregation string
param subscriptions array
param actionGroups_example string

module alertsPerRegion 'alerts-per-region.bicep' = [for region in regions: {
  name: '${region}-alerts'
  params: {
    actionGroups_example: actionGroups_example
    evaluationFrequency: evaluationFrequency
    metricName: metricName
    metricNamespace: metricNamespace
    operator: operator
    region: region
    subscriptions: subscriptions
    threshold: threshold
    timeAggregation: timeAggregation
    windowSize: windowSize
  }
}]

  • Related