Use BICEP to add a new version to AzureComputeGallery

This blogpost is about how to use BICEP to add a new version of your managed image to an AzureComputeGallery. Furthermore, you can do so by using this button in the portal:

Use BICEP to add a new version to AzureComputeGallery - Managed Image

But we like to automate these actions to save time and remove complexity.

Prerequisites

We need a managed image as a source. You can create this by hand or you can check out my posts about Packer & YAML

Furthermore, you need an AzureComputeGallery. I have also written a blog post about that. You can find it here.

Lastly, install Azure CLI & the BICEP module.

Add a new version using BICEP

Firstly, you need to save the following code as a .bicep file:

param imageversionname string = ''
param location string = 'WestEurope'
param imagedefinitionname string = ''
param basedate string = utcNow('u')
param managedimagename string = ''
param managedimagergname string = ''

param ResourceTags object = {
  Owner: 'Nielskok.Tech'
  Costcenter: 'AzureVirtualDesktop'
  Tier: 'Production'
}

var endoflifedate = dateTimeAdd(basedate, 'P30D')

resource managedimage 'Microsoft.Compute/images@2021-11-01' existing = {
  name: managedimagename
  scope: resourceGroup(managedimagergname)
}

resource imagedefinition 'Microsoft.Compute/galleries/images@2021-10-01' existing = {
  name: imagedefinitionname

}

resource newimageversion 'Microsoft.Compute/galleries/images/versions@2021-10-01' = {
  name: imageversionname
  location: location
  tags: ResourceTags
  parent: imagedefinition
  properties: {
    publishingProfile: {
      endOfLifeDate: endoflifedate
      replicaCount: 5
      targetRegions: [
        {
          name: location
          regionalReplicaCount: 5
        }
      ]

    }
    storageProfile: {
      source: {
        id: managedimage.id
      }
    }
  }
}

After that, you can log on to Azure using:

az login

Next, if you have multiple subscriptions, please select the appropriate subscription by using this command:

az account set --subscription <subscriptionid>

You need to provide the following variables in order to run the bicep file:

imageversionname –> This is going to be the name of the version in the AzureComputeGallery

location –> default is WestEurope but you can change this if you want to.

imagedefinitionname –> This is the name of the AzureComputeGallery/the image definition name (AzureComputeGallery/Imagedefinition)

managedimagename –> Name of the managed image of which you want to create a new version of.

managedimagergname –> ResourceGroup of the managed image of which you want to create a new version of.

This is the powershell code to run the BICEP file:

In addition, the resourcegroup in “az deployment group create –resource-group RG_WE_AVD_AzureComputeGallery” needs to be the resource group where your AzureComputeGallery resides.

$imageversionname = '1.15072022.1'
$location = 'westeurope'
$imagedefinitionname = 'NKO_AzureComputeGallery/AVDImage'
$managedimagename = 'Windows10_20220705.2'
$managedimagergname = 'RG_WE_AVD_Image_Test'

az deployment group create --resource-group RG_WE_AVD_AzureComputeGallery `
                        --template-file D:\GIT\NKO\Bicep\AzureComputeGallery\NewImageVersion.bicep `
                        --parameters imageversionname=$imageversionname location=$location imagedefinitionname=$imagedefinitionname managedimagename=$managedimagename managedimagergname=$managedimagergname

This is the result:

Use BICEP to add a new version to AzureComputeGallery - bicep deployment


And that is how you use BICEP to add a new version to AzureComputeGallery. You can add this into your pipeline to automate your new AzureComputeGallery image for Azure VMs, Azure Virtual Desktop and Windows 365.

2 thoughts on “Use BICEP to add a new version to AzureComputeGallery”

Leave a Comment