【问题标题】:How to use a DocumentDB(MongoDB) connection string as an Environment variable in an Azure Resource Management Template如何在 Azure 资源管理模板中使用 DocumentDB(MongoDB) 连接字符串作为环境变量
【发布时间】:2017-03-30 19:43:02
【问题描述】:

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

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

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

【问题讨论】:

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


    【解决方案1】:

    ARM模板支持listKeyslist{Value}功能,更多细节可以参考ARM template function。 我们可以找到 DocumentDBlist 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]"
            }
          }
    
      ]
    

    从 Azure 门户检查结果。

    更新:

    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
            }
    
        }
    }
    

    【讨论】:

    • 嘿@Tom Sun,如果我在属性中定义连接字符串,它会给出此错误部署模板验证失败:找不到'模板参数documentdb'。请参阅aka.ms/arm-template/#parameters 了解使用详情。'。 (代码:InvalidTemplate)如果我在appsettings中定义,那么它除了参考代码[listConnectionStrings(resourceId('Microsoft.DocumentDb/databaseAccounts',parameters('documentdb')),'2015-04-08之外没有给出任何值').connectionStrings[0].connectionString]
    • 我们需要在模板中定义参数documentdbdocumentdb 是 documentdb(MongoDB) 的名称。您也可以从参数部分的屏幕截图中获取定义。
    • 是的,我尝试在属性中使用他的名字,但它也不接受,现在我正在使用 appSettings 进行测试
    • 它不起作用,在我的模板参数中定义为:“databaseAccounts_feedsdb_name_1”:{“defaultValue”:“feedsdb”,“type”:“String”},我不知道是什么我做错了
    【解决方案2】:

    经过一番挣扎:

    "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]"
                    }]
    

    【讨论】:

    • 嘿 @4c74356b41 这是一个 MongoDB 连接字符串。
    • 是 :) thanx 但它们不同,并且会出错。
    • 嗯,这给出了一个答案,您可以轻松地创建自己的答案,我不知道 mongodb 连接字符串的外观,但这为您提供了创建它所需的一切,我很确定@LeventOner
    • 我不想花时间但我真的找不到,在 MongoDB 中我应该只定义密码。 var url = 'mongodb://:@.documents.azure.com:10250/?ssl=true';
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 2023-02-04
    相关资源
    最近更新 更多