Last Update: Sep 04, 2024 | Published: Jan 13, 2017
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:
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:
My deployment is as follows:
We can see the JSON that can be used to mimic this deployment by clicking Automation Script in the settings of the resource group.
You can save this JSON a few ways:
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.
We can see that:
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" ] } },
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]" },
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.
When you deploy the finished template, you are asked for two pieces of information:
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.