Building a Simple Azure JSON File

Aidan Finn profile picture
Aidan Finn Petri Contributor

Follow

Aidan Finn, Microsoft Most Valuable Professional (MVP), has been working in IT since 1996. He has worked as a consultant and administrator for the likes of Innofactor Norway, Amdahl DMR, Fujitsu, Barclays and Hypo Real Estate Bank International where...

JSON Hero scaled
In this step-by-step post, I’m going to show you, without using the paid-for version of Visual Studio, how to build a simple JSON file using the Azure Portal, and customize it using the free version of Visual Studio, VS Code.
Before you proceed, you should read the following posts:

 

 

Start in the Azure Portal

I have found that the easiest way to build a JSON that you can start with is to deploy something by hand in the Azure Portal. For this simple example, I’m going to deploy a resource group with a single storage account. Note that the boundary of a JSON deployment in Azure is the resource group:

  • Every JSON deployment is done within a resource group.
  • A JSON deployment cannot span resource groups.
  • You can do multiple deployments to a resource group.

My deployment is as follows:

  • A resource group called rg-petri.
  • A storage account that is in the rg-petri resource group, deployed into the West US region.

The manually deployed resource group and storage account [Image Credit: Aidan Finn]
The manually deployed resource group and storage account [Image Credit: Aidan Finn]
We can see the JSON that can be used to mimic this deployment by clicking Automation Script in the settings of the resource group.
The default JSON of the manual deployment [Image Credit: Aidan Finn]
The default JSON of the manual deployment [Image Credit: Aidan Finn]
You can save this JSON a few ways:

  • Download: Download a zip file that contains the JSON, the parameters from the JSON in their own file, and various command-line and scripted ways to deploy the JSON file.
  • Save to library: A preview feature in Azure allows you to save JSON files in your personal Azure profile.
  • Simple copy and paste: This is my preferred option. I copy the entire JSON, as seen above, and paste it into VS Code so I can customize it.

Customize the JSON in VS Code

I have installed the free VS Code with the Azure Resource Manager Tools (a free plugin) on my Surface Book and use this to edit JSON files; the addition of the plugin provides some handy JSON-awareness.
Launch VS Code and paste the copied JSON template into a new window. Note that VS Code will see this as simple text until you save the file as a JSON file; VS Code will color code the text immediately after the save operation.

The default JSON in VS Code [Image Credit: Aidan Finn]
The default JSON in VS Code [Image Credit: Aidan Finn]
We can see that:

  • There is one parameter: We are only asked to name the storage account.
  • There are no variables: We could store text values as variables instead of listing them in the resources as strings. This would allow centralized editing in the JSON and for easy reuse and change at a later point if we expand the JSON.
  • The storage account resource: The comment for the resource mentions the original subscription ID, which I have blacked out but doesn’t describe the resource, and it’s pretty much locked down to LRS and West US. I want to change resiliency into a parameter, and I want the storage account to be in the same region (location) as the resource group.

Parameters

I am going to rename the “storageAccounts_sapetri_name” parameter to “Storage Account Name”. This means that all references to “storageAccounts_sapetri_name” must be updated to “Storage Account Name”. The default will be null; we cannot have people trying to reuse a default storage account name because storage account names must be globally unique.
I am also going to add a second parameter called Storage Account Resiliency. This parameter will allow the person deploying the template to choose between LRS (Standard_LRS) or GRS (Standard_GRS), with LRS as the default setting. I will end up with the following parameters section:

     "parameters": {
        "Storage Account Name": {
            "defaultValue": null,
            "type": "string"
            },
        "Storage Account Resiliency": {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues":
                [
                "Standard_LRS",
                "Standard_GRS"
                ]
        }    
    },

 

Variables

I want to ensure that my new storage account is deployed into a region that matches the region of the resource group. This can be done using a handy function, as depicted below. I will create a variable that stores the resource group location and can be used by all resources within this template.

    "variables": {
        "RGLocation": "[resourceGroup().location]"
    },

Resources

We have just a single resource in this JSON file – a storage account. The first thing that I want to do is modify the comment, which, by default, lists the subscription, resource group, resource type, and name of the source resource. I want to change it to something that describes the purpose of the resource in the template.
I have defined a parameter called “Storage Account Resiliency” to allow an administrator select the resiliency of the new storage account. The resource has a hard-coded value in a setting called skuname. I will change this to refer to the new parameter.
I’ve also created a variable to define the location of the resource group. I want all resources to use this location, instead of using some hard-coded location – this will allow me to deploy my JSON to any resource group to any Azure region. I will modify the location setting of the resource to use the variable “RGLocation”.

    "resources": [
        {
            "comments": "Will be used to store virtual machines in this resource group",
            "type": "Microsoft.Storage/storageAccounts",
            "sku": {
                "name": "[parameters('Storage Account Resiliency')]",
                "tier": "Standard"
            },
            "kind": "Storage",
            "name": "[parameters('Storage Account Name')]",
            "apiVersion": "2016-01-01",
            "location": "[variables('RGLocation')]",
            "tags": {},
            "properties": {},
            "resources": [],
            "dependsOn": []
        }
    ]

Make sure that you save the JSON file and look for any errors or warnings in the bottom-left of VS Code.

The Entire JSON

When you deploy the finished template, you are asked for two pieces of information:

  • The desired name of the new storage account
  • The resiliency of the new storage account

Deploying the new JSON template [Image Credit: Aidan Finn]
Deploying the new JSON template [Image Credit: Aidan Finn]
I know how annoying it can be to try to assemble code snippets into a single example, so here is the entire JSON file – which I have tested.

{
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "parameters": {
        "Storage Account Name": {
            "defaultValue": null,
            "type": "string"
        },
        "Storage Account Resiliency":
            {
            "type": "string",
            "defaultValue": "Standard_LRS",
            "allowedValues":
                [
                "Standard_LRS",
                "Standard_GRS"
                ]
        }    
    },
    "variables": {
        "RGLocation": "[resourceGroup().location]"
    },
    "resources": [
        {
            "comments": "Will be used to store virtual machines in this resource group",
            "type": "Microsoft.Storage/storageAccounts",
            "sku": {
                "name": "[parameters('Storage Account Resiliency')]",
                "tier": "Standard"
            },
            "kind": "Storage",
            "name": "[parameters('Storage Account Name')]",
            "apiVersion": "2016-01-01",
            "location": "[variables('RGLocation')]",
            "tags": {},
            "properties": {},
            "resources": [],
            "dependsOn": []
        }
    ]
}

This is a very simple example of a JSON. In a soon-to-be-published post, I’ll expand on this JSON by adding more resources.