【问题标题】:How to reference an existing storage accounts in an ARM template of a function app如何在函数应用的 ARM 模板中引用现有存储帐户
【发布时间】:2021-09-18 23:43:26
【问题描述】:

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

}

【问题讨论】:

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


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 谢谢!我怀疑是dependsOn 块。但即使没有dependsOn,它也无法正常工作。我已将其删除,但仍然无法在 ARM 部署中识别存储帐户。
    • 你也在使用listKeys。参考listKeys文档;当资源未在同一个模板中定义时,您必须使用资源 ID,而不是资源名称。
    【解决方案2】:

    通过在存储帐户资源中添加 "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')]"
        }
    }
    

    }

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 2020-10-02
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 2020-09-04
      • 2021-09-30
      • 1970-01-01
      相关资源
      最近更新 更多