How to use JSON code for creating Azure Infrastructure

Let me try to walk you through your first successful deployment of infrastructure in Azure using JSON template code.

We will be using PowerShell ISE (builtin to Windows) and Visual Studio Code (download here).

First of all, open PowerShell ISE and login to your Azure Subscription and create a Resource Group:

#Login to AzureRM.
Login-AzureRmAccount
#See which Azure RM Subscription your currently managin.
Get-AzureRmContext
#Get a view of all Azure RM Subscriptions your account is administrating.
Get-AzureRmSubscription
#Select a subscription by copying the SubscriptionName.
Get-AzureRmSubscription –SubscriptionName "MSDN-plattformer" | Select-AzureRmSubscription
#Datacenter location
$locName = "northeurope"
#Resource Group Name
$rgName = "Demo"
New-AzureRmResourceGroup -Name $rgName -Location $locName

Lets say you have an empty environment, and uses this as your templates to start a demo environment whenever needed. The PowerShell command to invoke the action in our code is:

$rgName = "Demo"
$deployName="DEMO"
$templatePath = "C:\Users\Roy\OneDrive\Azure\ADDS\VirtualMachineTemplate1.json"
$parameterFile = "C:\Users\Roy\OneDrive\Azure\ADDS\Parameters1.json"

New-AzureRmResourceGroupDeployment -ResourceGroupName $rgName -TemplateFile $templatePath -TemplateParameterFile $parameterFile

Examples of the templatefile and parameterfile can found in my little Github repository. But what is this templatefile and parameterfile? What should you do with them?

These are the files I edit using Visual Studio Code. The template file holds all the basic information on how to create your infrastructure, but it will create it without any names or speifications. It would be like installing your infrastructure using round robin, which would be fun, but probably useless. So at the beginning of the template, there is a section called “parameters”, which collects information from your parameterfile.

{
  "$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "newStorageAccountName": {
      "type": "string",
      "metadata": {
        "Description": "The name of the storage account where the VM disk is stored."
      }
    },
    "adminUsername": {
      "type": "string",
      "metadata": {
        "Description": "The name of the administrator account on the VM."
      }
    },
    "adminPassword": {
      "type": "securestring",
      "metadata": {
        "Description": "The administrator account password on the VM."
      }
    },
    "dnsNameForPublicIP": {
      "type": "string",
      "metadata": {
        "Description": "The name of the public IP address used to access the VM."
      }
    }
  }

Below the “parameter” section, we have the “variables” section. This is also information gathered for the template to create whatever you would like the code to create.

 "variables": {
    "location": "North Europe",
    "imagePublisher": "MicrosoftWindowsServer",
    "imageOffer": "WindowsServer",
    "windowsOSVersion": "Windows-Server-Technical-Preview",
    "OSDiskName": "DEMO-DC01-OSdisk01",
    "nicName": "DEMO-DC01-NIC01",
    "addressPrefix": "10.0.0.0/16",
    "subnetName": "DEMO-Backend",
    "subnetPrefix": "10.0.0.0/24",
    "storageAccountType": "Standard_GRS",
    "publicIPAddressName": "DEMO-DC01-PIP01",
    "publicIPAddressType": "Dynamic",
    "vmStorageAccountContainerName": "vhds",
    "vmName": "DEMO-DC01",
    "vmSize": "Standard_F1",
    "virtualNetworkName": "DEMO-VN01",
    "vnetID": "[resourceId('Microsoft.Network/virtualNetworks',variables('virtualNetworkName'))]",
    "subnetRef": "[concat(variables('vnetID'),'/subnets/',variables('subnetName'))]"
  }

The paramterfile will looke like this, giving information about the storageaccountname, adminusername, adminpassword and dns name for the public IP.

{
  "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "newStorageAccountName": { "value": "itiscloudydemo" },
    "adminUserName": { "value": "demo" },
    "adminPassword": { "value": "MyPassword" },
    "dnsNameForPublicIP": { "value": "itiscloudydemo" }
  }
}

The parameters should hold the customization of your deployment, but the variables could also have customization. As we see in these examples, I could move all variables that are custom for each VM into the parameters and change the parameterfile in the deployment, when I deployed the different type of VM.

If you move a value from variable to parameter, we need to add the parameter to the parameter section of the templatefile. Copy this part and paste it at the bottom of the parameters section.

    },
    "location": {
      "type": "string",
      "metadata": {
        "Description": "The name of the location used to host your VM."
      }

Leaving the section with two “} }.” brackets under each other before the variables start.

        "Description": "The name of the location used to host your VM."
      }
    }
  },
  "variables": {

Then go through the templatefile and locate everywhere it uses “location” and change from “[variables(‘location’)]” to “[parameters(‘location’)]”.

{
      "apiVersion": "2016-03-30",
      "type": "Microsoft.Network/publicIPAddresses",
      "name": "[variables('publicIPAddressName')]",
      "location": "[parameters('location')]",
      "properties": {
        "publicIPAllocationMethod": "[variables('publicIPAddressType')]",
        "dnsSettings": {
          "domainNameLabel": "[parameters('dnsNameForPublicIP')]"
        }
      }
    },

Now it will use the parameter to locate the value, rather then looking for a variable for the value.

After you have modified the variables and parameters, run the deployment and see that your infrastructure is created.

My next post will cover my best use of this templatefile and parameterfile, depending on the purpose, but main is often to create a demo environment.


Leave a Reply

Ehlo!

I am Roy Apalnes, a Microsoft Cloud Evangelist working av Sopra Steria. Main focus in Microsoft Security and Endpoint Management, with a bigger picture in mind.

Featured Posts

    %d bloggers like this: