Getting Started with ARM Templates


Microsoft Azure

By now you've probably heard of the term Infrastructure as Code (IaC). But what is it and how can we implement this approach in Azure?

Infrastructure as code is the process of managing your infrastructure in the same way that you manage your application code. So rather than manually configuring resources and deployments through a series of points and clicks, IaC defines the resources and their configuration as code in template files. This approach has several benefits, not least the ability to re-use templates. For example, you could create a template for a web application. You could use the same template to deploy into separate development, QA and Production environments, thus ensuring consistency between the 3 environments and saving someone a lot of pointing and clicking!

Azure Resource Manager (ARM) templates give us the ability to implement Infrastructure as Code within an Azure environment. ARM templates are JSON files that define resources (e.g. Virtual Machines, Virtual Networks, Storage Accounts, etc.) and their configurations. 

At first glance ARM templates can seem daunting, a simple windows VM deployment template can consist of around 180 lines of code. Some people may look at this and be put off, particularly if they haven't got a background in scripting or coding; but it's worth investing the time in learning how to write the templates, it's going to save you a lot of time and effort in the long run and is key automating the deployment of workloads. In this post I'll aim to take you through the basic structure of a template and some resources you can use to help write your own.

 ARM templates themselves are made up of the following elements:

{

    "$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",

    "contentVersion": "",

    "parameters": {  },

    "variables": {  },

    "functions": [  ],

    "resources": [  ],

    "outputs": {  }

}

When you're starting out, the key elements to understand are parameters, variables, resources and outputs. Parameters are values that are provided by the user when they deploy the template. For example, if you were deploying a VM you might specify parameters for Username and Password so the user could customise these for each deployment. Variables are values the used further on in the template to simplify them. You might define these by concatenating parameters you had specified. The resource section of the template is where the resource deployment itself is defined, it will take the values from your parameters and variables and use them as part of the deployment into Azure. Outputs are values that are returned post deployment, you may for example return the public IP address of a Virtual Machine so you can connect to it. 

You may want to dive straight in and start writing your own but I found the easiest way to get started was to look through some existing templates, examine their structure and then deploy them into a test environment. Luckily Microsoft provide 100's of templates for you to peruse. If you head over to Azure Quickstart Templates GitHub repository you will find a raft of templates that have already been created. Looking through just a few of these will get you more familiar with the structure of a template, there's even the option to deploy straight to Azure, this will pass you through the portal and take you through the deployment process (note you will be charged for any resources created so make sure you are aware of the costs).

You can view the templates in their raw format on the GitHub site, but they can be difficult to read with no syntax colouring. I would recommend downloading a code editor, if you don't already have a preference I would recommend Visual Studio Code. It's free, open source and multi-platform. What's more you can install useful extensions that makes writing ARM templates much easier; I would recommend installing the Azure Account, Azure Resource Manager Tools and JSON tools extensions. You can also install Azure Resource Manager Snippets; snippets allow you to type shortcuts that will insert pre-configured fragments of ARM template code. For example, typing arm! will insert the skeleton arm template outlined above.

Another useful function is provided from the Azure portal itself. During the process of creating a new resource you will notice a "download a template for automation" link at the bottom of the blade. If you click the link you will be presented with an ARM template for the deployment. One thing to note with this is you will see a lot of parameters, you will want to remove some of the unnecessary ones and change some of them to variables.

 image 1-1

 

Image 2

Finally, if you want to learn more, then I would highly recommend following this hands-on, self-paced course on the Azure Citadel site. You'll need an Azure subscription to deploy into but the course material itself is free. It will start with the creation of a basic template before moving onto more complex concepts like conditions, copies, and nested templates.

Posted by Dale Marshall