0

I want to use a DocumentDB(MongoDB) connection string as an environment variable in an Azure Resource Management Template. Forexample i have a resource group which has a wep app and a DocumentDB(MongoDB) database.

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

How can i assign an environment variable to a connection string in template?

Pythonist
  • 115
  • 1
  • 9

2 Answers2

0

After a bit of a struggle:

"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
  • 59,484
  • 5
  • 63
  • 109
  • Hey @4c74356b41 it is a MongoDB connection string. – Pythonist Mar 30 '17 at 20:53
  • Yes :) thanx but they are different and it gives error. – Pythonist Mar 30 '17 at 20:57
  • well, this gives are an answer from which you can easily create your own, I have no idea how mongodb connection string looks, but this gives you all you need to create that, I'm pretty sure @LeventOner – 4c74356b41 Mar 30 '17 at 20:59
  • I dont want to take your time but i really can't find, in MongoDB i should define only the password. var url = 'mongodb://:@.documents.azure.com:10250/?ssl=true'; – Pythonist Mar 30 '17 at 21:05
0

ARM template supports listKeys and list{Value} function, more details we can refer to ARM template function. We can find DocumentDB list connection strings API, so we can use listconnectionstrings function to get documentdb connection string in the ARM template.

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

It works correct on my side. If we want to add appsetting for WebApp, we also can do with following code

  "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

Check the result from the Azure portal.

enter image description here

Update:

ARM template demo code

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

      ]
    }
  ]
}

Paramter file

{
    "$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 - MSFT
  • 22,436
  • 3
  • 23
  • 40
  • Hey @Tom Sun, if i define the connection string in properties it gives this error Deployment template validation failed: 'The template parameter documentdb' is not found. Please see https://aka.ms/arm-template/#parameters for usage details.'. (Code: InvalidTemplate) And if i in appsettings define, then it does not give any value but the reference code [listConnectionStrings(resourceId('Microsoft.DocumentDb/databaseAccounts', parameters('documentdb')), '2015-04-08').connectionStrings[0].connectionString] – Pythonist Mar 31 '17 at 06:26
  • we need to define the paramter `documentdb` in the template. `documentdb` is the name of documentdb(MongoDB). You also can get the define from the screenshot in the parameter section. – Tom Sun - MSFT Mar 31 '17 at 06:30
  • yeah i tried with his name in properties, but it didn't accept too, now i'm testing with appSettings – Pythonist Mar 31 '17 at 06:36
  • It didn't work, in my template parameter is defined like: "databaseAccounts_feedsdb_name_1": { "defaultValue": "feedsdb", "type": "String" }, i don't know what i do wrong – Pythonist Mar 31 '17 at 06:46