This is part 3 of developing with Azure IaaS. In this post we will be wrapping up by creating an Azure Template Deployment in JSON format that provision everything we’ve built previously as a single unit.

In part 1 we learned how to create Azure Virtual Networks and how to protect our resources using network security groups. Part 2 focused on the different ways to provision virtual machines. We used the Azure Portal and PowerShell commands as different ways for provisioning our resources.

Imagine you need to create this environment for several customers or separate subscriptions. There is a lot of manual work and steps involved to complete the environment and things could go wrong. This is where Azure Template Deployment comes in handy. We’ll create a Template in JSON format, save it to source control then deploy it from Visual Studio and pass a file for the parameters. Let's get started…

The source code for this post an be found here

First, an ARM JSON Template has the following sections:

Schema (Required): the endpoint to the schema of this JSON template.

"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",

Content Version (Required): The version number of the content

"contentVersion": "1.0.0.0",

Parameters (Optional): Dynamic values provided at time of deployment

"parameters": {
    "userName": {
            "type": "string",
            "minLength": 1
        },
        "password": {
            "type": "securestring"
        },
     }

Variables (Optional): Static values used at deployment

 "variables": {
    "api-version": "2015-06-15",

    "BackendNSGName": "Backend-nsg",
    "FrontNSGName": "Frontend-nsg",
   }

Resource (Required): An array of resources to be created in Azure

  "resources": [
    {
      "name": "[variables('newvmstorageName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-06-15",
      "dependsOn": [ ],
      "tags": {
        "displayName": "newvmstorage"
      },
      "properties": {
        "accountType": "[parameters('newvmstorageType')]"
      }
    },...

Outputs (Optional): assign values created during the deployment into output variables. These values could be used in your PS script.

A simple template for creating a storage account could look like this:

{
    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "storageName": {
            "type": "string",
            "minLength": 1
        }
     },
  "resources": [
    {
      "name": "[variables('storageName')]",
      "type": "Microsoft.Storage/storageAccounts",
      "location": "[resourceGroup().location]",
      "apiVersion": "2015-06-15",
      "dependsOn": [ ],
      "tags": {
        "displayName": "vmstorage"
      },
      "properties": {
        "accountType": "[parameters(storageName)]"
      }
    }
  ]
   }

For a complete reference of the JSON Template format check the documentation here

Now that we know the basic format of a JSON Template Deployment lets see some methods to author and deploy them:

Author and deploy using Visual Studio Code/2015:

Visual Studio is by far the best tool to author ARM Templates deployment –at the time of this writing. Don’t be worried about installing the full blown Visual Studio; you can use Visual Studio Coe which is a light free source editor version that runs on any platform (Windows, Linux and OS X). You can download visual studio from here

For a step by step tutorial on authoring Template Deployment check this link here

Deploy from GitHub:

GitHub hosts the largest number of open source ARM Templates that will almost cover any use case that you might run into with minor modifications.

Browse to GitHub and search for “Azure QuickStart Templates” or go to this URL: https://github.com/Azure/azure-quickstart-templates

The readme file should cover how to use these for your deployment. The important thing to know is that each template of these has the “Deploy to Azure” button, this means you can click on a button and it will deploy this template with all resources to your Azure account. You will still be given the chance to enter your parameters.

These templates are also supported and can be deployed directly from the Azure portal. I’ll show below how to deploy these same templates from the portal.

Deploy from the Azure Portal

To deploy your template from the Azure Portal:

  1. Click on the “+ New” button on the top left
  2. Type “Template Deployment” in the search field and hit enter
  3. Select the “Template deployment” then click “Create”
  4. A new “Edit Template” blade will open allowing you to create/paste your JSON template, provide values to the parameters and select other options like resource group and subscription then finally you have to agree to the legal terms. enter image description here
  5. Alternatively, you can deploy one of the ready templates on GitHub from the “Edit Template” blade by clicking on the “Quickstart Template” button on the top menu bar

ARM Template github

Create and Deploy from existing Resource Group

Finally, the last method we’ll talk about for creating and deploying our templates is from an existing resource group in Azure. If you followed the first two parts of this post, you can export the template by selecting your resource group and from the Settings blade click on the Export template.

This is a very nice way to capture what you already have built manually into an ARM template. You will have the options to download locally, deploy straight to another Resource Group or even better save this template to your own “Templates” repository by clicking any of the buttons on the top menu.

enter image description here

Conclusion

Microsoft Azure can simplify the process of provisioning resources in a repeatable process with expected outcome every time. You can now create entire environments with virtual machines, network security groups and much more including deploying your application as a single unit with a click of a button.