Home > Enterprise >  ARM: use copy to add connection strings to an app from an array of database names
ARM: use copy to add connection strings to an app from an array of database names

Time:09-01

In my ARM, I have a website with multiple connection strings. I have a variable which is an array of database names. I'm trying to use the copy function to add the connection strings to the website from that array.

Here is the resource:

{
      "name": "[variables('AppName')]",
      "type": "Microsoft.Web/sites",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-08-01",
      "dependsOn": [
        "[resourceId('Microsoft.Web/serverfarms', variables('AppPlanName'))]"
      ],
      "tags": {
        "[concat('hidden-related:', resourceId('Microsoft.Web/serverfarms', variables('AppPlanName')))]": "Resource",
        "displayName": "Web App"
      },
      "properties": {
        "name": "[variables('AppName')]",
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms', variables('AppPlanName'))]",
        "httpsOnly": true,
        "siteConfig": {
          "connectionStrings": [
            {
              "name": "variables('databasesNames')[copyIndex()]",
              "connectionString": "[concat('Server=tcp:', variables('SqlServerName'), '.database.windows.net,1433;Initial Catalog=',variables('databasesNames')[copyIndex()], ';Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;User ID=', variables('dbAdminLogin'), ';Password=', variables('singleQuote'), parameters('serverAdminLoginPassword'), variables('singleQuote'), ';')]",
              "copy": {
                "name": "loopingConnectionStrings",
                "count": "[length(variables('databasesNames'))]"
              }
            }
          ]
        }
      },

This gives me the following error:

The template function 'copyIndex' is not expected at this location. The function can only be used in a resource with copy specified

I think that I unfortunately I can't use copy there. Can anyone confirm that or correct what's wrong or suggest another approach?

CodePudding user response:

Looking at the documentation, it is possible: Property iteration in ARM templates

This should work in your case:

"siteConfig": {
  "copy": [
    {
      "name": "connectionStrings",
      "count": "[length(variables('databasesNames'))]",
      "input": {
        "name": "[variables('databasesNames')[copyIndex('connectionStrings')]]",
        "connectionString": "[format('Server=tcp:{0}.database.windows.net,1433;Initial Catalog={1};Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;User ID={2};Password={3}{4}{5};', variables('SqlServerName'), variables('databasesNames')[copyIndex('connectionStrings')], variables('dbAdminLogin'), variables('singleQuote'), parameters('serverAdminLoginPassword'), variables('singleQuote'))]"
      }
    }
  ]
}

On another note, you should have a look at Bicep, it provides a nicer syntax especially for looping: Iterative loops in Bicep

  • Related