Azure Virtual Machine Scale Sets (VMSS) are useful when an application needs to run across multiple virtual machines while automatically scaling based on demand. Instead of configuring every VM manually, we can create a standard VM image and use that image to provision identical VM instances.
In this article, we'll walk through an example architecture where:
- We build a custom application image.
- The image is stored in Azure Compute Gallery.
- A VM Scale Set uses that image.
- Azure DevOps handles CI/CD.
- A new image version is deployed to the VM Scale Set through the pipeline.
1. What is Azure VM Scale Set?
An Azure Virtual Machine Scale Set allows you to create and manage a group of load-balanced VMs.
For example:
Internet | Azure Load Balancer | +---------+---------+ | | | VMSS-1 VMSS-2 VMSS-3 | | | +---------+---------+ | Application
If traffic increases, VMSS can automatically add instances. When traffic decreases, it can remove instances.
A major advantage is that all instances can be created from the same VM image, giving us consistent environments.
2. Creating a Custom VM Image
Suppose we have a simple Node.js application.
Our desired VM might contain:
Ubuntu Linux | +-- Node.js | +-- Application | +-- Configuration | +-- Required dependencies
Instead of installing these components every time a VM is created, we can create a reusable image.
There are several approaches for creating images in Azure. One common approach is to use Azure Image Builder.
The general process is:
Source Image | v Azure Image Builder | +-- Install packages +-- Copy application +-- Configure services | v Azure Compute Gallery | v VM Scale Set
The resulting image becomes the standard image used by VMSS.
3. Store the Image in Azure Compute Gallery
Azure Compute Gallery allows organizations to manage and version VM images.
For example, we could have:
Gallery: myApplicationGallery Image Definition: webapp Versions: 1.0.0 1.1.0 1.2.0
This is particularly useful for CI/CD because every application release can produce a new image version.
For example:
Application Release | v Build Image | v Image Version 1.2.0 | v Deploy to VMSS
Instead of modifying running servers manually, we create a new immutable image and roll it out.
4. Create a VM Scale Set
A VMSS can be created using Azure CLI.
First, define some variables:
RESOURCE_GROUP="rg-vmss-demo" LOCATION="eastus" VMSS_NAME="webapp-vmss"
Create the resource group:
az group create \ --name $RESOURCE_GROUP \ --location $LOCATION
A VM Scale Set can then be created using an image from Azure Compute Gallery.
For example:
az vmss create \ --resource-group $RESOURCE_GROUP \ --name $VMSS_NAME \ --image "/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<IMAGE_RG>/providers/Microsoft.Compute/galleries/myApplicationGallery/images/webapp/versions/1.0.0" \ --instance-count 2 \ --admin-username azureuser \ --generate-ssh-keys
The important part is the image reference:
.../galleries/myApplicationGallery/images/webapp/versions/1.0.0
This tells VMSS which image version should be used.
5. Why Image-Based Deployment?
Imagine we have five VM instances.
Without an image-based approach, we might have:
VM1 -> install application VM2 -> install application VM3 -> install application VM4 -> install application VM5 -> install application
This can result in configuration differences.
With an image-based approach:
Application Image 1.0.0 | +--------------+--------------+ | | | VM1 VM2 VM3
Every VM starts from the same known state.
This makes deployments more predictable and repeatable.
6. CI/CD Pipeline Architecture
A typical CI/CD architecture can look like this:
Developer | | git push v Azure Repos / GitHub | v Azure DevOps Pipeline | +------------------+ | | v v Build/Test Build Image | v Azure Compute Gallery | v Image Version 1.2.0 | v Update VMSS | v Rolling Upgrade
The key idea is that the application image becomes the deployment artifact.
7. Example Azure DevOps Pipeline
A simplified azure-pipelines.yml could look like this:
trigger: branches: include: - main variables: resourceGroup: 'rg-vmss-demo' location: 'eastus' vmssName: 'webapp-vmss' imageVersion: '1.2.0' stages: - stage: Build displayName: Build Application jobs: - job: Build steps: - script: | npm install npm test displayName: Install and test application - stage: BuildImage displayName: Build VM Image dependsOn: Build jobs: - job: Image steps: - script: | echo "Building VM image..." # Azure Image Builder commands go here displayName: Build custom image - stage: Deploy displayName: Deploy to VMSS dependsOn: BuildImage jobs: - job: DeployVMSS steps: - task: AzureCLI@2 inputs: azureSubscription: 'Azure-Service-Connection' scriptType: bash scriptLocation: inlineScript inlineScript: | az vmss update \ --resource-group $(resourceGroup) \ --name $(vmssName) \ --set virtualMachineProfile.storageProfile.imageReference.version=$(imageVersion) az vmss rolling-upgrade start \ --resource-group $(resourceGroup) \ --name $(vmssName)
This is a simplified example. In a production pipeline, you would normally add validation, image creation, approval gates, health checks, and rollback handling.
8. Understanding the Pipeline
Let's break down what happens.
Stage 1 — Build
The pipeline checks out the application and runs tests:
git push | v npm install | v npm test
If the tests fail, the deployment stops.
Stage 2 — Build Image
If the application passes testing, the pipeline creates a new VM image.
For example:
webapp | +-- 1.0.0 +-- 1.1.0 +-- 1.2.0 <-- new version
The image can contain:
Ubuntu Node.js Application version 1.2.0 Application dependencies Configuration
Stage 3 — Deploy
The pipeline updates the VMSS to use the new image version.
VMSS | +-- VM1 -> old image +-- VM2 -> old image +-- VM3 -> old image
After the upgrade:
VMSS | +-- VM1 -> new image +-- VM2 -> new image +-- VM3 -> new image
A rolling upgrade can be used so that instances are updated progressively rather than all being replaced simultaneously.
9. Rolling Deployment
For production systems, we generally don't want to replace every VM at exactly the same time.
For example:
Before: VM1 -> v1.1 VM2 -> v1.1 VM3 -> v1.1 VM4 -> v1.1
The deployment can progressively update instances:
Step 1 VM1 -> v1.2 VM2 -> v1.1 VM3 -> v1.1 VM4 -> v1.1 Step 2 VM1 -> v1.2 VM2 -> v1.2 VM3 -> v1.1 VM4 -> v1.1 Step 3 VM1 -> v1.2 VM2 -> v1.2 VM3 -> v1.2 VM4 -> v1.2
Health checks should be used to ensure the updated instances are working correctly before continuing.
10. Image Versioning
One of the most important practices is to give every image a unique version.
For example:
1.0.0 1.1.0 1.2.0 1.2.1
Avoid treating latest as your only deployment reference.
Instead, record exactly which version is running:
Production | +-- VMSS | +-- Image: webapp +-- Version: 1.2.0
This makes troubleshooting and rollback easier.
11. Rollback
Suppose version 1.2.0 has a production issue.
Because the previous image still exists:
1.0.0 1.1.0 1.2.0
we can deploy the previous known-good image:
VMSS | +-- current: 1.2.0 | +-- rollback: 1.1.0
This is one of the biggest benefits of immutable image-based deployments.
12. Recommended Production Architecture
A more complete production architecture might look like:
Developer | v Git Repository | v Azure DevOps CI | +-------+-------+ | | Build Tests | | +-------+-------+ | v Azure Image Builder | v Azure Compute Gallery / \ v1.1.0 v1.2.0 | v Azure DevOps | v VM Scale Set | +---------+---------+ | | | VM1 VM2 VM3 | | | +---------+---------+ | Application
13. Security Considerations
The pipeline should not contain Azure passwords or service-principal secrets directly.
Instead, use an Azure DevOps service connection with an appropriately scoped identity.
For example:
Azure DevOps | | Managed identity / service connection v Azure Resources
Also consider:
- Store application secrets in Azure Key Vault.
- Follow least-privilege access.
- Restrict access to image galleries.
- Scan application and OS images for vulnerabilities.
- Don't bake sensitive credentials into VM images.
- Use HTTPS and appropriate network security controls.
14. Best Practices
For a production VMSS CI/CD implementation, consider the following:
- Immutable images: Build a new image rather than modifying production VMs manually.
- Version images: Use explicit image versions such as 1.2.0.
- Automated testing: Run application and infrastructure tests before deployment.
- Rolling upgrades: Update instances progressively.
- Health checks: Validate the application after instances are upgraded.
- Rollback: Keep previous image versions available.
- Infrastructure as Code: Manage VMSS, networking, identities, and other infrastructure using Bicep or Terraform.
- Secrets management: Use Azure Key Vault instead of putting secrets in pipeline YAML.
- Approvals: Add an approval before production deployment where appropriate.
Conclusion
A strong Azure VM Scale Set deployment strategy is to treat the VM image as a versioned deployment artifact.
The overall process becomes:
Code | v Build & Test | v Create VM Image | v Publish Image Version | v Update VMSS | v Rolling Upgrade | v Health Check | +---- Success ---> Production | +---- Failure ---> Rollback
This approach gives teams a repeatable way to deploy applications to VM Scale Sets while maintaining consistency between instances and providing a straightforward path for upgrades and rollback.
The exact implementation can vary depending on whether you use Azure DevOps or GitHub Actions, and whether your images are built using Azure Image Builder, Packer, or another image-building tool.