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

时间:2021-07-08 13:41:12

标签: azure-functions arm-template azure-storage-account

我想使用 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')]"
    }
}

}

2 个答案:

答案 0 :(得分:0)

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

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

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

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

答案 1 :(得分:0)

通过在存储帐户资源中添加 "mode":"Incremental" 属性,我设法使用现有存储帐户部署了函数应用。新的 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')]"
    }
}

}

相关问题