如何在Azure资源管理模板中将DocumentDB(MongoDB)连接字符串用作环境变量

时间:2017-03-30 19:43:03

标签: azure azure-resource-manager azure-resource-group

我想在Azure资源管理模板中使用DocumentDB(MongoDB)连接字符串作为环境变量。例如,我有一个资源组,它有一个wep应用程序和一个DocumentDB(MongoDB)数据库。

"siteConfig": {
          "appSettings": [
            {
              "name": "db",
              "value": "connection string"
            }
          ]
        }

如何将环境变量分配给模板中的连接字符串?

2 个答案:

答案 0 :(得分:0)

经过一番努力:

"appSettings": [{
                    "Name": "DOCUMENTDB_ENDPOINT",
                    "Value": "[reference(concat('Microsoft.DocumentDb/databaseAccounts/', parameters('databaseAccountName'))).documentEndpoint]"
                }, {
                    "Name": "DOCUMENTDB_PRIMARY_KEY",
                    "Value": "[listKeys(resourceId('Microsoft.DocumentDb/databaseAccounts', parameters('databaseAccountName')), '2015-04-08').primaryMasterKey]"
                }]

答案 1 :(得分:0)

ARM模板支持listKeyslist{Value}功能,有关详细信息,请参阅ARM template function。 我们可以找到DocumentDB list connection strings API,因此我们可以使用listconnectionstrings函数在ARM模板中获取documentdb连接字符串。

"appSettings": [
            {
              "name": "db",
              "value": " [listConnectionStrings(resourceId('Microsoft.DocumentDb/databaseAccounts', parameters('documentdb')), '2015-04-08').connectionStrings[0].connectionString]"
            }

这对我来说是正确的。如果我们想为WebApp添加appsetting,我们也可以使用以下代码

  "resources": [
      {
          "name": "appsettings",
          "type": "config",
          "apiVersion": "2015-08-01",
          "dependsOn": [
              "[resourceId('Microsoft.Web/sites', variables('webSiteName'))]"
          ],
          "tags": {
              "displayName": "appsetting"
          },
        "properties": {
          "db": "[listConnectionStrings(resourceId('Microsoft.DocumentDb/databaseAccounts', parameters('documentdb')), '2015-04-08').connectionStrings[0].connectionString]"
        }
      }

  ]

enter image description here

检查Azure门户网站的结果。

enter image description here

<强>更新

ARM模板演示代码

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "hostingPlanName": {
      "type": "string",
      "minLength": 1
    },
    "documentdb": {
      "type": "string",
      "minLength": 1
    },
    "skuName": {
      "type": "string",
      "defaultValue": "S1",
      "allowedValues": [
        "F1",
        "D1",
        "B1",
        "B2",
        "B3",
        "S1",
        "S2",
        "S3",
        "P1",
        "P2",
        "P3",
        "P4"
      ],
      "metadata": {
        "description": "Describes plan's pricing tier and instance size. Check details at https://azure.microsoft.com/en-us/pricing/details/app-service/"
      }
    },
    "skuCapacity": {
      "type": "int",
      "defaultValue": 1,
      "minValue": 1,
      "metadata": {
        "description": "Describes plan's instance count"
      }
    }
  },
  "variables": {
    "webSiteName": "[concat('webSite', uniqueString(resourceGroup().id))]",
    "docDbName": "tomdocumentdb",
    "storageAccountId": "[concat(resourceGroup().id,'/providers/Microsoft.Storage/storageAccounts/', parameters('storageAccountName'))]"
  },
  "resources": [
    {
      "apiVersion": "2015-08-01",
      "name": "[parameters('hostingPlanName')]",
      "type": "Microsoft.Web/serverfarms",
      "location": "[resourceGroup().location]",
      "tags": {
        "displayName": "HostingPlan"
      },
      "sku": {
        "name": "[parameters('skuName')]",
        "capacity": "[parameters('skuCapacity')]"
      },
      "properties": {
        "name": "[parameters('hostingPlanName')]"
      }
    },
    {
      "apiVersion": "2015-08-01",
      "name": "[variables('webSiteName')]",
      "type": "Microsoft.Web/sites",
      "location": "[resourceGroup().location]",
      "tags": {
        "[concat('hidden-related:', resourceGroup().id, '/providers/Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]": "Resource",
        "displayName": "Website"
      },
      "dependsOn": [
        "[concat('Microsoft.Web/serverfarms/', parameters('hostingPlanName'))]"
      ],
      "properties": {
        "name": "[variables('webSiteName')]",
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', parameters('hostingPlanName'))]"
      },

      "resources": [
          {
              "name": "appsettings",
              "type": "config",
              "apiVersion": "2015-08-01",
              "dependsOn": [
                  "[resourceId('Microsoft.Web/sites', variables('webSiteName'))]"
              ],
              "tags": {
                  "displayName": "appsetting"
              },
            "properties": {
              "db": "[listConnectionStrings(resourceId('Microsoft.DocumentDb/databaseAccounts', parameters('documentdb')), '2015-04-08').connectionStrings[0].connectionString]"
            }
          }

      ]
    }
  ]
}

参数文件

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "hostingPlanName": {
            "value": "tomtest" //your hostingplan name
        },
        "skuName": {
            "value": "B1"
        },
        "documentdb": {
            "value": "tomdocument" // your documentdb name
        }

    }
}