无法在 ARM 模板中正确使用引用函数



我有一个主模板。其中:我有一个链接的模板来创建应用程序见解。以及另一个链接模板调用来创建网络应用程序。

在webapp链接的模板调用中,我想将AIKey作为参数传递,但这是有问题的。如果我这样做:

"value": "[reference(resourceId('Microsoft.Insights/components', variables('appInsightsName')),'2014-04-01').InstrumentationKey]"

这是第一次失败,因为引用值会立即求值,而appinsights还不存在。即使我在Web应用程序中使用appinsights链接模板资源调用的依赖项,也会发生这种情况。

所以我想也许我可以在引用中使用引用,以防止过早评估它,但这不起作用——似乎你不能在引用中有引用。

"value": "[reference(reference('AppInsights').outputs.resourceID.value,'2014-04-01').InstrumentationKey]"

我不想把AI密钥放在链接模板的输出中,因为它会把它放在明文中。有没有办法实现我想要做的事情?

这里有一些代码片段,这是一个单独的模板,但模板很大,所以我没有包括所有内容:

调用AI嵌套模板:

{
"name": "AppInsights",
"type": "Microsoft.Resources/deployments",
"apiVersion": "2016-09-01",
"dependsOn": [],
"properties": {
"mode": "Incremental",
"templateLink": {
"uri": "[variables('AppInsightsTemplatePath')]",
"contentVersion": "1.0.0.0"
},
"parameters": {
"tagValues": {
"value": "[parameters('tagValues')]"
},
"workspaceId": {
"value": "[parameters('workspaceId')]"
},
"appInsightsName": {
"value": "[variables('appInsightsName')]"
}
}
}
},

调用WebAPP模板:

{
"name": "WebApp",
"type": "Microsoft.Resources/deployments",
"apiVersion": "2016-09-01",
"dependsOn": [
"AppInsights",
"AppServicePlan"
],
"properties": {
"mode": "Incremental",
"templateLink": {
"uri": "[variables('WebAppTemplatePath')]",
"contentVersion": "1.0.0.0"
},
"parameters": {
"siteConfig": {
"value": {
"netFrameworkVersion": "v4.7",
"phpVersion": "",
"pythonVersion": "",
"javaVersion": "",
"nodeVersion": "",
"linuxFxVersion": "",
"use32BitWorkerProcess": "False",
"webSocketsEnabled": "False",
"alwaysOn": "True",
"managedPipelineMode": "Integrated",
"remoteDebuggingEnabled": "False",
"appSettings": [
{
"name": "APPINSIGHTS_INSTRUMENTATIONKEY",
"value": "[reference(resourceId('Microsoft.Insights/components', variables('appInsightsName')),'2014-04-01').InstrumentationKey]"
}
],
"connectionStrings": [],
"defaultDocuments": [],
"handlerMappings": [],
"virtualApplications": [
{
"virtualPath": "/",
"physicalPath": "site\wwwroot",
"preloadEnabled": "True",
"virtualDirectories": ""
}
],
"minTlsVersion": "1.2"
}
}
}
}
},

错误如下:"code":"ResourceNotFound","message":"未找到资源组"MyResourceGroup"下的资源"Microsoft.Insights/components/MyAppInsightsName"。">

与其将键传递到web应用程序模板中,不如传递AI资源的resourceId,并将reference((调用放入web应用程序中。不要输出resourceId,只需像第一个代码片段中那样传入字符串即可。

"value": "[resourceId('Microsoft.Insights/components', variables('appInsightsName'))]

此外,如果你能分享你的模板(或足够多的模板进行复制(,那将很有帮助。。。我以为我们纠正了这种行为,但听起来我们没有,所以如果我们错过了一个案例,那就太好了。

好吧,我想我明白你在说什么,这就是你的模板的样子:

parent:
- child1:
- application insights
- child2:
- webapp

所以你在这里没有太多好的选择。首先:这种设置意义不大。仅仅为了创建嵌套部署而创建嵌套部署只会导致创建堆栈溢出问题;(你可以把它转换成一个平面模板,它会很好地工作,或者你可以像这样重新排列它们:

父级:-child1:-应用程序见解-child2:-webapp

您也可以尝试将配置作为child2:中的独立资源进行更新

{
"name": "appsettings",
"type": "config",
"apiVersion": "2015-08-01",
"properties": {
"APPINSIGHTS_INSTRUMENTATIONKEY": "[reference(resourceId('Microsoft.Insights/components', variables('appInsightsName')),'2014-04-01').InstrumentationKey]"
}
}

另一个选项是在child1中输出它,并在child2中引用,这将起作用。你可以将部署作为脚本的一部分删除(如果你不想删除child1,你可以创建一个只执行此操作的代理部署(,这样Azure中就不会有AI密钥的踪迹。另一个选项有点棘手:

父级:-child1:-child1.1(前child1(:-应用程序见解-child2:<lt;此依赖于子项1-child2.1(前child2(:<lt;这引用了AI密钥-webapp

最新更新