Create an Azure Resource Manager (ARM) template that can provision the web application, SQL database, and deployment slots in a single automated processing

This blog post shows, how to define an Azure Resource Manager template that can deploy application into the Microsoft Azure cloud using Platform-as-a-Service technology for web application and SQL database.

We are going to create three Azure environments (dev, test, production), each consisting of the following resources:

  • App Service
    • Web App
      • Auto-scale rule
    • Deployment slots (for zero-downtime deployments)
    • SQL Server
      • SQL Database
    • Application Insights
    • Since this solution is based on Azure Platform-as-a-Service (PaaS) technology, it should take advantage of that platform by utilizing automatic scale for the web app and the SQL Database PaaS service instead of SQL Server virtual machines.

    • Create an Azure Resource Manager (ARM) template using Visual Studio

    • Open Visual Studio and create a new project of the type Cloud — Azure Resource Group. Name the new project “GITS.AzureResourceTemplate” and save it to C: Also, make sure that only the “Create directory for solution” option is checked on the lower right, as in the screen shot below. When finished, click OK.

    • On the next window, click Blank Template, and click OK.

    • In the Solution Explorer window, open the azuredeploy.json file by double-clicking it.

    • Then, on the left side of the Visual Studio window, open the window called JSON Outline.

    • Save your files.

    • Add an Azure SQL database and server to the template

    • Right-click on the resources item in the JSON Outline and click Add New Resource.

    • Select SQL Server and give it a name like “gitssqlserver”, then click Add.

    • Now that the SQL Server has been created as a resource, right-click that SQL Server resource and choose Add New Resource so that you can add a database.

    • Choose SQL Database, and call it “GITSData” Make sure that your server is selected in the drop-down list below, and click Add.

    • Add a web hosting plan and web app to the template

    • Add another resource, this time choose App Service Plan, and call it “GITSHostingPlan”, followed by clicking Add.

    • Right-click the hosting plan resource and add a new resource underneath it

    • Choose Web App, name it “GITSWeb”, make sure your hosting plan is selected in the drop-down list, and then click Add.

    • Add Application Insights to the template

    • Add a new resource to the template, this time choose Application Insights for Web Apps. Make sure your correct hosting plan and web app are selected in the boxes. Name the Application Insights resource “GITSWeb” and then click Add.

    • Next, you need to add the Application Insights extension to the App Service so that it will be running automatically once the site is deployed. This is going to require some manual code because there is not a wizard for this resource type. Click on the GITSWeb web app resource to locate its JSON code. Then, just below the “properties” property, paste or type in this block of JSON code.

    • “resources”: [

    • {

    • “apiVersion”: “2015-08-01”,

    • “name”: “Microsoft.ApplicationInsights.AzureWebSites”,

    • “type”: “siteextensions”,

    • “tags”: {

    • “displayName”: “Application Insights Extension”

    • },

    • “dependsOn”: [

    • “[resourceId(‘Microsoft.Web/Sites/’, variables(‘GITSWebName’))]”,

    • “[resourceId(‘Microsoft.Insights/components/’, ‘GITSWeb’)]”

    • ],

    • “properties”: {

    • }

    • }

    • ]

    • It will look something like this screen shot:

    • Configure automatic scale for the web app in the template

    • Click on the resource called “GITSWeb AutoScale” to see its JSON value.

    • In the main window, scroll down a little to find the “enabled” property of the auto scale rule. Change it from “false” to “true.” You can examine the other settings in this JSON value to understand the setting. It defaults to increasing the instance count if the CPU goes above 80% for a while and reduces the instance count if the CPU falls below 60% for a while.

    • Configure the list of release environments parameters

    • Next, you need to configure the list of release environments we’ll be deploying to. Our scenario calls for adding three environments: dev, test, and production. This is going to require some manual code because there is not a wizard for this resource type.

    • “environment”: {

    • “type”: “string”,

    • “defaultValue”: “dev”,

    • “allowedValues”: [

    • “dev”,

    • “test”,

    • “production”

    • ]

    • },

    • Click on the parameters item in the JSON Outline window to locate its JSON code. Then, add this code as the first element inside the of the “parameters” object.

    • After adding the code, it will look like this

    • Configure the name of the web app using the environments parameters

    • Next, you need to configure the template so that it dynamically generates the name of the web application based on the environment it is being deployed to. Click on the variables item in the JSON Outline window to locate its JSON code.

    • “GITSWebName”: “[concat(‘GITSWeb’, ‘-‘, parameters(‘environment’), ‘-‘, uniqueString(resourceGroup().id))]”},

    • Then, location the “variables” section and replace the corresponding GITSWebName value with the following code:

    • After adding the code, it will look like this:

    • Add a deployment slot for the “staging” version of the site

    • Next, you need to add the “staging” deployment slot to the web app. This is used during a deployment to stage the new version of the web app. This is going to require some manual code because there is not a wizard for this resource type

    • {

    • “apiVersion”: “2015-08-01”,

    • “name”: “staging”,

    • “type”: “slots”,

    • “tags”: {

    • “displayName”: “Deployment Slot: staging”

    • },

    • “location”: “[resourceGroup().location]”,

    • “dependsOn”: [

    • “[resourceId(‘Microsoft.Web/Sites/’, variables(‘GITSWebName’))]”

    • ],

    • “properties”: {

    • },

    • “resources”: []

    • }

    • Click on the GITSWeb web app resource to locate its JSON code. Then, add this code to the “resources” array, just below the element for the application insights extension.

    • It will look something like this screen shot:

    • Create the dev environment and deploy the template to Azure

    • First, before you deploy the template, you need to make sure that you can get the instrumentation key from the Application Insights resource because you will need it later. To do this, you can add an output property to the template. Go the “outputs” area of the template and paste or type in this JSON code.

    •     MyAppInsightsInstrumentationKey”: {

    • “value”: “[reference(resourceId(‘Microsoft.Insights/components’, ‘GITSWeb’), ‘2014-04-01’).InstrumentationKey]”,

    • “type”: “string”

    • }

    • Now, save all your files.

    • Right-click the project in Solution Explorer and choose “Deploy” and then “New…”

    • Sign in to your Azure account if necessary, and then choose your correct subscription. Under Resource group, choose “Create New…” and create a new resource group for this deployment.

    • Since we are creating a dev environment, let us name it “Gits-dev.” Choose a location near you and Click Create

    •  

    • In the next window, select “dev” from the list of environments. Then, pick an admin username, and password for the database, it does not matter what you choose. Then use “GITSData” for the GITSDataName value. Call the hosting plan “GITSHostingPlan1” and choose “S1” for the Sku. Finally, be sure to check the “Save passwords…” option at the bottom See this screen shot for help. When finished, click Save

    • .

    • Click the Deploy button on the deployment window.

    • If we have done everything correct, the deployment will begin. You can watch the output window inside Visual Studio to follow along. This deployment typically takes a few minutes. Upon completion, you should see success.

    • Create the test environment and deploy the template to Azure

    • The following steps are very similar to what was done in the previous task with the exception that you are now creating the test environment

    • Right-click the project in Solution Explorer and choose “Deploy” and then “New…”

    • Sign in to your Azure account if necessary, and then choose your correct subscription. Under Resource group, choose “Create New…” and create a new resource group for this deployment.

    • Since we are creating a test environment, let us name it “Gits-test.” Choose a location near you.

    • In the next window, select “test” from the list of environments. Then, pick an admin username, and password for the database, it does not matter what you choose. Then use “GitsData” for the GitsDataName value. Call the hosting plan “GitsHostingPlan1” and choose “S1” for the Sku. Finally, be sure to check the “Save passwords…” option at the bottom See this screen shot for help.When finished, click Save.

    • Then, click the Deploy button on the deployment window.

    • Create the production environment and deploy the template to Azure

    • The following steps are very similar to what was done in the previous task with the exception that you are now creating the production environment.

    • Right-click the project in Solution Explorer and choose “Deploy” and then “New…”

    • Sign in to your Azure account if necessary, and then choose your correct subscription. Under Resource group, choose “Create New…” and create a new resource group for this deployment.

    • Since we are creating a production environment, let us name it “Gits-prod.” Choose a location near you and Click Create.

    •  

    •  

    • Once you have the resource group created, click the Edit Parameters button.

    • In the next window, select “production” from the list of environments. Then, pick an admin username, and password for the database, it does not matter what you choose. Then use “GitsData” for the GitsDataName value. Call the hosting plan “GitsHostingPlan1” and choose “S1” for the Sku. Finally, be sure to check the “Save passwords…” option at the bottom See this screen shot for help. When finished, click Save.

    • Then, click the Deploy button on the deployment window.

    • If you visit the Azure Portal for your Azure subscription, you should now see the three newly created resource groups.

  • Leave a Reply

    Fill in your details below or click an icon to log in:

    WordPress.com Logo

    You are commenting using your WordPress.com account. Log Out /  Change )

    Google photo

    You are commenting using your Google account. Log Out /  Change )

    Twitter picture

    You are commenting using your Twitter account. Log Out /  Change )

    Facebook photo

    You are commenting using your Facebook account. Log Out /  Change )

    Connecting to %s