使用功能ARM模板时出现现有资源错误

时间:2020-04-01 05:36:58

标签: c# azure azure-pipelines azure-template

我正在为CI / CD编写功能ARM模板,该功能已经托管在Azure门户上。现在,我决定创建一个ARM功能模板,并遇到以下错误。它说我无法创建已经存在的资源。我知道资源已经存在,但是我想基于模板创建CI / CD管线。我尝试了增量模式,但似乎缺少了一些东西。在线有任何指导吗?我已经从AZURE Git中获取了Azure功能模板。

C:\ Program Files \ Microsoft SDKs \ Azure.NET SDK \ v2.9> az组部署验证--mode增量--resource-group cloud-shell-storage-southeastasia --template-file azuredeploy.json < / p>

请提供'appName'的字符串值(?以获取帮助):SchedulerHttpFunctionSample

azuredeploy.json
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "appName": {
      "type": "string",
      "metadata": {
        "description": "The name of the function app that you wish to create."
      }
    },
    "storageAccountType": {
      "type": "string",
      "defaultValue": "Standard_LRS",
      "allowedValues": [
        "Standard_LRS",
        "Standard_GRS",
        "Standard_RAGRS"
      ],
      "metadata": {
        "description": "Storage Account type"
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Location for all resources."
      }
    },
    "runtime": {
      "type": "string",
      "defaultValue": "node",
      "allowedValues": [
        "node",
        "dotnet",
        "java"
      ],
      "metadata": {
        "description": "The language worker runtime to load in the function app."
      }
    }
  },
  "variables": {
    "functionAppName": "[parameters('appName')]",
    "hostingPlanName": "[parameters('appName')]",
    "applicationInsightsName": "[parameters('appName')]",
    "storageAccountName": "[concat(uniquestring(resourceGroup().id), 'azfunctions')]",
    "storageAccountid": "[concat(resourceGroup().id,'/providers/','Microsoft.Storage/storageAccounts/', variables('storageAccountName'))]",
    "functionWorkerRuntime": "[parameters('runtime')]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "name": "[variables('storageAccountName')]",
      "apiVersion": "2018-12-01",
      "location": "[parameters('location')]",
      "kind": "Storage",
      "sku": {
        "name": "[parameters('storageAccountType')]"
      },
      "properties":{
        "mode":"Incremental"
      }
    },
    {
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2018-02-01",
      "name": "[variables('hostingPlanName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Y1",
        "tier": "Dynamic"
      },
      "properties": {
        "mode":"Incremental",
        "name": "[variables('hostingPlanName')]",
        "computeMode": "Dynamic"
      }
    },
    {
      "apiVersion": "2018-02-01",
      "type": "Microsoft.Web/sites",
      "name": "[variables('functionAppName')]",
      "location": "[parameters('location')]",
      "kind": "functionapp",
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
      ],
      "properties": {
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('hostingPlanName'))]",
        "mode":"Incremental",
        "siteConfig": {
          "appSettings": [
            {
              "name": "AzureWebJobsStorage",
              "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
            },
            {
              "name": "WEBSITE_CONTENTAZUREFILECONNECTIONSTRING",
              "value": "[concat('DefaultEndpointsProtocol=https;AccountName=', variables('storageAccountName'), ';AccountKey=', listKeys(variables('storageAccountid'),'2015-05-01-preview').key1)]"
            },
            {
              "name": "WEBSITE_CONTENTSHARE",
              "value": "[toLower(variables('functionAppName'))]"
            },
            {
              "name": "FUNCTIONS_EXTENSION_VERSION",
              "value": "~2"
            },
            {
              "name": "WEBSITE_NODE_DEFAULT_VERSION",
              "value": "~10"
            },
            {
              "name": "APPINSIGHTS_INSTRUMENTATIONKEY",
              "value": "[reference(resourceId('microsoft.insights/components/', variables('applicationInsightsName')), '2015-05-01').InstrumentationKey]"
            },
            {
              "name": "FUNCTIONS_WORKER_RUNTIME",
              "value": "[variables('functionWorkerRuntime')]"
            }
          ]
        }
      }
    },
    {
      "apiVersion": "2018-02-01",
      "name": "[variables('applicationInsightsName')]",
      "type": "microsoft.insights/components",
      "location": "East US",
      "tags": {
        "[concat('hidden-link:', resourceGroup().id, '/providers/Microsoft.Web/sites/', variables('applicationInsightsName'))]": "Resource"
      },
      "properties": {
        "ApplicationId": "[variables('applicationInsightsName')]",
        "Request_Source": "IbizaWebAppExtensionCreate",
        "mode":"Incremental"
      }
    }
  ]
}

天蓝色错误:InvalidResourceLocation 消息:资源“ SchedulerHttpFunctionSample”已经存在于资源组“ cloud-shell-storage-southeastasia”中的位置“ southcentralus”中。无法在“ southeastasia”位置创建具有相同名称的资源。请选择一个新的资源名称。

1 个答案:

答案 0 :(得分:2)

这是因为该名称已被使用。功能应用名称必须是全局唯一的。

我建议使用后缀。在变量部分中,创建一些变量:

"variables": {
    "suffix": "[uniqueString(resourceGroup().id, resourceGroup().location)]",
    "functionAppName": "[concat(parameters('appName'), variables('suffix'))]"
}

函数uniqueString将根据收到的信息生成一个唯一的字符串。因此,如果您在相同的资源组和区域中重新部署,则后缀将相同。

看看这篇文章:http://www.frankysnotes.com/2019/05/how-to-make-your-deployment-successful.html或这段视频https://www.youtube.com/watch?v=dnb-f4C052w