为什么我不能在Azure资源模块中循环两次? [英] Why can't I loop twice in an Azure resource module?
本文介绍了为什么我不能在Azure资源模块中循环两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在Azure二头肌资源/模块中设置警报。我们的目标是拥有3个文件,其中一个是创建初始资源结构并将其参数化的文件:
param metricAlertsName string
param description string
param severity int
param enabled bool = true
param scopes array = []
param evaluationFrequency string
param windowSize string
param targetResourceRegion string = resourceGroup().location
param allOf array = []
param actionGroupName string
var actionGroupId = resourceId(resourceGroup().name, 'microsoft.insights/actionGroups', actionGroupName)
resource dealsMetricAlerts 'Microsoft.Insights/metricAlerts@2018-03-01' = [for appService in appServices : {
name: metricAlertsName
location: 'global'
tags: {}
properties: {
description: description
severity: severity
enabled: enabled
scopes: scopes
evaluationFrequency: evaluationFrequency
windowSize: windowSize
targetResourceRegion: targetResourceRegion
criteria: {
'odata.type': 'Microsoft.Azure.Monitor.SingleResourceMultipleMetricCriteria'
allOf: allOf
}
actions: [
{
actionGroupId: actionGroupId
}
]
}
}]
第二个文件main.biep,其中我的所有资源都作为模块使用:
module dealsMetricAlerts '../modules/management/metric-alert.bicep' = [for (alert, index) in alertConfig.metricAlerts: {
name: alert.name
params: {
metricAlertsName: alert.params.metricAlertsName
actionGroupName: actionGroupName
description: alert.params.description
evaluationFrequency: alert.params.evaluationFrequency
severity: alert.params.severity
windowSize: alert.params.windowSize
allOf: alert.params.allOf
scopes: [
resourceId(resourceGroup().name,'Microsoft.Web/sites', 'gbt-${env1}-${env}-${location}-webapp')
]
}
}]
第三个文件是我可以在其中循环的参数文件,将它的值输入到main.biep中的模块中,以创建多个资源,而不必编写多个模块:
{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"alertConfig": {
"value": {
"metricAlerts": [
{
"name": "403ErrorTest",
"params": {
"metricAlertsName": "AppService403ErrorTest",
"actionGroupName": "actionGroupName",
"description": "Alert fires whenever the App Service throws a 403 error",
"evaluationFrequency": "PT5M",
"severity": 2,
"windowSize": "PT15M",
"allOf": [
{
"name": "403errorTest",
"metricName": "Http403",
"dimensions": [],
"operator": "GreaterThan",
"threshold": 0,
"timeAggregation": "Count"
}
]
}
},
{
"name": "5XXErrorTest",
"params": {
// Values for 500 error
}
}
]
}
}
}
}
我总共有5个应用程序服务,1个Web应用程序和4个功能应用程序。如果我像上面那样硬编码应用程序服务的名称,则部署该Web应用程序时会完全正确地出现两个错误。
我遇到的问题是尝试在作用域参数上使用另一个循环,以便将2个错误附加到所有5个应用程序服务。
我尝试了如下内容:
// Created an array with the names of the app services
var appServices = [
'dda-webApp'
'dealservice-fnapp'
'itemdata-fnapp'
'refdata-fnapp'
'userprofile-fnapp'
]
// Update the scopes param to loop through the names
scopes: [for appService in appServices: [
resourceId(resourceGroup().name,'Microsoft.Web/sites', 'gbt-${env1}-${env}-${location}-${appService}')
]]
这最常见的错误是:Unexpected token array in scopes[0]
。
我认为发生的情况是,当它尝试按资源ID中的名称查找资源时,它将遍历所有appServices名称并将整个数组附加到资源名称上,从而使其无效。
是否还有其他方法可以遍历这些作用域,以便将403和500通知分别附加到我的5个应用程序服务?
推荐答案
查看documentation,您不能执行内部循环:
在Bicep中使用循环有以下限制:
- 循环迭代次数不能为负数或超过800次。
- 无法循环包含嵌套子资源的资源。将子资源更改为顶级资源。请参阅Iteration for a child resource。
- 无法在多个属性级别上循环。
在这种情况下,您可以定义一个变量并在模块循环之外创建数组:
// Created an array with the names of the app services
var appServices = [
'dda-webApp'
'dealservice-fnapp'
'itemdata-fnapp'
'refdata-fnapp'
'userprofile-fnapp'
]
// Create an array of app service resource ids
var appServiceIds = [for appService in appServices: resourceId(resourceGroup().name,'Microsoft.Web/sites', 'gbt-${env1}-${env}-${location}-${appService}')]
// Set the scope param with this variable
module dealsMetricAlerts '../modules/management/metric-alert.bicep' = [for (alert, index) in alertConfig.metricAlerts: {
name: alert.name
params: {
...
scopes: appServiceIds
}
}]
这篇关于为什么我不能在Azure资源模块中循环两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文