Introduction to Azure Resource Manager
Template Deployment & Advanced Concepts
The course is part of these learning pathsSee 3 more
This demonstration will review an existing Azure Resource Manager (ARM) JSON Template, and break down the individual components that make up the template. Many template examples can be downloaded from the Azure Quickstart Templates repository on GitHub. While some of the templates are quite complex, and incorporate a variety of concepts, many of them also are designed to demonstrate a specific concept, such as a Template Function, or show how to declare a specific type of ARM Resource.
Let's take a look at some of the tools that help us to author Azure Resource Manager JSON Templates.
First, we have the Azure Quickstart Templates Repository, which is publicly available on GitHub. If you go to github.com/ azure/azure-quickstart-templates, you can access all of the templates that are available inside of this repository. Now you can optionally search this Git repository just by using your browser's search function so if we search for something like VM, we can highlight all of the Azure templates that have VMs inside of them. You can also use the GitHub Search option at the very top of the webpage or if you prefer, you can scroll down to the Readme file where they provide a link to a template index. So if we follow that link to Azure.Microsoft.com, we're presented with a much nicer looking page where we can search for a template such as simple VM. So when we search for simple VM, you can see that we get back a variety of templates here, and we can then deploy those directly to Azure straight from here. If we go back to the GitHub Project page in our browser, and let's open up one of the sample templates here. So let's search for simple. And as you can see, there's a template here called 101-simple-windows-vm. If we click on that folder inside the Git repository, you'll see a few different files.
First of all, there's the README.markdown file that provides us some simple feedback about what this template does. We have a metadata JSON file that has a little bit of metadata that describes the GitHub Azure template. We also have the Azuredeploy.json file, which is the actual template itself. Now, once we open the template, we can click on Raw, and that is going to literally give us just the plain text of the template itself. So what we could do is do a Command or Ctrl-A to select that, and then copy it to our clipboard and paste it into our editor, which we will do in a moment. But there's one more file that I want to point out first. If we go back to that simple-windows-vm folder, we have the azuredeploy.parameters.json file. Now with the parameters for a deployment can be specified in several different ways. One method of doing that is to provide an actual JSON file that declares the parameter values that will be passed into the template file itself. You can also specify parameters inline using the Azure PowerShell module or using the Azure cross-platform CLI tool. We will look at that during the deployment section. So for the time being, let's go ahead and go back to this Raw view of this ARM JSON template. Let's do a Command A, Command C to copy and then we'll go over to our editor.
So as you can see here, I have Visual Studio Code running, and Visual Studio Code has a JSON file that I pre-saved and all that's inside of it is a schema. So the schema actually comes from the template, and the schema allows the editor to understand all of the different IntelliSense items that can be used to auto-complete values inside of the JSON template. The schema is also used to validate the consistency of the template prior to deployment. So if we were authoring a JSON template from scratch, what we could do is just put a comma here and start specifying the other sections. If you remember from our slide, we have a few different sections such as parameters, resources and variables. If you want to output values from your template, you can also use the outputs area and that defines parameters that will be emitted from the JSON template. So as you can see, inside of the Visual Studio Code editor, we actually get this rich IntelliSense that helps us to author our JSON templates for Azure Resource Manager.
So let's go ahead and specify an input parameter here. Let's call it username. And then the parameter will be an object and using the IntelliSense, we can see all the different properties that we can specify for that particular parameter. For example, we can specify the type of the parameter and in this case, the username will be a string. However, we can also specify other types such as an integer, a Boolean, a JSON object or a JSON array. So for the time being, we're just going to use string for that. We can also specify other properties such as the minimum or maximum length for the string and we can also specify metadata that provides feedback to the user of our template to help them understand the purpose behind this particular parameter. So in here what we can do is we can add a description metadata, and we will say this username is used to specify the administrative user for the Windows VM. And then presuming that we are actually deploying a virtual machine inside of our Azure Resource Manager template, we could reference this username parameter inside of our resources section, and specify the username for the virtual machine dynamically. Inside the Resources section, we specify an array of resources. As you can see, because the schema up here is taking effect, we are basically being prompted by the Visual Studio Code editor to say that we don't have enough items inside the Resources section of our Azure Resource Manager JSON template. So if we use the F8 key on our keyboard to cycle through all the different error messages, we can see that this resource's array has too few items. It expects there to be at least one or more items in the resources array.
Let's go ahead and define a resource. So a resource is an object, and if we use the Ctrl-Space shortcut on our keyboard to prompt for IntelliSense inside of Visual Studio Code, you'll see that we're prompted for all the different properties that make up a resource. Now a resource could be anything such as a virtual machine, or a storage account or a virtual network. And there are certain generic properties that must exist for all resources. So those properties are going to be the type. So we are going to specify the type of resource and if we hit Ctrl-Space, you'll see that we are prompted for all the resource providers and all the individual resource types that we can provision inside of our Azure Resource Manager template. So in this case, let's go ahead and specify that we are going to deploy a Redis Cache. Another one of the generic parameters that we have to specify for our resources is called the API version. So any time that we provision a resource we have to specify which API version we want to use to deploy that resource. Now you'll also see that we get IntelliSense here coming from the JSON schema. So let's go ahead and use the latest API version, which is 2015-08- 01. Each resource in resource manager also has a location. So the location is the Azure region where we want to deploy that resource. Now you could define a variable that specifies the location and then simply use that variable on all of your resources to deploy them to the same location rather than having to repeat it each time.
So in this case we're going to specify West US and you can see that we get IntelliSense for that, but we're actually going to take that value, just this West US here. We're going to cut that and we're going to create a variable called location, and we're going to put the West US value into that variable. So now that we've declared a JSON variable, we are going to go back to our location and we're going to use one of the Azure Resource Manager Template functions to utilize that variable. So the template function name that we want is called variables, and then inside of the parameters to that template function, we're going to specify the name of the variable that we want to populate into this particular JSON property. So that's how we define a variable, and then utilize it inside of a resource. But as you can see, it's still complaining that we're missing a couple of properties. So we also need to specify a name property and for Redis Cache, the name property is going to determine the name of that cache in our subscription. So let's go ahead and just call this Cloud Academy, and you'll see that it's still complaining. We still have more errors that we need to resolve. We still have to specify properties. So the last generic property that we have to specify in a resource is called properties. And this properties JSON property is actually very important, because it specifies all of the parameters that make up this Redis Cache resource. So as you will see, we'll go ahead and define a new property here. And we actually get back an array of properties that we can specify on the Redis Cache.
So in this case, we're not going to go ahead and fill this out for the time being, but the one final thing that I wanted to show you in our template here is that we're missing a property called Content Version. So just as each resource has its own set of generic properties that apply to all resources, the Azure Resource Manager Template also has a set of generic properties that apply to all Azure Resource Manager templates. We've already gone over most of those. We have the schema. We have parameters. We have resources, variables and outputs, but there's one more that's actually required in order for us to have a formalized JSON template and that is called the Content Version. So the content version allows us to specify a version for our template so that as it changes over time, we can increment that version and ensure that we're only deploying the most current version of our Azure Resource Manager template.
About the Author
Trevor Sullivan is a Microsoft MVP for Windows PowerShell, and enjoys working with cloud and automation technologies. As a strong, vocal veteran of the Microsoft-centric IT field since 2004, Trevor has developed open source projects, provided significant amounts of product feedback, authored a large variety of training resources, and presented at IT functions including worldwide user groups and conferences.