Skip main navigation

Implementing Azure Resource Manager Templates

.
16.6
Now that we’re clear with all the moving parts that go together to create the template, let’s actually get into a real demo and look at the template in Visual Studio Code, see the IntelliSense support we get, and have a go at deploying the first resource group using PowerShell, and then deploying this template. So I’ll flip right into the demo. So what I’ve done is I’ve assembled all the moving parts together that I showed you in the slide. We’ve got the schema. We’ve got the content version. We’ve got the parameters, the storage account type, the name that we’re taking inputs from the user, the variables, the resources, and the output, all in one place.
53.5
Now, it’s all within Visual Studio Code, and I’ve installed the ARM extension in there as well. The benefit of doing that, of course, is I get IntelliSense support. So, for example, I can come down here and say, in the resource section, I also want to include tags. Not only do I– it gives me IntelliSense support. It does autocompletion for me as well. So I can say the tags for this is Purpose, and the value is Demo.
82.4
And I can add another tag in here to say Delete.
90.1
Like I said, tags, on their own, have no face value, but this is metadata that you’re associating with the resources you’re deploying. It’s just useful for you at a later stage to work out what it was for, or maybe use that as a group by clause. So I could go in into the Azure portal and say show me all the resources that are tagged as deleted, or show me all the resources that are tagged with this charge code so I know what project they belong to. And, again, simple ideas, but just, real world, make a lot of sense. This template has some problems in it, and it’s going to fail the first time.
123.1
The intent is that it fails so I can work through with you to show you how you can identify where the problems are, then fix them. So let’s go into the–
138.3
let’s go right into the Azure ISE. What I’m going to do is I’m going to have to log in into the Azure RM account once again because I restarted my machine. So just go through the process of authenticating again.
162.8
All right. It’s going to blink a little bit to reauthenticate me for all the subscriptions I have access to. Next step, we’re going to set the RM context to a specific subscription, which is the one where I intend to do the demo.
188.3
Just waiting for it to complete. It’s completed now. So, all right, that’s done now. So we’re kind of in the context now. We’re connected to the right subscription. The next command that I’m going to run is the one which actually allows us to deploy a few things.
207.1
So in order for you to deploy anything into Azure, like a storage account, you first need to have a resource group that that storage account needs to go in. Deploying a resource group within Azure is quite straightforward. You would just use the command New-AzureRmResourceGroup, provide a name and a location. And that’s enough, usually, to create a resource group. So let’s just call this resource group iacdemo01-basic-arm-rg. And let’s copy this and take it into the Azure portal to make sure that we– that I don’t already have a resource group with this name. So if you click on this button here, it lists all the resource groups that are available within your subscription. Let’s paste this.
252
And we see we don’t have any resource groups available with this name. So what I’m going to do is I’m going to simply run this command. Select it and run– select this button, because it doesn’t run the whole script. It only runs the selected part. And what it’s doing, or has done, at the speed of light is deployed a new resource group within Azure. So if I come back to the portal and refresh, there we go. My resource group is provided, created, available. What I don’t have here is tags. So if I wanted to create a resource group using tags, then what I could do is add another variable here and use the following syntax.
301.5
And, remember, I said resource groups are idempotent. So if I run the same script again, it’s saying provided resource group already exists. Do you really want to update it? And I can click Yes. And if I didn’t want this prompt, I could just use the Force keyword along with this, in which case it would understand that I just want to force a rerun. Now, it’s not deleted, this resource group, but simply check that the script still matches the state the resource group is in. And it’s found that it just doesn’t have the tags, and it still doesn’t have the tags, so I’m clearly doing something wrong here.
338.5
Oh, what I’m doing wrong is, while I’ve defined the tags here, I haven’t really passed them in the resource group creation task. So let me use the tag, and then just use the parameter that I’ve set up here. So let’s run this again. And see, this time it doesn’t prompt me. It just simply goes and executes the command in the background. Now if I go into the resource group and I click on Tags, we’ve got the Purpose Demo tag in there. As I said, it’s just the semantics of having it, but, over time, you’re going to find it useful. Because if everything was tagged with Demo, it would get listed out here.
373.1
And that means if we wanted to see a slice of cost spend, an opportunity for optimization when the bill comes for Azure usage, then we could do it by tags as well. So, stepping back from here, now that we’ve got a resource group, the next thing, simply, is to deploy the template that we’ve created. So I’ve already got the command here, which is AzureRmResourceGroupDeployment. It’s just asking what you want to call this deployment activity as. So let’s just say IacDemoDeployment. Now, remember, the deployment name is not the same as the resource group name. The deployment name is, as you have a deployment activity, just is a way of identifying that deployment activity.
420.2
When you go in the Azure portal, you can see logs which will tell you what happened as part of that deployment workflow. And if you provide a sensible name here, well, it would just make it easier for you to identify that in the Azure portal. So we’ve got the template file here. What I could use or do from here is also specify some of the optional keywords, such as verbose.
448.1
And in order to do that, I just need to tell it to allow me to use a new line, and then just say verbose. So any output that it generates as part of deployment, it will just have verbose logging on it. So let’s select this and selectively run it. Now, it’s bound to fail, for obvious reasons that I know, and if it passes I’d be surprised.
477.5
So, see, it’s got logic to constantly check the status of the deployment as well.
486.3
So it’s checking the status. It’s checking the status. It’s saying– well, actually, it’s saying that the template is valid and nothing’s wrong with the template. So it’s going to go off and try and deploy this. So if I go into the Azure portal–
504.6
oh, it’s failed. As I said, right, it was going to fail. Now let’s work through how we would identify why it’s failing. So every time something fails as part of the deployment process, you get a correlation ID back from the deployment. So if I scroll down here, it says dot dot da– OK, I think it’s not giving me a correlation ID because I’m already running this in verbose mode. If I was not running it in verbose mode and you don’t tend to run your scripts in verbose mode, then you would get a correlation ID back. And you can use this command, which is called Get-AzureRMLog CorrelationId.
545.7
Basically, what this lets you do, with the DetailedOutput keyword switch, is that it brings back detailed logs of the reason for failure. Now, because we’re running verbose, I assume that it’s already bringing that back. The reason for the failure is easily highlighted here. It says the resource failed because you have a too long name. The storage account name must be between 3 to 24 characters, and it must be lowercase. So let’s come back to the script here, and leverage the out-of-box functions that are available. So I’m going to just use the IntelliSense support to say toLower, put that in parentheses.
587.3
And then the next thing I’m going to do is use the substring functionality to reduce the count from whatever it is down to 2 in the prefix that I’m creating, which should, hopefully, bring down the total length to under 24 characters. So let’s run this again. Probably try running this without verbose.
612.6
It’s going to go through the motions of revalidating the template, making an attempt to deploy it. Only if the template is valid will it actually go ahead and deploy it, but we’ll soon find out. All right. So it seems like it’s just completed now. It’s come back. So me commenting out the verbose had no benefit because the script passed and it didn’t fail. So we wouldn’t know whether the correlation ID would have been returned or not. But take my word for it. It usually would give you a correlation ID if you run it without verbose. Let’s see what happened here. It gives me the results, saying it’s created the storage account. This is the name of the storage account.
652.3
This is the type of the storage account. It’s actually printing out the actual parameter names, the outputs as well. Because, you remember, in our template, we’re saying give me the output of the account– storage account name. And if I flip into the– tab into the Azure portal, you see, within this resource group, I have my storage account provisioned. If I click on the storage account, and within the storage account I click on Tags, I can see the two tags that I pushed through my Azure Resource Manager template. But, hey, that’s your storage account provisioned right there. And idempotent– so if I run the same script again, it will work.
695.3
It will work, and it will run out– it will run very quickly because it has nothing new to provision now. It’s just going to rerun it. So benefits of Azure Resource Manager, we looked at it. We looked at– if I go back to the slides, we looked at a simple ARM template deployment with PowerShell. We went through the process of understanding its structure, the different moving parts. We assembled it into a single template. We then used the IntelliSense support within Visual Studio Code to make some amendments to it. And then, using PowerShell, Azure PowerShell, we were able to create a resource group and deploy the template to provision a storage account for us with the tags.
739.1
Now, this might seem quite laborious, right? You might feel, how am I going to scale my automation if every template I create is going to have so many moving parts? It’s going to be hard. How am I going to learn this language? How am I going to start querying for all the providers that are out there and then create my library? Well, the benefit with Azure is it’s got a massive ecosystem of open source alongside it. So if I take you into the Azure portal– so if I take you into GitHub, and within GitHub, if we search for azure quick start, then you would see that the Azure-QuickStart-Templates, which is a repository provided by Microsoft– it’s quite popular.
782.6
It’s got over a hundred forks on it. This has got over a hundred resource group templates. For every resource type that’s available in Azure, there’s a sample template available here. Say a name. Say– tell me something bleeding edge. Say– I don’t know.
808.5
So there is a template for implementing SharePoint 2013 on high-availability load. There is a template for SQL. There is a template for Azure Stack QuickStart Template that allows you to deploy SQL. So if you just want to take inspiration on how you can structure your templates, or look for templates that you can just pick and deploy, then this library of 100 plus templates is all you need to look at. And that’s thanks to the benefit of the vibrant ecosystem around it.

Now that you have a good understanding of Azure Resource Manager, you are ready to learn how to implement and deploy Azure Resource Manager templates. This step demonstrates how to implement Azure Resource Manager templates using a variety of tools.

Deploying Templates from the Azure Portal

Deploying templates from Marketplace:

From the Azure Portal, you can deploy using a template from the Marketplace:

  1. Select New from the portal, then the type of resource you wish to deploy, and then the version of the resource.
  2. Set the properties and deploy.
  3. Add more resources by using the Add command on the resource group blade.

Deploying templates from Marketplace graphical representation

Deploying Custom Templates:

From the portal, you can also deploy a custom template that defines the infrastructure for your solution.

  1. After your template is created, select New from the portal and search for Template Deployment
  2. Select it from the available resources.
  3. After launching template deployment, open a blank template and in the editor, add the JSON syntax that defines the resources you wish to deploy.
  4. Select Save.

Deploying custom templates graphical representation

Deploying Azure Quickstart Templates:

You can also select a template contributed by the community from the Azure quickstart templates.

  1. To deploy resources from a template already saved to your account, browse your saved templates.
  2. Select the one you wish to work on, and deploy it:

Deploying Azure Quick Start templates graphical representation

Deploying Azure Quick Start templates graphical representation

NOTE: If you have new ideas or templates that you created and would like to share to open source, submit a pull request on GitHub to the Microsoft Repo.

Deploying Templates from the Azure CLI:

Use the following commands to quickly create a resource group, and deploy a template to that resource group from the CLI if both the template and parameter files are local:
azure group create -n examplegroup -l "West US"

**azure group deployment create -f

c:MyTemplatesexample.json -e

c:MyTemplatesexample.params.json -g examplegroup -n exampledeployment
When deploying your resources, specify that the deployment is either an incremental update or a complete update. By default, Azure Resource Manager handles deployments as incremental updates to the resource group.
With the incremental deployment, Resource Manager:
  • Leaves unchanged resources that exist in the resource group but are not specified in the template.
  • Adds resources that are specified in the template but do not exist in the resource group.
  • Does not reprovision resources that exist in the resource group in the same condition defined in the template.
  • Reprovisions existing resources that have updated settings in the template.
With the complete deployment, Resource Manager:
  • Deletes resources that exist in the resource group but are not specified in the template.
  • Adds resources that are specified in the template but do not exist in the resource group.
  • Does not reprovision resources that exist in the resource group in the same condition defined in the template.
  • Reprovisions existing resources that have updated settings in the template.

Deploying Templates with PowerShell:

The following commands create a resource group and deploy a template to that resource group. The template file and parameter file are both local files:
New-AzureRmResourceGroup -Name ExampleResourceGroup -Location "West US"

New-AzureRmResourceGroupDeployment -Name ExampleDeployment -ResourceGroupName ExampleResourceGroup -TemplateFile

c:MyTemplatesexample.json -TemplateParameterFile

c:MyTemplatesexample.params.json
NOTE: There are more options available to you for specifying the resources to deploy. For more options on how to specify the resources to deploy with Azure PowerShell, see Deploy resources with Resource Manager templates and Azure PowerShell.

Deploying Templates with Rest API:

After setting common parameters and headers, validate your deployment by running a validate operation, providing the parameters exactly as you would when executing a deployment.
Next, create a deployment by providing your subscription ID, the name of the resource group, the name of the deployment, and a link to your template. The mode in the following request method is set to Incremental. To run a complete deployment, remember to set the mode to Complete.
PUT https://management.azure.com/subscriptions/<YourSubscriptionId>/resourcegroups/<YourResourceGroupName>/providers/Microsoft.Resources/deployments/<YourDeploymentName>?api-version=2015-01-01

<common headers>

{

"properties": {

"templateLink": {

"uri": "http://mystorageaccount.blob.core.windows.net/templates/template.json",

"contentVersion": "1.0.0.0",

},

"mode": "Incremental",

"parametersLink": {

"uri": "http://mystorageaccount.blob.core.windows.net/templates/parameters.json",

"contentVersion": "1.0.0.0",

}

}

}

GitHub Resource Manager QuickStart Templates

You can leverage GitHub as another resource to help build and deploy solutions on Azure. This repo contains all currently available Azure Resource Manager templates contributed by the community. There is a searchable template index, contribution guide, template best practices, and usage Git tutorial.

Azure Resource Manager Template Visualiser Tool

To map your existing Azure Resource Manager resources visually, you can use the Azure Resource Manager Template Visualiser Tool, an open-source tool that allows you to visualise, customise, and edit your templates from a UI rather than via script.
  1. From the Azure portal, select Resource Groups.
  2. Select the resource group name that you want to export.
  3. Select Export template.

    Exporting Azure Resource Manager Template graphical representation

    You should see a JSON template from the Resource Group.

    JSON template graphical representation

  4. Click Save to file and provide a filename: filename.JSON
  5. Visit the Azure Resource Manager visualiser and import the JSON template to visualise it:

Join the discussion

In the last two steps you’ve learned about the structure of Azure Resource Manager Templates and how to implement and deploy ARM templates. Use the structure provided in the previous section to write an ARM template that you can deploy in your environment. Share your ARM template with the group.
Use the Discussion section below and let us know your thoughts. Try to respond to at least one other post and once you’re happy with your contribution, click the Mark as complete button to move on to the next step.
This article is from the free online

Microsoft Future Ready: DevOps Development, Implementation and Azure Automation

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education