Azure Resource Manager Template – JSON
This topic will be around what is JSON template and how it is going to make your deployment easy and industry standard. Resource Manager template is a JSON file that enables quickly deploying infrastructure as code to Azure. Infrastructure as code means treating your network, server, and application as code. Rather than deploying the network and waiting for the outcome and then deploying the server and the same for the application, program it with a JSON template. In the template, we have to fill up all the required details such as resource group, storage account, VM size, and location and it will deploy the resources for you. The same template can be used to deploy the solution to the other organization by simply changing the value with minimal effort– either a few mouse clicks or running a small PowerShell script. Let the template deploy solution for you and you do something else.
To start with templates, you need a JSON editor. Visual Studio Code is a lightweight, open-source, cross-platform code editor. It supports creating and editing Resource Manager templates.
Install Visual Studio Code and add Azure extension
- Install Visual Studio Code from https://code.visualstudio.com/.
- To install the Azure Resource Manager Tools open VS Code and click on Extensions Pane, and search for the extension.
- Restart VS Code to enable the extension.
Start with Template
First of all start with a blank template that includes only the basic sections of a template then we will add the details.
- Create a new file.
- Copy and paste the following JSON syntax into the newly created file:
{
"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": { },
"variables": { },
"resources": [ ],
"outputs": { }
}
- Save the file as AzureRMTemplate.json.
Now, lets understand the syntax of the JSON template. It has four section Parameters, Variables, Resources and Outputs. Let’s understand each of them.
Parameters
While deploying anything via the Azure Portal, you are asked a bunch of questions such as VM name, user name, password, and network address as per the customer requirements. we use parameters to ask for configuration data to customize the deployment. The below simple example shows how we can ask for a username and password for one or more virtual machines that a template will deploy.
When a template with the above details will be deployed, two fields (ServerAdminName and ServerPassword) will appear and request data entry to configure the deployment.
Understand the {} delimited definition of ServerAdminName is followed by a comma; this comma designates that another parameter will follow. The last parameter will not have a trailing comma.
we can also supply a default value for the parameters that can be overwritten at the time of deployment.
Variables
Variable is used to store values rather than hard-coding these values in the Resources section to reuse value.
Resources
This is the section of the template where you will do most of the work. Here we describe storage accounts, networks, NICs, virtual machines, load balancers, and so forth. Parameters and variables are used to customize each deployment. Each type of resource is described using the Microsoft Azure JSON schema.
Outputs
In this section, you can define outputs from your template. These values can be for example a connection string from a deployment of a database. This can then be passed into another deployment to use for example as a connection string for a website you are going to deploy.
There is a repository of community templates on GitHub, download a template and try to understand all the sections in more detail before writing your first JSON template.
I hope now you are ready to deploy the first resource using a template. In the next post, we will deploy a storage account using a template.