Step-By-Step: Deploying Multiple Services via Azure Resource Manager

A few weeks ago, Pierre Roman explained the changes from cloud services in the classic portal to the new resource manager in the preview portal. He explained that the Azure Resource Manager (ARM) manages a Resource Group which contains all the application resources, from storage and networking to virtual machines. This Step-By-Step will provide details on how to deploy a basic infrastructure in a Resource Group in one script. The completed solution will contain a Windows 2012 R2 Server, networks, and storage.

When deploying using ARM some of the PowerShell commands are different than the commands you may have used in the Classic portal. Please be sure to run the Powershell command update-help.

You may be wondering why you would want to script a basic deployment. Scripting is a great solution when you need to deploy a similar solution repeatedly. In this script you will notice I have used parameters that can easily be changed for each deployment.

We will walk through the following full script. You can also download the script from here.

The first section of the script assigns the correct subscription and, more importantly changes the mode to ARM, as the default mode is classic.

## Subscription

$subscriptionName = "Pay-As-You-Go" # get with Get-AzureSubscription

Set-AzureSubscription -SubscriptionName $subscriptionName

Select-AzureSubscription $subscriptionName

Switch-AzureMode –Name AzureResourceManager

  

The next section is for some additional parameters. Now you can start to see the advantage of using parameters in your scripts. It is easy to change the variables to meet the deployment needs. We will assign resources to a Resource Group, therefore you will need to define a Resource Group.

## Global

$ResourceGroupName = "canitpro"

$Location = "East US"

## Storage

$StorageName = "CanStor"

$StorageType = "Standard_LRS"

The following section details our network setup.

## Network

$InterfaceName = "ServerInterface"

$Subnet1Name = "Subnet1"

$VNetName = "VNet01"

$VNetAddressPrefix = "10.0.0.0/16"

$VNetSubnetAddressPrefix = "10.0.0.0/24"

The next section is compute. This is where we define our virtual machine configuration.

## Compute

$VMName = "CanItPro"

$ComputerName = "FileServer"

$VMSize = "Standard_A1"

$OSDiskName = $VMName + "osDisk"

Now that we have most of our parameters defined, we can start building the rest of the script. Our first task is to create our Resource Group.

# Resource Group

New-AzureResourceGroup -Name $ResourceGroupName -Location $Location

Next we set up our storage account. You will notice I substituted the "Name" label for a fixed name instead of the defined $Name parameter. This was done for demonstration purposes.

# Storage

$StorageAccount = New-AzureStorageAccount -ResourceGroupName $resourcegroupname -Name "canstor" -Type $StorageType -Location $Location

  

# Network

$PIp = New-AzurePublicIpAddress -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -AllocationMethod Dynamic

$SubnetConfig = New-AzureVirtualNetworkSubnetConfig -Name $Subnet1Name -AddressPrefix $VNetSubnetAddressPrefix

$VNet = New-AzureVirtualNetwork -Name $VNetName -ResourceGroupName $ResourceGroupName -Location $Location -AddressPrefix $VNetAddressPrefix -Subnet $SubnetConfig

$Interface = New-AzureNetworkInterface -Name $InterfaceName -ResourceGroupName $ResourceGroupName -Location $Location -SubnetId $VNet.Subnets[0].Id -PublicIpAddressId $PIp.Id

Now that our network is configured, we can add our virtual machines to the Resource Group, referencing some of the parameters that were defined during the network configuration.

# Compute

## Setup local VM object

$Credential = Get-Credential

$VirtualMachine = New-AzureVMConfig -VMName $VMName -VMSize "Standard_A1"

$VirtualMachine = Set-AzureVMOperatingSystem -VM $VirtualMachine -Windows -ComputerName $ComputerName -Credential $Credential -ProvisionVMAgent -EnableAutoUpdate

$VirtualMachine = Set-AzureVMSourceImage -VM $VirtualMachine -PublisherName MicrosoftWindowsServer -Offer WindowsServer -Skus 2012-R2-Datacenter -Version "latest"

$VirtualMachine = Add-AzureVMNetworkInterface -VM $VirtualMachine -Id $Interface.Id

$OSDiskUri = $StorageAccount.PrimaryEndpoints.Blob.ToString() + "vhds/" + $OSDiskName + ".vhd"

$VirtualMachine = Set-AzureVMOSDisk -VM $VirtualMachine -Name $OSDiskName -VhdUri $OSDiskUri -CreateOption FromImage

Finally, the virtual machine is created in the "canitpro" Resource Group in the East US datacenter, using the parameters defined for $virtualmachine.

## Create the VM in Azure

New-AzureVM -ResourceGroupName $ResourceGroupname -Location $Location -VM $VirtualMachine

  

Here's the completed Resource Group in the Azure Preview Portal.

In under 10 minutes, my environment was deployed using my defined parameters.

Having a few go-to scripts that can be customized for each deployment can save you time and ensure that you have all the required resources for your deployments.

Now that you have seen the flexibility of scripting, it is the perfect time to consider using Powershell for your future deployments if you have not already.