Deconstructing JSON: Networking 101

This leads me to really needing to deconstruct a JSON template so I can really understand what’s going on.  I think it’s great that we can use the Azure Quickstart Templates as resources, as well as export some templates directly from Portal configurations, but I need to break it down to really feel like I understand what I’m working with.

To relate this to real life, I started to map out a simple, yet ambitious improvement to my Imperfect Lab (the subject of prior posts).  Still, this proved to be too ambitious for my “getting started with JSON” goal, so I simplified it.

Within Azure, ARM templates are awesome for doing the equivalent of “racking and stacking” in the real world.  So I went small… let’s get a network deployed.  A small network; the physical equivalent of a switch with a couple VLANs. Let the JSON “unboxing” begin!

A basic JSON template has this format:

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

The sections for $schema and resources are required.  Parameters, variables and outputs are optional, depending on your needs.   So let’s check out the resources I’m using and take a few notes:

  1.  The contents of the resources section are flanked with [square brackets].
  2. All resources need to include the apiVersion, type and name.  All the other details vary based on resource.
  3. All these resources in my sample use parameters to determine the properties, not variables. This means they can all be overridden with an additional “azuredeploy.parameters.json” file.
 "resources": [
    {
      "apiVersion": "2015-06-15",
      "type": "Microsoft.Network/virtualNetworks",
      "name": "[parameters('vnetName')]",
      "location": "[parameters('location')]",
      "properties": {
        "addressSpace": {
          "addressPrefixes": [
            "[parameters('vnetAddressPrefix')]"
          ]
        },
        "subnets": [
          {
            "name": "[parameters('subnet1Name')]",
            "properties": {
              "addressPrefix": "[parameters('subnet1Prefix')]"
            }
          },
          {
            "name": "[parameters('subnet2Name')]",
            "properties": {
              "addressPrefix": "[parameters('subnet2Prefix')]"
            }
          },
          {
            "name": "[parameters('subnet3Name')]",
            "properties": {
              "addressPrefix": "[parameters('subnet3Prefix')]"
            }
          }
        ]

As you can see, this resources deploys a single virtual network with three subnets.  Because there are no hard coded names and IP addresses, I use the parameters section to set those. (In the sample below, I’ve only shown the parameters for subnet 1, but the parameters for the other two subnets would be similar.)

  1.  The “defaultValue” will allow the deployment to be repeatable, but can be overridden with values provided in a separate parameters file.
  2. The “allowedValues” section used in the location parameter is an optional example.  This would drive selections from the drop down menus using the Azure portal or limit the possibilities that can be passed from a parameters file.
   "parameters": {
    "location": {
      "type": "string",
      "defaultValue": "West US",
      "allowedValues": [
          "West US",
          "East US"
       ],
      "metadata": {
        "description": "Deployment location"
      }
    },
    "vnetName": {
      "type": "string",
      "defaultValue": "SmallNet",
      "metadata": {
        "description": "VNet name"
      }
    },
    "vnetAddressPrefix": {
      "type": "string",
      "defaultValue": "192.168.0.0/16",
      "metadata": {
        "description": "Address prefix"
      }
    },
    "subnet1Prefix": {
      "type": "string",
      "defaultValue": "192.168.20.0/24",
      "metadata": {
        "description": "Subnet 1 Prefix"
      }
    },
    "subnet1Name": {
      "type": "string",
      "defaultValue": "Subnet20",
      "metadata": {
        "description": "Subnet 1 Name"
      }

As none of my resources require variables or outputs, I won’t need those section in my file.  The final version of the file is here.

Now to actually deploy this template, you have a few options:

  1. Copy and paste the code into a blank “Template Deployment” in the Azure Portal.  You’ll be shown the blades for the parameters to edit or accept the default values.

  2. Deploy the template using PowerShell either from a local file or a web location.  Not the different parameters used to determine the location and replace the placeholders with the appropriate paths.

    For a web location source (like Github) use:

       New-AzureRmResourceGroupDeployment -ResourceGroupName 'BigLab' -TemplateURI <templateFileURI> -TemplateParameterURI <parameterFileURI> -Verbose
    

     

    For a local source (on your pc) use:

     New-AzureRmResourceGroupDeployment -ResourceGroupName 'BigLab' -TemplateFile <$templateFileURI> -TemplateParameterFile <parameterFileURI> -Verbo
    

Once this template deploys, which only takes a few moments, you’ll be able to review the results in the Azure Portal.