If you need to deploy a VM in Azure these days with PowerShell, you've got a few options. You can either create them via the Service Management (classic) model, or you can create them with Azure Resource Manager (ARM). Microsoft now recommends building all VMs using ARM. If you choose to deploy Azure VMs with ARM JSON templates, though, you're faced with a choice: using templates or not.

Adam Bertram

Adam Bertram is a 20-year IT veteran, Microsoft MVP, blogger, and trainer. Adam is the founder of the e-learning tech screencast platform TechSnips. Catch up on Adam’s articles at adamtheautomator.com, or follow TechSnips on Twitter at @techsnips_io.

In a previous 4sysops article, I showed you quite a bit of PowerShell code to provision an Azure VM. This was how you could spin up a VM without templates. Let's now go into how you can cut those dozens of lines of code down to just one using the New-AzureRmResourceGroupDeployment cmdlet and a few parameters.

Why would you choose to build Azure VMs with templates, anyway?  Well, other than requiring significantly less PowerShell code, you also get to provision Azure VMs in a declarative way. What this means is that you don't have to create your own logic to say "if this VM exists, do this" or "if this storage account does not exist, do this." ARM templates simply allow you to tell Azure "I want this VM with this network, public IP address, storage account, etc.," and it just works, regardless of how many times you run it.

In a nutshell, building Azure VM with ARM templates consists of two rough steps: building a JSON template file and executing the New-AzureRmResourceGroupDeployment cmdlet, which then reads all of the logic inside of this JSON template file to feed the Azure API the parameters it needs to provision a VM.

The hard part is understanding the schema for the JSON template file and figuring out how each component that makes up an Azure VM fits together. We're not going to dive into the nuances of the JSON template file here. However, I am going to give you some pointers on how to build an example VM to get you started down that path if you ever need to build template from scratch.

To get started, I recommend first heading over to the Azure Quickstart Templates page. This will show you lots of example templates which you can open up to see how a template is built. One of the best start templates I found was a template by Corey Sanders where he demonstrates provisioning a simple Windows VM in Azure.

Once you have an example template and have begun learning more about the schema the JSON template files use, you can then go more in depth and either begin tweaking some example templates or even authoring your own.

At this time, the authoring tools are limited, but if you have Visual Studio, it provides some assistance as it understands the template schema. However, since ARM templates are so new, there is no fully-functional editor that allows you to build templates from scratch with a WYSIWYG interface, which would be much easier.

To deploy your first VM, I suggest you download the simple Windows VM template and modify it as you see fit. However, due to the template's schema, you can provide parameters for the template at runtime, so all environment attributes don't have to be hardcoded into the template itself.

To deploy a VM with the simple Windows VM template, you'd first need to decide on the parameter values the template has. This means that you will need to know the storage account to place the VM on, the Windows admin username and password, the unique DNS name for its public IP and the version of Windows to deploy with it. Once you have all of these values, it's simply a matter of specifying them as parameters to the New-AzureRmResourceGroupDeployment cmdlet.

In this example, I am downloading the azuredeploy JSON template from Github, passing into that template file at runtime the parameter values for newStorageAccountName, adminUsername, adminPassword, dnsNameForPublicIp and windowsOSVersion. All the other key attributes of VM creation are already stored in the template itself.

Once you open up a JSON template, you'll see exactly why it takes so much less code to deploy an Azure VM with templates. It's because all of the logic required has shifted from PowerShell code to the JSON. You'll find that just a simple Windows VM will require at least a 140 line template. However, you now have all of this logic bundled up into a single "answer" file, plus you get the declarative nature of templates, which is a win-win.

If you're just getting started deploying Azure VMs with ARM, I suggest you first look at templates. It might be a little harder to understand at first but will pay off in the long run by preventing lots of PowerShell coding and by providing a platform agnostic way of building Azure VMs with JSON.

In my next article I will discuss a simple example that demonstrates how to assign a public IP address with a JSON template.

Join the 4sysops PowerShell group!

1+
Share
0 Comments

Leave a reply

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2019

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account