Azure ARM Template: Define Web App Connection Strings - Perficient Blogs
Blog
  • Topics
  • Industries
  • Partners

Explore

Topics

Industries

Partners

Azure ARM Template: Define Web App Connection Strings

Previous articles covered what Azure Resource Manager is, along with how to setup an Azure Resource Group project within Visual Studio 2015 to deploy an ASP.NET Web Application to Azure as an App Service Web App. Being able to deploy an application to Azure is just the first step. In order to really use ARM Templates for production deployments, the various settings for the Web App need to be set. As a result, this article will build on top of the foundation of the previous articles, and define how to setup and manage Web App Connection Strings as ARM Template Parameters.To start, you’ll want to be familiar with the basics of ARM Templates. If you need to, please go read / reference the previous article in this series: “Deploy Azure Web App using ARM Template from Visual Studio 2015

Statically Defined Connection Strings

Defining the Connection Strings for an App Service Web App within an ARM Template is performed by adding a new “siteConfig/connectionStrings” section to the “properties” configuration section for the JSON that defines the deployment settings for the Azure App Service Web App.
When using an Azure Resource Group project within Visual Studio 2015, the easiest way to find this location within the ARM Template file is to:

  1. Open the ARM Template file
  2. Click on “Website” in the JSON Outline window

After performing these steps, the editor will lightly highlight the JSON section that contains the Website configuration definition. Reference the below screenshot to see what this part of the file looks like.
AzureWebAppARMTemplateWebsiteSectionScreenshot
To add the “connectionStrings” configuration section, go to the “properties/siteConfig” section of the Website deployment configuration. Underneath the existing “properties/name” and “properties/serverFarmId” settings, simply add a new section named “siteConfig” with a child section named “connectionStrings”. Within this new “properties/siteConfig/connectionStrings” configuration section is where any defined Connection Strings will be placed.
Here is a screenshot of what the resulting JSON looks like with the new “properties/siteConfig/connectionStrings” section added, with a single Connection String named “ConnString1” defined:
AzureWebAppARMTemplateWebsiteSectionScreenshotWithConnectionStrings
For additional clarity here’s the same JSON snippet from the file:

{
  "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'))]",
    "siteConfig": {
      "connectionStrings": [
        {
          "name": "ConnString1",
          "connectionString": "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;"
        }
      ]
    }
  }
}

Connection Strings are defined within the “properties/siteConfig/connectionStrings” section of the JSON as an Array of objects with the following properties / values:

  • “name” – This is a String value that defines the key for the Connection String in the collection
  • “connectionString” – This is a String that contains the actual Connection String value
  • “type” – The Type is not displayed above since it’s a little more complex, but the Type is basically just an Integer value that defines an Enumeration value for the Type of Connection String

The following section of the article will describe how to set the specific Connection String Type.

Connection String Types

When configuring Web App Connection Strings another settings that’s important to set is the Connection String Type. The available values for Type within the Azure Portal are: SQL Database, SQL Server, MySQL, and Custom. If you don’t specify the Type within the ARM Template, it will default to a value of Zero (0) which means MySQL. It’s likely that most often the Connection String defined will be pointing to a SQL Server or Azure SQL database. So, it’s good to get the Type set correctly to reflect the actual Type it is.
AzureWebAppConnectionStringTypesDropdown
Setting the Connection String Type within the ARM Template JSON is done by specifying an Integer value for the Connection String Type enumeration.
The value can be set as follows:

"connectionStrings": [
    {
        "name": "ConnString1",
        "connectionString": "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;",
        "type": 2 /* SQL Database */
    }
]

Also, in order to define the various Types supported within Azure, just use the following values for it’s respective Connection String Type:
[table “19” not found /]

Parameterized Connection Strings

Configuring static values within the ARM Template for Web App Connection Strings is fine when deploying to only a single environment. However, almost all applications end up being deployed to multiple environments. The ability to define Deployment Parameters with an ARM Template allows for a Parameters file to be created for each Environment, each with specific Connection String values defined for that particular Environment.
To parameterize a Web App Connection String, the following steps need to be taken:

  1. Add new Parameter to ARM Template
  2. Set Web App Connection String to populate its value from the Parameter
  3. Specify the Parameter in the ARM Template Parameters file

Add Parameter to ARM Template
The definitions for all the Parameters that are supported by an Azure ARM Template are contained within the “parameters” section of the JSON file. This section will include all Parameters for the ARM Template, such as the Package Folder and Package File Name for the Web App Artifacts package to be deployed, along with any other settings necessary for the deployment.
To add a new Parameter to the ARM Template, a small amount of JSON needs to be added that defines the Parameter. At minimum the Parameter needs a Name and a Type. Here’s a list of some of the properties that can be set on Parameters:

  • Name
  • “type” – This defines the data type of the Parameter.
  • “minLength” – This defines the minimum supported length for a Parameter of Type “String.”
  • “metadata” – This allow for additional metadata to be added for the Parameter, such as a short Description of what the Parameter is for.

Here’s the definition of a Parameter named “WebApp_ConnString1”. The other parameters in this file have been truncated to make things easier to read.

{
  "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "hostingPlanName": {...},
    "skuName": {...},
    "skuCapacity": {...},
    "_artifactsLocation": {...},
    "_artifactsLocationSasToken": {...},
    "WebApplication1PackageFolder": {...},
    "WebApplication1PackageFileName": {...},
    "WebApp_ConnString1": {
      "type": "string",
      "minLength": 1,
      "metadata": {
        "description": "ConnString1 Connection String for Web App"
      }
    }
  }
}

Populate Connection String from ARM Template Parameter
The Web App Connection String can be updated to pull the Parameter value once the Parameter has been added to the ARM Template. To do this you need to replace the actual Connection String value in the JSON with an Expression that will be executed at the time of Deployment to set the value equal to the Parameter passed to the ARM Template. The Expression is really simple and basically looks like some JavaScript code that calls the “parameters” method while passing in a String containing the name of the Parameter to get the value of.
The Expression alone is as follows: [parameters(‘WebApp_ConnString1’)]
Here’s the above “ConnString1” Connection String example that’s been modified to include the necessary Expression to pull the Parameter value, rather than being hard coded:

  {
    "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'))]",
      "siteConfig": {
        "connectionStrings": [
          {
            "name": "ConnString1",
            "connectionString": "[parameters('WebApp_ConnString1')]"
          }
        ]
      }
    }
  }

Specify the Parameter Value
There is only one final step after the Parameter has been defined, and the ARM Template has been modified to populate the Connection String value based on the Parameter value. This last step is to set the Parameter in the ARM Template Parameters file. Having all this implemented allows for a specific Connection String to be set for each Environment being deployed to, based on having a different ARM Template Parameters file for each Environment.
Here’s a screenshot of the “WebSite.parameters.json” file created in the previous article in this series with the newly created “WebApp_ConnString1” ARM Template Parameter set:
AzureWebAppConnectionStringParametersFileScreenshot
For additional clarity, here’s the JSON from this file as well:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentParameters.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "hostingPlanName": {
            "value": "WebApp1HostingPlan"
        },
        "WebApplication1PackageFolder": {
            "value": "WebApplication1"
        },
        "WebApplication1PackageFileName": {
            "value": "package.zip"
        },
        "WebApp_ConnString1": {
            "value": "Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;"
        }
    }
}

Web App Connection Strings in the Azure Portal
Once the ARM Template has been deployed, the Connection String will then show up in the Azure Portal under Application Settings for the App Service Web App. Checking the Azure Portal after testing the deployment can be a good idea when first setting up a custom, parameterized Connection String. If the Connection String and correct value is listed within the Azure Portal after deployment, then everything in the ARM Template is configured correctly.
AzureWebAppARMTemplateConnectionStringsScreenshot

What’s Next?

This is the third article in this series on Azure Resource Manager. There are so many customizations and Azure services that can be deployed and managed using ARM Templates. This provides a huge amount of flexibility using Infrastructure as Code to define and automate deployments. One of the next most important configurations for a Web App is Application Setting (appSettings), so the next article in the series will cover how to define Web App Application Settings with an ARM Template.

One thought on “Azure ARM Template: Define Web App Connection Strings

Leave a Reply

Subscribe to the Weekly Blog Digest:

Sign Up

Categories