如何在函数应用程序的ARM模板中引用现有存储帐户



我想用ARM模板创建一个函数应用程序,以便ARM模板引用现有的存储帐户(此模板中的"onestoragebo"(,而不是创建新帐户。存储在ARM模板下部署时,我收到以下错误。请帮忙!!

InvalidTemplate-部署模板验证失败:"模板引用"onestoragebo"无效:找不到具有此名称的模板资源或资源副本。"。请参阅https://aka.ms/arm-template-expressions/#reference有关用法详细信息。'

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"baseName": {
"defaultValue": "func",
"type": "string",
"metadata": {
"description": "Name use as base-template to named the resources deployed in Azure."
}
},
"funcname": {
"defaultValue": "projectitemedited",
"type": "string",
"metadata": {
"description": "description"
}
},
"storageaccountname": {
"defaultValue": "onestoragebo",
"type": "string",
"metadata": {
"description": "description"
}
}


},
"variables": {
"suffix": "[toLower(resourceGroup().name)]",
"funcAppName": "[toLower(concat(parameters('baseName'),'-', variables('suffix'), '-', parameters('funcname')))]",
"serverFarmName": "[concat('plan', '-', variables('suffix'), '-', substring(parameters('funcname'), 0, min(length(parameters('funcname')),16)))]",
"appInsightsName": "[toLower(concat('appi','-', variables('suffix'),'-', parameters('funcname')))]"
},
"resources": [
{
"type": "Microsoft.Web/sites",
"apiVersion": "2018-11-01",
"name": "[variables('funcAppName')]",
"location": "West Europe",
"kind": "functionapp",
"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms',variables('serverFarmName'))]",
"[parameters('storageaccountname')]"
],
"properties": {
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('serverFarmName'))]",
"siteConfig": {
"appSettings": [
{
"name": "AzureWebJobsDashboard",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=', parameters('storageaccountname'), ';AccountKey=', listKeys(parameters('storageaccountname'),'2019-06-01').key1)]"
},
{
"name": "AzureWebJobsStorage",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=', parameters('storageaccountname'), ';AccountKey=', listKeys(parameters('storageaccountname'),'2019-06-01').key1)]"
},
{
"name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=', parameters('storageaccountname'), ';AccountKey=', listKeys(parameters('storageaccountname'),'2019-06-01').key1)]"
},
{
"name": "WEBSITE_CONTENTSHARE",
"value": "[variables('funcAppName')]"
},
{
"name": "FUNCTIONS_EXTENSION_VERSION",
"value": "~1"
},
{
"name": "FUNCTIONS_WORKER_RUNTIME",
"value": "powershell"
},
{
"name": "APPINSIGHTS_INSTRUMENTATIONKEY",
"value": "[reference(resourceId('microsoft.insights/components/', variables('appInsightsName')), '2015-05-01').InstrumentationKey]"
}

]
},
"httpsOnly": true
}
},
{
"type": "Microsoft.Web/sites/config",
"apiVersion": "2018-11-01",
"name": "[concat(variables('funcAppName'), '/web')]",
"location": "West Europe",
"dependsOn": [
"[resourceId('Microsoft.Web/sites', variables('funcAppName'))]"
],
"properties": {
"ftpsState": "FtpsOnly"
}
},

{
"type": "Microsoft.Web/serverfarms",
"apiVersion": "2018-02-01",
"name": "[variables('serverFarmName')]",
"location": "[resourceGroup().location]",
"sku": {
"name": "Y1",
"tier": "Dynamic"
},
"properties": {
"name": "[variables('serverFarmName')]",
"computeMode": "Dynamic"
}
},
{
"apiVersion": "2015-05-01",
"name": "[variables('appInsightsName')]",
"type": "Microsoft.Insights/components",
"kind": "web",
"location": "[resourceGroup().location]",
"tags": {
"[concat('hidden-link:', resourceGroup().id, '/providers/Microsoft.Web/sites/', variables('funcAppName'))]": "Resource"
},
"properties": {
"Application_Type": "web",
"ApplicationId": "[variables('appInsightsName')]"
}
}


],
"outputs": {
"functionappname": {
"type": "string",
"value": "[variables('funcAppName')]"
}
}

}

我通过添加"模式":"增量">属性。将附加新的ARM文件。如果有人知道更好的解决方案,请发表评论。

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"baseName": {
"defaultValue": "func",
"type": "string",
"metadata": {
"description": "Name use as base-template to named the resources deployed in Azure."
}
},
"funcname": {
"defaultValue": "projectitemadded",
"type": "string",
"metadata": {
"description": "description"
}
}

},
"variables": {
"suffix": "[toLower(resourceGroup().name)]",
"funcAppName": "[toLower(concat(parameters('baseName'),'-', variables('suffix'), '-', parameters('funcname')))]",
"funcStorageName": "[tolower(concat('st', substring(variables('suffix'), 0, min(length(variables('suffix')),16))))]",
"serverFarmName": "[concat('plan', '-', variables('suffix'), '-', substring(parameters('funcname'), 0, min(length(parameters('funcname')),16)))]",
"appInsightsName": "[toLower(concat('appi','-', variables('suffix'),'-', parameters('funcname')))]"
},
"resources": [
{
"type": "Microsoft.Web/sites",
"apiVersion": "2018-02-01",
"name": "[variables('funcAppName')]",
"location": "West Europe",
"kind": "functionapp",
"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms',variables('serverFarmName'))]",
"[resourceId('Microsoft.Storage/storageAccounts', variables('funcStorageName'))]"
],
"properties": {
"serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('serverFarmName'))]",
"siteConfig": {
"appSettings": [
{
"name": "AzureWebJobsDashboard",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('funcStorageName'), ';AccountKey=', listKeys(variables('funcStorageName'),'2015-05-01-preview').key1)]"
},
{
"name": "AzureWebJobsStorage",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('funcStorageName'), ';AccountKey=', listKeys(variables('funcStorageName'),'2015-05-01-preview').key1)]"
},
{
"name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
"value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('funcStorageName'), ';AccountKey=', listKeys(variables('funcStorageName'),'2015-05-01-preview').key1)]"
},
{
"name": "WEBSITE_CONTENTSHARE",
"value": "[variables('funcAppName')]"
},
{
"name": "FUNCTIONS_EXTENSION_VERSION",
"value": "~1"
},
{
"name": "FUNCTIONS_WORKER_RUNTIME",
"value": "powershell"
},
{
"name": "APPINSIGHTS_INSTRUMENTATIONKEY",
"value": "[reference(resourceId('microsoft.insights/components/', variables('appInsightsName')), '2015-05-01').InstrumentationKey]"
}

]
},
"httpsOnly": true
}
},
{
"type": "Microsoft.Web/sites/config",
"apiVersion": "2018-11-01",
"name": "[concat(variables('funcAppName'), '/web')]",
"location": "West Europe",
"dependsOn": [
"[resourceId('Microsoft.Web/sites', variables('funcAppName'))]"
],
"properties": {
"ftpsState": "FtpsOnly"
}
},
{
"type": "Microsoft.Storage/storageAccounts",
"apiVersion": "2018-07-01",
"name": "[variables('funcStorageName')]",
"location": "[resourceGroup().location]",
"tags": {
"displayName": "funStorageName"
},
"sku": {
"name": "Standard_LRS"
},
"kind": "StorageV2",
"properties": {
"supportsHttpsTrafficOnly": true,
"mode":"Incremental"
}
},
{
"type": "Microsoft.Web/serverfarms",
"apiVersion": "2018-02-01",
"name": "[variables('serverFarmName')]",
"location": "[resourceGroup().location]",
"sku": {
"name": "Y1",
"tier": "Dynamic"
},
"properties": {
"name": "[variables('serverFarmName')]",
"computeMode": "Dynamic"
}
},
{
"apiVersion": "2015-05-01",
"name": "[variables('appInsightsName')]",
"type": "Microsoft.Insights/components",
"kind": "web",
"location": "[resourceGroup().location]",
"tags": {
"[concat('hidden-link:', resourceGroup().id, '/providers/Microsoft.Web/sites/', variables('funcAppName'))]": "Resource"
},
"properties": {
"Application_Type": "web",
"ApplicationId": "[variables('appInsightsName')]"
}
}


],
"outputs": {
"functionappname": {
"type": "string",
"value": "[variables('funcAppName')]"
}
}

}

dependsOn块中,您指的是storageAccountName参数。没有必要那样做。根据您的问题,存储帐户名称已存在。您不能依赖ARM模板中未定义的资源。dependsOn的存在是为了确保以正确的顺序创建资源。

从逻辑上思考:如果模板中没有定义资源,那么如果资源不存在,该怎么办?它不能等待资源被创建,因为没有任何东西告诉ARM模板如何创建它

"dependsOn": [
"[resourceId('Microsoft.Web/serverfarms',variables('serverFarmName'))]"
],

您也在使用listKeys。请参阅listKeys文档;如果资源不是在同一个模板中定义的,则必须使用资源ID,而不是资源名称。

最新更新