Deploying virtual machine in Azure using templates

You can also create Azure VMs by using Azure Resource Manager templates. This option relies on the capability to describe an Azure Resource Manager deployment by using an appropriately formatted text file, referred to as an Azure Resource Manager template. Such a text file follows the JSON syntax and includes definitions of all the Azure Resource Manager resources that are part of the deployment. Templates typically contain several parameters, which enable you to customize each deployment, accounting for individual preferences and requirements. Thus, every deployment based on the same template might potentially result in a different outcome, depending on the values of parameters you provide.

The following code is part of a JSON template for the deployment of an Azure Resource Manager virtual machine:

{
“$schema”: “http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#“,
“contentVersion”: “1.0.0.0”,
“parameters”: {
    “newStorageAccountName”: {
        “type”: “string”,
        “metadata”: {
            “Description”: “Unique DNS name for the storage account where the virtual machine’s disks will be placed.”
        }

    },
    “adminUsername”: {
        “type”: “string”,
        “metadata”: {
           “Description”: “User name for the virtual machine.”
        }
    },
    “adminPassword”: {
        “type”: “securestring”,
        “metadata”: {
            “Description”: “Password for the virtual machine.”
        }
    },
    “dnsNameForPublicIP”: {
        “type”: “string”,
        “metadata”: {
              “Description”: “Unique DNS Name for the Public IP used to access the virtual machine.”
        }
    },
    “windowsOSVersion”: {
        “type”: “string”,
        “defaultValue”: “2012-R2-Datacenter”,
        “allowedValues”: [
            “2008-R2-SP1”,
            “2012-Datacenter”,
            “2012-R2-Datacenter”,
            “Windows-Server-Technical-Preview”
        ],
        “metadata”: {
            “Description”: “The Windows version for the virtual machine. This will pick a fully updated image of this given Windows version. Allowed values: 2008-R2-SP1, 2012-Datacenter, 2012-R2-Datacenter, Windows-Server-Technical-Preview.”
        }
    }
},
“variables”: {
    “location”: “West US”,
    “imagePublisher”: “MicrosoftWindowsServer”,
    “imageOffer”: “WindowsServer”,
    “OSDiskName”: “osdiskforwindowssimple”,
    “nicName”: “myVMNic”,
    “addressPrefix”: “10.0.0.0/16”,
    “subnetName”: “Subnet”,
    “subnetPrefix”: “10.0.0.0/24”,
    “storageAccountType”: “Standard_LRS”,
    “publicIPAddressName”: “myPublicIP”,
    “publicIPAddressType”: “Dynamic”,
    “vmStorageAccountContainerName”: “vhds”,
    “vmName”: “MyWindowsVM”,
    “vmSize”: “Standard_D1”,
    “virtualNetworkName”: “MyVNET”,
    “vnetID”: “[resourceId(‘Microsoft.Network/virtualNetworks’,variables(‘virtualNetworkName’))]”,
    “subnetRef”: “[concat(variables(‘vnetID’),’/subnets/’,variables(‘subnetName’))]”
},

Deploying a virtual machine by using a template

To utilize the definitions hosted within a JSON template such as the one above, you must use the New-AzureRMResourceGroupDeployment cmdlet with the –Template switch. The following example creates an Azure Resource Manager resource group named TestRG, and then deploys the template contents into the resource group in the West US region:

$deployName=”DeploymentName”
$RGName=”ResrouceGroup”
$locname=”West US”
$templateURI=”https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-simple-windows-vm/azuredeploy.json
New-AzureRmResourceGroup –Name $RGName –Location $locName
New-AzureRmResourceGroupDeployment -Name $deployName -ResourceGroupName $RGName -TemplateUri $templateURI

You can accomplish the same outcome by running the azure group deployment create Azure CLI command. In either case, you would also need to provide values of parameters specified in the template.

Using Azure PowerShell and Azure CLI requires familiarity with their syntax and installation of their scripting engines. There is a more convenient way of deploying Azure Resource Manager resources, which is the GitHub template repository, where you will find hundreds of ready-to-use templates. Each of the templates published on GitHub has a corresponding Deploy to Azure link. When you click the link, it automatically redirects you to the Azure portal and initiates deployment, prompting you only for values of required parameters. In addition, on the same GitHub page, you will see a Visualize link, which opens the template in Azure Resource Manager Template Visualizer, displaying a diagram showing resources defined in the template, including the relationships between them.

 

Azure Resource Manager templates

When you need to create a consist method where you want to give to the end-user, that is consuming resources on your cloud, the power of creating resource like virtual machines, but you want to control lot of the environment aspects, you should consider using ARM templates.

An Azure Resource Manager template contain JSON formatted definition of one or more Azure resources, along with parameters and variables that facilitate customizing their configuration. When creating, and working with resource templates, you should consider:

  • Which resources you are going to deploy.
  • Where your resources will be located.
  • Which version of the resource provider API you will be use.
  • Whether there are dependencies between resources.
  • When you will specify values of resource properties. While you can include these values in the template, in general, it is preferable to specify them during deployment via corresponding parameters.

Understanding the structure of a resource template

A resource template consists of the following sections:

{
   “$schema”: “http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#“,
   “contentVersion”: “”,
   “parameters”: {  },
   “variables”: {  },
   “resources”: [  ],
   “outputs”: {  }
}

The following table describes the sections in the code sample above.

Element name           Required Description
$schema Yes This is a URL representing the location of the JSON schema file, which describes the template language.
contentVersion Yes This can be any value that helps you keep track of changes to template content.
parameters No Parameters can be provided during deployment either interactively or via a parameter file to customize properties of deployed resources.
variables No Variables are typically used to convert values of parameters to the format that is required to set resource property values.
resources Yes These are resource that you want to create or modify as the result of the deployment.
outputs No These values are returned by the deployment.

The next topic discusses these sections in more detail.

The following code is an example of a complete template that deploys a web app and uses code from a .zip file to provision the app:

{
   “$schema”: “http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#“,
   “contentVersion”: “1.0.0.0”,
   “parameters”: {
     “siteName”: {
       “type”: “string”
     },
     “hostingPlanName”: {
       “type”: “string”
     },
     “hostingPlanSku”: {
       “type”: “string”,
       “allowedValues”: [
         “Free”,
         “Shared”,
         “Basic”,
         “Standard”,
         “Premium”
       ],
       “defaultValue”: “Free”
     }
   },
   “resources”: [
     {
       “apiVersion”: “2014-06-01”,
       “type”: “Microsoft.Web/serverfarms”,
       “name”: “[parameters(‘hostingPlanName’)]”,
       “location”: “[resourceGroup().location]”,
       “properties”: {
         “name”: “[parameters(‘hostingPlanName’)]”,
         “sku”: “[parameters(‘hostingPlanSku’)]”,
         “workerSize”: “0”,
         “numberOfWorkers”: 1
       }
     },
     {
       “apiVersion”: “2014-06-01”,
       “type”: “Microsoft.Web/sites”,
       “name”: “[parameters(‘siteName’)]”,
       “location”: “[resourceGroup().location]”,
       “tags”: {
         “environment”: “test”,
         “team”: “ARM”
       },
       “dependsOn”: [
         “[resourceId(‘Microsoft.Web/serverfarms’, parameters(‘hostingPlanName’))]”
       ],
       “properties”: {
         “name”: “[parameters(‘siteName’)]”,
         “serverFarm”: “[parameters(‘hostingPlanName’)]”
       },
       “resources”: [
         {

           “apiVersion”: “2014-06-01”,

           “type”: “Extensions”,
           “name”: “MSDeploy”,
           “dependsOn”: [
             “[resourceId(‘Microsoft.Web/sites’, parameters(‘siteName’))]”
           ],
           “properties”: {
             “packageUri”: “https://auxmktplceprod.blob.core.windows.net/packages/StarterSite-modified.zip“,
             “dbType”: “None”,
             “connectionString”: “”,
             “setParameters”: {
               “Application Path”: “[parameters(‘siteName’)]”
             }
           }
         }
       ]
     }
   ],
“outputs”: {
“siteUri”: {
“type”: “string”,
“value”: “[concat(‘http://’,reference(resourceId(‘Microsoft.Web/sites‘, parameters(‘siteName’))).hostNames[0])]”
}
}
}