gpt4 book ai didi

azure - 如何结合多订阅和多区域功能在 Bicep 中创建 Azure 警报

转载 作者:行者123 更新时间:2023-12-03 06:56:51 25 4
gpt4 key购买 nike

下面的代码是我尝试创建一个模板,以便将静态警报规则轻松部署到多个 Azure 订阅。为了实现这一目标,我循环访问一个包含我想要部署到的订阅的数组,以便更新警报的名称和部署的范围。

我打算调整此模板以支持将警报部署到多个区域。我之前已经针对单个订阅实现了这一点,但我不确定组合多订阅和多区域功能所需的语法。任何指示将不胜感激!

@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
}]

最佳答案

理想情况下,您可以创建嵌套循环,但目前不支持它,如所解释的 here .

通过创建模块,您将能够为每个区域的每个订阅创建警报。

alerts-per-region.bicep 文件。它与您当前的实现相同,我刚刚添加了一个 region 参数:

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
}]

然后在您的 main.bicep 中,您可以像这样调用它。我们有一个新的 regions 数组参数。

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
}
}]

关于azure - 如何结合多订阅和多区域功能在 Bicep 中创建 Azure 警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72870881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com