Defining virtual machine state in Azure

In this post, we’ll explore how we can leverage desired state configuration feature of PowerShell to define the desired configuration of Azure virtual machine. Up to this point, we’d explored how to define the OS/Size/Storage/Network and other related configuration using Get-AzureVM and associated cmdlets. What if one can go a bit further, and also define server configuration at the same time.

Do note that we’ll need to have PowerShell version 4.0 or greater on the client machine to use related commands.

Here’s a simple example we will be using to define a very basic web server with ASP.NET:

configuration IISInstall 
{ 
    node "localhost"
    { 
         WindowsFeature IIS 
         { 
              Ensure = “Present” 
              Name = “Web-Server”      
         } 
         WindowsFeature IIS-ASP
         { 
             Ensure = “Present” 
             Name = “Web-Asp-Net45”      
         } 
    } 
}

Let’s say this file as dsc-webconfig.ps1. Now we can use Publish-AzureVMDscConfiguration cmdlet to upload our configuration file to Azure. Here’s the command for same:

Publish-AzureVMDscConfiguration -ConfigurationPath "C:\Users\Admin\Documents\dsc-webconfig.ps1"

All configuration files are saved in a container named “windows-powershell-dsc” in the storage account:

container-location-for-dsc-files
container location for DSC files

Now, we only need to add only one line to script used in our earlier post:

#Login into Microsoft Azure
add-azureaccount

#Create a new storage account, if not already having one
$storageAccountName = "qastoragenortheurope"
$storageAccountLocation = "north europe"
$storageAccountType = "Standard_LRS"
New-AzureStorageAccount -StorageAccountName $storageAccountName -Location $storageAccountLocation -Type $storageAccountType

#Set default storage account for current azure subscription
Set-AzureSubscription -SubscriptionId 65369d7b-b6a4-444e-a6e7-7359df26fba0 -CurrentStorageAccountName "qastoragenortheurope"


#create quick azure vm
$adminUser = "itadmin"
$password = "itadmin@123"
$serviceName = "metavrseweb"
$location = "north europe"
$Size = (Get-AzureRoleSize | Where { $_.InstanceSize –eq "Standard_D1" }).InstanceSize
$vmName = "metavrse-web-02"
$imageFamily = "Windows Server 2012 R2 Datacenter"

$imageName = Get-AzureVMImage | 
 where { $_.ImageFamily -eq $imageFamily } |
 sort PublishedDate -Descending |
 select -ExpandProperty ImageName -First 1

New-AzureVMConfig -Name $vmName `
 -InstanceSize $size `
 -ImageName $imageName |
Add-AzureProvisioningConfig -Windows `
 -AdminUsername $adminUser `
 -Password $password |
Add-AzureDataDisk -CreateNew `
 -DiskSizeInGB 500 `
 -LUN 0 `
 -DiskLabel "data" |
Add-AzureEndpoint -Name "SQL" `
 -Protocol tcp `
 -LocalPort 1433 `
 -PublicPort 1433 |
Set-AzureVMDSCExtension –ConfigurationArchive "dsc-webconfig.ps1.zip" |
New-AzureVM -ServiceName $serviceName `
 -Location $location

You can use DSC to create your development and test environments in Microsoft Azure. When you’re done, you can use the same file to spin up your production systems, regardless of location.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s