ARM模板部署报错:未定义Microsoft.Web/resourceGroups资源
ARM模板部署错误修正方案
错误原因分析
报错模板中未定义资源'Microsoft.Web/resourceGroups/*'的核心问题有两个:
- 资源组的资源提供商是Microsoft.Resources,不是
Microsoft.Web,你在dependsOn里错误引用了错误的提供商命名空间 - 订阅级部署中,创建资源组内的资源时,
resourceId函数的参数顺序错误,需要先指定资源组名称,再指定资源类型和名称
具体修正点
1. 修正资源组的dependsOn引用
所有依赖资源组的资源,dependsOn里的resourceId要改为正确的提供商:
"dependsOn": ["[resourceId('Microsoft.Resources/resourceGroups', variables('resourceGroup'))]"]
2. 修正资源引用的resourceId参数顺序
对于App Service Plan、Application Insights、App Service这类资源组内的资源,resourceId需要遵循resourceId(资源组名称, 资源类型, 资源名称)的格式,而非把资源组名称当成资源名称传递。
比如App Service的dependsOn和serverFarmId要改成:
"dependsOn": [ "[resourceId(variables('resourceGroup'), 'Microsoft.Web/serverfarms', variables('appServicePlan'))]", "[resourceId(variables('resourceGroup'), 'Microsoft.Insights/components', variables('ApplicationInsights'))]" ], "properties": { "serverFarmId": "[resourceId(variables('resourceGroup'), 'Microsoft.Web/serverfarms', variables('appServicePlan'))]" }
修改后的完整ARM模板
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "application": { "type": "string" }, "location": { "type": "string", "allowedValues": ["north europe", "west europe"] }, "environment": { "type": "string", "allowedValues": ["dev", "fea", "int", "pre", "prd"] }, "severity": { "type": "string", "allowedValues": ["low", "medium", "high", "critical"] }, "hostingPlanSkuTier": { "type": "string", "allowedValues": ["Basic", "Standard"] }, "hostingPlanSkuName": { "type": "string", "allowedValues": ["B1", "B2", "B3", "S1", "S2", "S3"] }, "hostingPlanSkuCapacity": { "type": "int" } }, "variables": { "resourceGroup": "[concat('rg-', parameters('application'), '-', parameters('environment'), '-', first(first(split(parameters('location'), ' '))), first(last(split(parameters('location'), ' '))))]", "appServicePlan": "[concat('asp-', parameters('application'), '-', parameters('environment'), '-', first(first(split(parameters('location'), ' '))), first(last(split(parameters('location'), ' '))))]", "appService": "[concat('as-', parameters('application'), '-', parameters('environment'), '-', first(first(split(parameters('location'), ' '))), first(last(split(parameters('location'), ' '))))]", "ApplicationInsights": "[concat('ai-', parameters('application'), '-', parameters('environment'), '-', first(first(split(parameters('location'), ' '))), first(last(split(parameters('location'), ' '))))]" }, "resources": [ { "type": "Microsoft.Resources/resourceGroups", "apiVersion": "2021-04-01", "name": "[variables('resourceGroup')]", "location": "[parameters('location')]", "tags": { "location": "[parameters('location')]", "environment": "[parameters('environment')]", "severity": "[parameters('severity')]" } }, { "type": "Microsoft.Web/serverfarms", "apiVersion": "2022-03-01", "name": "[variables('appServicePlan')]", "location": "[parameters('location')]", "dependsOn": ["[resourceId('Microsoft.Resources/resourceGroups', variables('resourceGroup'))]"], "tags": { "location": "[parameters('location')]", "environment": "[parameters('environment')]", "severity": "[parameters('severity')]" }, "sku": { "name": "[parameters('hostingPlanSkuName')]", "tier": "[parameters('hostingPlanSkuTier')]", "size": "[parameters('hostingPlanSkuName')]", "family": "B", "capacity": "[parameters('hostingPlanSkuCapacity')]" }, "kind": "app", "properties": { "zoneRedundant": false } }, { "type": "Microsoft.Insights/components", "apiVersion": "2020-02-02", "name": "[variables('ApplicationInsights')]", "location": "[parameters('location')]", "dependsOn": ["[resourceId('Microsoft.Resources/resourceGroups', variables('resourceGroup'))]"], "tags": { "location": "[parameters('location')]", "environment": "[parameters('environment')]", "severity": "[parameters('severity')]" }, "kind": "web", "properties": { "Application_Type": "web" } }, { "type": "Microsoft.Web/sites", "apiVersion": "2022-03-01", "name": "[variables('appService')]", "location": "[parameters('location')]", "dependsOn": [ "[resourceId(variables('resourceGroup'), 'Microsoft.Web/serverfarms', variables('appServicePlan'))]", "[resourceId(variables('resourceGroup'), 'Microsoft.Insights/components', variables('ApplicationInsights'))]" ], "tags": { "location": "[parameters('location')]", "environment": "[parameters('environment')]", "severity": "[parameters('severity')]" }, "kind": "app", "properties": { "serverFarmId": "[resourceId(variables('resourceGroup'), 'Microsoft.Web/serverfarms', variables('appServicePlan'))]", "siteConfig": { "appSettings": [ { "name": "APPINSIGHTS_INSTRUMENTATIONKEY", "value": "[reference(resourceId(variables('resourceGroup'), 'Microsoft.Insights/components', variables('ApplicationInsights')), '2020-02-02').InstrumentationKey]" }, { "name": "ApplicationInsightsAgent_EXTENSION_VERSION", "value": "~2" } ] } } } ] }
额外说明
- 订阅级部署时,所有要放在新建资源组内的资源,必须通过
resourceId明确指定所属资源组,否则Azure会默认尝试在订阅级创建这些资源(而这类资源只能在资源组级创建) reference函数引用AI资源时,同样要使用完整的resourceId路径,确保能正确获取到新建RG内的AI资源的InstrumentationKey
内容的提问来源于stack exchange,提问作者Ross
相关产品推荐
相关产品推荐

