Azure ARM Templates - Design, validate and deploy
Hi!
Finally we will add a Windows virtual machine. Right click the resources section of the JSON outline and click "Add New Resource". Give it a name, specify the storage account we created above, now choose a subnet we created earlier to attach the vm to during deployment.
The JSON outline is now updated with the new resource, a VM. With this, we have also received some parameters and variables that go with the VM as outlined in the image below. Because a VM cannot function with a Nic, one of those has been added to the deployment as the VM is dependant on it, aswell as a storage account.
Now we need to validate our template before deployment. To do this, go to the solution explorer, right click on the project name, and choose validate.
Login to the Azure account that has access to deploy to an environment. Select the appropriate resource group, if you havn't got one then login to the azure portal and create one quickly. Click Edit parameters.
Parameters must be set for the virtual machine before this template can be deployed. Enter a vm name, vm user name and a password (must meet Azure password complexity requirements).
I chose to save the password in the parameters file but you should NOT do this in production environments for obvious reasons.
Go back to the solution explorer and right click, deploy. Resource groups should be shown in the list, select one to deploy to.
Click Deploy
Back in Azure, we can see the resource's created under our resource group "NorthWindTraders".
As I write this blog post England have just been knocked out of the world cup so if you happen to be French and reading this, congratulations!
In this post I go through the basics of an (ARM) azure resource manager template in Visual Studio and explain the various parts that make up the template. We will then go through the validation of the template so that deployment goes through smoothly and then deploy it to a subscription in Azure.
Before we get started, you may want to get Visual Studio community edition installed or if you can, the full version of VS 2017. Other options are available such as Visual Studio Code. I find VS 2017 to be the best editor which includes intellisense. When writing and editing in JSON, intellisense helps to pick out syntax to prevent you from getting stuck on what to type. It's very difficult to remember all the structure and syntax if you don't write or edit templates that often, intellisense helps a lot here.
The structure of an ARM template
The structure of ARM, sometimes referred to as a JSON template, is made up of 6 different parts. The screenshot below shows the different parts as seen in Visual Studio. These are:
- Schema
- Content version
- Parameters
- Variables
- Resources
- Outputs
Schema
Contains a link to a JSON schema file that describes how the template structure is made up.
Content version
Quite simply, the version of the JSON schema file.
Parameters
The parameters section, is a set of user defined configurations at the time of template deployment. The name of a virtual network or subnet for example, can be defined using these parameters. The vnet and subnets are a great case for ARM templates used to setup brand new environments, as these names will not exist and be different for each deployment. Parameters can be used with expressions to simplify template structure.
Variables
Similar in concept to parameters, variables are settings that exist exclusively in the template and cannot be modified at the time of deployment. Variables are hard coded into the template to make deployment easier. Expressions that are written in the template also make use of variables for quicker writing and editing.
A template that makes use of an existing virtual network or subnet, may have those parameters embedded. For example a corporation that deploys Azure infrastructure for a suite of web servers may hard code the virtual network and subnet into the template with variables. These settings are likely to be the same in all scenarios so using variables here would work well. Variables also ensure clean, error free deployments because mistakes are completely avoided by not giving users the option to make changes.
Use variables whenever you do not want users to configure things, or when a value/name is going to be used more than once in the template.
Resources
Use variables whenever you do not want users to configure things, or when a value/name is going to be used more than once in the template.
The resources section of the template defines what we will use and interact with in this template. These include things like virtual machines, load balancers, virtual networks, SQL databases, network interfaces and storage accounts. We are essentially defining the cloud resources we want to use, whereas parameters and variables are used to define the configurations of those cloud resources using names and values etc...
Outputs
The outputs section of a template is where we can define what happens when a deployment is finished. For example a URL to a website, in the case of a web tier template.
Designing our ARM template
Open visual studio and start a New Project as shown below, give it a suitable name.
The next section displays a list of premade templates from Azure quick start via github. For the purposes of learning, we will select the first option, a blank template. In production environments it is probably far more efficient to start with a template close to what you want.
Next we need to double click on the json file to open it.
The window should now look something like this:
Lets add a virtual network to our template.
Once we have added the vnet to our template, our page is updated and we can start to see the template coming along. You will notice a resource has been added with a set of variables to go with it.
In the below screenshot we define the variable for the vnet prefix (1). Later on in the resource section of the template we are defining configurations for the vnet resource so we recall our variable (2). It is standard practice to use variables wherever they may appear more than once. This is so that if the value changes, one simple edit to the variable is all that is needed to keep the template valid and avoid duplication and unnecessary work.
Next we will add a storage account to store the virtual machine hard disks. Repeat the steps to add a storage account, just like we did with the virtual network and give it a unique name.
Once added, you will see the JSON template has been populated with the storage account 'type' parameter (the type of storage account we want such as LRS, GRS etc) and a resource object.
Finally we will add a Windows virtual machine. Right click the resources section of the JSON outline and click "Add New Resource". Give it a name, specify the storage account we created above, now choose a subnet we created earlier to attach the vm to during deployment.
Now we need to validate our template before deployment. To do this, go to the solution explorer, right click on the project name, and choose validate.
Parameters must be set for the virtual machine before this template can be deployed. Enter a vm name, vm user name and a password (must meet Azure password complexity requirements).
I chose to save the password in the parameters file but you should NOT do this in production environments for obvious reasons.
Click save and VS will analyse the template for errors. At the bottom you are looking for the "Template is valid." output. Once this is done we can now deploy the template safely and error free!
Go back to the solution explorer and right click, deploy. Resource groups should be shown in the list, select one to deploy to.
Voila! Our deployment into Azure is complete. This template can be re-used to deploy a new vnet, storage account, and a virtual machine to make a new environment in any Azure subscription.
Back in Azure, we can see the resource's created under our resource group "NorthWindTraders".
Comments
Post a Comment