Azure Managed VM Images: Using Premium Data Disks

DZone 's Guide to

Azure Managed VM Images: Using Premium Data Disks

Here's a simple script that enables Premium storage for data disks with Azure Managed VM Images while using an ARM template.

· Cloud Zone ·
Free Resource

Azure Managed VM Images provide a great way to capture customizations to a base Virtual Machine that can be reused over and over as a custom image — and these images can include the Managed Disks for both OS and data disks! However, when saving an image, disks are defaulted to using Standard storage to save on costs. When provisioning a new VM from this image via the portal or PowerShell, we can override this default to use Premium storage for OS disks, but not currently for data disks.

Premium Data Disks With Managed VM Images... What to Do, What to Do?!

So, how can we use Managed VM Images to provision new VMs with Premium data disks? Well... we can provision the new VMs using ARM templates (see the template storageProfile snippet here) instead of via the Portal or PowerShell, or we can use a bit of PowerShell magic to convert our existing Managed VM Image(s) to default to Premium Storage. Below, I've included a sample script snippet that we can leverage to convert an existing Managed VM Image to a new image that defaults the disks to using Premium storage. You can also fork a copy of this script from my GitHub repo here.

# Copyright (c) Microsoft.  All rights reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#   http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# See the License for the specific language governing permissions and
# limitations under the License.

# Sign-in with Azure AD account



# Select Azure Subscription

    $subscriptionId = 
        ( Get-AzureRmSubscription |
            Out-GridView `
              -Title "Select an Azure Subscription ..." `

    Select-AzureRmSubscription `
        -SubscriptionId $subscriptionId

# Select Azure Resource Group in which existing VM is provisioned

    $rgName =
        ( Get-AzureRmResourceGroup |
            Out-GridView `
              -Title "Select Azure Resource Group in which VM is provisioned ..." `

# Select Managed VM Image to convert to Premium storage disks

    $imageName = 
        ( Get-AzureRmImage `
            -ResourceGroupName $rgName 
        ).Name | 
        Out-GridView `
            -Title "Select an existing Image to convert ..." `

    $image = 
        Get-AzureRmImage `
            -ResourceGroupName $rgName `
            -Name $imageName

    $location = 

# Convert OS disk to default to Premium storage

    $osDisk = 

    $osDisk.StorageAccountType = 

# Convert Data disks to default to Premium Storage

    $dataDisks = 

    for ($i = 0; $i -lt $dataDisks.Count; $i++)
        $dataDisks[$i].StorageAccountType = "PremiumLRS" 

# Enter new name for converted image

    $newImageName = 
        Read-Host `
            -Prompt "Enter new name for converted VM image [Enter = ${imageName}-ssd]"

    if ($newImageName -eq "") 
        $newImageName = "${imageName}-ssd" 

# Define new config for converted image

    $newImageConfig = 
        New-AzureRmImageConfig `
            -Location $location `
            -OsDisk $osDisk `
            -DataDisk $dataDisks

# Create new image based on converted image config 

    New-AzureRmImage `
        -ResourceGroupName $rgName `
        -ImageName $newImageName `
        -Image $newImageConfig
arm templates, azure vm, cloud, microsoft azure

Published at DZone with permission of Keith Mayer , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}