In a previous post I introduced you to the Azure Marketplace where you can deploy solutions or publish your own for free for for a fee. The Azure MarketPlace uses the Azure Resource Manager technology to programatically define these deployments however even if you don’t intend to publish in the MarketPlace it’s still handy to define your own infrastructure for dev, test and production setups.
Azure Resource Manager
A few things you should know about Azure Resource Manager (or ARM)
- At it’s core it a JSON template that defines your infrastructure (Virtual Machines, Databases, Services etc.)
- You group related components together into Resource Groups
- You can deploy, update and tear down entire setups using a single operation
- You can define user roles and access
So in short, Azure Resource Manager is all about DevOps and making it easy to stand up entire environments using JSON.
You could startup with a blank template and pour over the documentation which is fine but if like myself you like to start with something that already works and customize it then I have great news for you!
The Azure team has a whole bunch of Quick Start Templates on GitHub which I recommend starting and forking for your own needs.
Single VM Deployment
Let’s start with a single VM deployment using PowerShell (there are many other ways to do this including through the portal, through the Node.js command line interface etc. but let’s start with this one).
We are going to use the “101-simple-windows-vm” as the base of our deployment.
Install Azure PowerShell
Follow the instructions here on How to install and configure Azure PowerShell
Clone the git repository
Start by cloning the sample templates locally
git clone https://github.com/Azure/azure-quickstart-templates
I suggest copying
azure-quickstart-templates\101-simple-windows-vm to another folder so you can edit as needed.
Understanding the files
azure-quickstart-templates\101-simple-windows-vm you will find 2 files related to Azure.
This is the actual template that will deploy a single Windows Server virtual machine. The main sections are
$schema section defines which schema the template adheres to. You can find the latest schema at the official GitHub rep here.
contentVersion is the version of your template. I suggest you update it with each commit to your source control (you are using source control right?!?).
parameters section declares all user defined parameters to be specified during a deployment either from a parameters.json file or via the command line.
More on parameters here.
Variables are just that, variables defined at the top of the template that can be used throughout the template. Handy for specifying things like ip addresses, usernames and other metadata.
You can even include parameters into your variables (to say built a connection string using the user name & password specified via parameters) using built in functions. Learn more about Expressons & Functions & variables.
Here is where we define the actual resources to be deployed such as storage accounts, network interfaces, virtual machines and much more.
In the sample template we are using there are 5 resources being defined for deployment
- A storage account for the OS & Data drives for the VM
1. A public IP address
1. A Virtual Network
1. A Network Interface
1. And finally a Virtual Machine
You could pass in parameters via the command line and perhaps you should for things like passwords, however for everything else it’s good practice to keep them in a parameters file which can be checked into your source control. You could have separate files for different environments such as azuredeploy.prod.parameters.json & azuredeploy.dev.parameters.json.
Edit the file to supply the following
newStorageAccountName: This name must be unique for your subscription and will be used to store the OS & Data disk for your Virtual Machine. Tip: The name MUST be lowercase.
adminUsername: This is the administrator username, which cannot be admin or administrator just FYI
adminPassword: Pretty straight forward ;)
dnsNameForPublicIP: This is your globally unique name for your server. Tip: The name MUST be lowercase.
Deploying from PowerShell
Open PowerShell and switch Azure to ARM
- Open a Microsoft Azure PowerShell
- By default the Azure PowerShell is setup for the older “Azure Service Management” commands, to switch it to use the new ARM (Azure Resource Manager) API run the following:
Switch-AzureMode -Name AzureResourceManager
Create a resource group
All ARM template deployments must be deployed into a resource group. You could use an existing one but for this tutorial we’ll create a new one with the name MyNewResourceGroup and in the West US location:
New-AzureResourceGroup -Name MyNewResourceGroup -Location “West US”
Deploy the template
New-AzureResourceGroupDeployment -Name MyExampleDeployment -ResourceGroupName MyNewResourceGroup -TemplateFile azuredeploy.json -TemplateParameterFile azuredeploy.parameters.json
What if it all goes wrong?
You can see the errors by running
Get-AzureResourceGroupLog -ResourceGroup MyNewResourceGroup -Status Failed
Tear it down
All done testing this out? Simply run the following to delete everything in the resource group you just created:
Remove-AzureResourceGroup -Name MyNewResourceGroup
I highly suggest trying out various sample templates as well as this free Microsoft Virtual Academy Course: Building Infrastructure in Azure using Azure Resource Manager.