Bird
Raised Fist0
Azurecloud~10 mins

VM Scale Sets for auto scaling in Azure - Commands & Configuration

Choose your learning style10 modes available
Introduction
When you run many virtual machines to handle your app, it can be hard to add or remove machines as demand changes. VM Scale Sets help by automatically increasing or decreasing the number of virtual machines based on the workload.
When your website traffic grows during the day and shrinks at night, and you want to save costs by running fewer machines at night.
When you run a batch job that needs more machines to finish faster and fewer machines when idle.
When you want to keep your app available even if some virtual machines fail by automatically replacing them.
When you want to easily manage and update many identical virtual machines as a group.
When you want to respond quickly to sudden spikes in user demand without manual intervention.
Config File - main.tf
main.tf
provider "azurerm" {
  features {}
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "East US"
}

resource "azurerm_virtual_network" "example" {
  name                = "example-vnet"
  address_space       = ["10.0.0.0/16"]
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}

resource "azurerm_subnet" "example" {
  name                 = "example-subnet"
  resource_group_name  = azurerm_resource_group.example.name
  virtual_network_name = azurerm_virtual_network.example.name
  address_prefixes     = ["10.0.1.0/24"]
}

resource "azurerm_linux_virtual_machine_scale_set" "example" {
  name                = "example-vmss"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
  sku                 = "Standard_DS1_v2"
  instances           = 2
  admin_username      = "azureuser"

  source_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  os_disk {
    caching              = "ReadWrite"
    storage_account_type = "Standard_LRS"
  }

  network_interface {
    name    = "example-nic"
    primary = true

    ip_configuration {
      name      = "internal"
      subnet_id = azurerm_subnet.example.id
      primary   = true
    }
  }

  automatic_instance_repair {
    enabled      = true
    grace_period = "PT10M"
  }

  upgrade_mode = "Manual"
}

resource "azurerm_monitor_autoscale_setting" "example" {
  name                = "example-autoscale"
  resource_group_name = azurerm_resource_group.example.name
  target_resource_id  = azurerm_linux_virtual_machine_scale_set.example.id

  profile {
    name = "autoscale-profile"

    capacity {
      minimum = "1"
      maximum = "5"
      default = "2"
    }

    rule {
      metric_trigger {
        metric_name        = "Percentage CPU"
        metric_resource_id = azurerm_linux_virtual_machine_scale_set.example.id
        time_grain         = "PT1M"
        statistic          = "Average"
        time_window        = "PT5M"
        time_aggregation   = "Average"
        operator           = "GreaterThan"
        threshold          = 75
      }

      scale_action {
        direction = "Increase"
        type      = "ChangeCount"
        value     = "1"
        cooldown  = "PT5M"
      }
    }

    rule {
      metric_trigger {
        metric_name        = "Percentage CPU"
        metric_resource_id = azurerm_linux_virtual_machine_scale_set.example.id
        time_grain         = "PT1M"
        statistic          = "Average"
        time_window        = "PT5M"
        time_aggregation   = "Average"
        operator           = "LessThan"
        threshold          = 30
      }

      scale_action {
        direction = "Decrease"
        type      = "ChangeCount"
        value     = "1"
        cooldown  = "PT5M"
      }
    }
  }
}

This Terraform file creates a resource group and a virtual network with a subnet to host the virtual machines.

The azurerm_linux_virtual_machine_scale_set resource defines a scale set with 2 Ubuntu VMs, automatic repair enabled, and manual upgrade mode.

The azurerm_monitor_autoscale_setting resource sets up auto scaling rules based on CPU usage: it adds a VM if average CPU goes above 75% and removes one if it falls below 30%, with cooldown periods to avoid rapid changes.

Commands
This command initializes Terraform in the current directory, downloading the Azure provider and preparing to create resources.
Terminal
terraform init
Expected OutputExpected
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/azurerm... - Installing hashicorp/azurerm v3.64.0... - Installed hashicorp/azurerm v3.64.0 (signed by HashiCorp) Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure.
This command creates the VM Scale Set and autoscale settings in Azure as defined in the Terraform file, without asking for confirmation.
Terminal
terraform apply -auto-approve
Expected OutputExpected
azurerm_resource_group.example: Creating... azurerm_resource_group.example: Creation complete after 2s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources] azurerm_virtual_network.example: Creating... azurerm_subnet.example: Creating... azurerm_virtual_network.example: Creation complete after 3s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.Network/virtualNetworks/example-vnet] azurerm_subnet.example: Creation complete after 2s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.Network/virtualNetworks/example-vnet/subnets/example-subnet] azurerm_linux_virtual_machine_scale_set.example: Creating... azurerm_monitor_autoscale_setting.example: Creating... azurerm_linux_virtual_machine_scale_set.example: Still creating... [10s elapsed] azurerm_linux_virtual_machine_scale_set.example: Creation complete after 1m [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/Microsoft.Compute/virtualMachineScaleSets/example-vmss] azurerm_monitor_autoscale_setting.example: Creation complete after 5s [id=/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/microsoft.insights/autoscalesettings/example-autoscale] Apply complete! Resources: 5 added, 0 changed, 0 destroyed.
→
-auto-approve - Skip manual approval to apply changes immediately
This Azure CLI command lists all virtual machines currently running in the scale set to verify the instances are created.
Terminal
az vmss list-instances --resource-group example-resources --name example-vmss --output table
Expected OutputExpected
InstanceId ComputerName ProvisioningState PowerState ------------ -------------- ------------------- ------------ 0 example-vmss_0 Succeeded VM running 1 example-vmss_1 Succeeded VM running
→
--output table - Display output in a readable table format
This command shows the autoscale settings applied to the VM Scale Set to confirm the scaling rules are active.
Terminal
az monitor autoscale show --resource-group example-resources --name example-autoscale
Expected OutputExpected
{ "autoscaleSettingResourceId": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-resources/providers/microsoft.insights/autoscalesettings/example-autoscale", "enabled": true, "profiles": [ { "name": "autoscale-profile", "capacity": { "minimum": "1", "maximum": "5", "default": "2" }, "rules": [ { "metricTrigger": { "metricName": "Percentage CPU", "operator": "GreaterThan", "threshold": 75 }, "scaleAction": { "direction": "Increase", "value": "1" } }, { "metricTrigger": { "metricName": "Percentage CPU", "operator": "LessThan", "threshold": 30 }, "scaleAction": { "direction": "Decrease", "value": "1" } } ] } ] }
Key Concept

If you remember nothing else from this pattern, remember: VM Scale Sets let you automatically add or remove virtual machines based on demand to keep your app responsive and cost-efficient.

Common Mistakes
Not setting the minimum and maximum instance counts in autoscale rules.
Without limits, the scale set might try to add too many or remove all machines, causing failures or downtime.
Always define sensible minimum and maximum instance counts to keep your app stable and control costs.
Forgetting to specify the subnet ID in the VM Scale Set network configuration.
The scale set VMs won't have network access and will fail to start properly.
Always link the VM Scale Set to a valid subnet by providing the subnet ID.
Using manual upgrade mode without planning updates.
VMs won't update automatically, which can cause security or compatibility issues.
Use manual upgrade mode only if you plan to update VMs yourself; otherwise, consider automatic or rolling upgrades.
Summary
Initialize Terraform to prepare Azure provider and modules.
Apply the Terraform configuration to create a VM Scale Set with autoscaling rules.
Use Azure CLI to list VM instances in the scale set to verify deployment.
Check autoscale settings with Azure CLI to confirm scaling rules are active.

Practice

(1/5)
1. What is the main purpose of Azure VM Scale Sets in cloud infrastructure?
easy
A. To store data permanently in the cloud
B. To create a single virtual machine with fixed resources
C. To automatically adjust the number of virtual machines based on demand
D. To manage user access and permissions

Solution

  1. Step 1: Understand VM Scale Sets function

    VM Scale Sets allow running multiple VMs that can increase or decrease automatically.
  2. Step 2: Identify the main benefit

    This automatic adjustment helps handle changes in workload efficiently.
  3. Final Answer:

    To automatically adjust the number of virtual machines based on demand -> Option C
  4. Quick Check:

    Auto scaling = Adjust VM count [OK]
Hint: Think 'auto' means adjusting VM count automatically [OK]
Common Mistakes:
  • Confusing VM Scale Sets with storage services
  • Thinking it manages user permissions
  • Assuming it creates only one VM
2. Which of the following is the correct way to define an autoscale rule in Azure VM Scale Sets to add instances when CPU usage is above 70%?
easy
A. Set a rule to add instances when CPU > 70% using metric 'Percentage CPU' and operator 'GreaterThan' with threshold 70
B. Set a rule to decrease instances when CPU > 70%
C. Set a rule to add instances when CPU < 70% using metric 'Percentage CPU' and operator 'LessThan' with threshold 70
D. Set a rule to add instances when memory usage > 70% ignoring CPU metrics

Solution

  1. Step 1: Identify correct metric and operator

    The autoscale rule should trigger when CPU usage is greater than 70%, so metric 'Percentage CPU' and operator 'GreaterThan' are correct.
  2. Step 2: Confirm action to add instances

    The rule must add instances when the condition is met, not decrease or use wrong metrics.
  3. Final Answer:

    Set a rule to add instances when CPU > 70% using metric 'Percentage CPU' and operator 'GreaterThan' with threshold 70 -> Option A
  4. Quick Check:

    CPU > 70% triggers add instances [OK]
Hint: Match metric and operator to condition exactly [OK]
Common Mistakes:
  • Using 'LessThan' instead of 'GreaterThan'
  • Confusing CPU with memory metrics
  • Setting decrease action instead of add
3. Given this autoscale rule for a VM Scale Set:
{
  "metricTrigger": {
    "metricName": "Percentage CPU",
    "operator": "GreaterThan",
    "threshold": 80
  },
  "scaleAction": {
    "direction": "Increase",
    "value": "2"
  }
}

What happens when CPU usage goes above 80%?
medium
A. The VM Scale Set decreases by 2 instances
B. The VM Scale Set increases by 2 instances
C. The VM Scale Set stays the same
D. The VM Scale Set increases by 1 instance

Solution

  1. Step 1: Analyze metric trigger

    The rule triggers when CPU usage is greater than 80%.
  2. Step 2: Analyze scale action

    The action is to increase the VM count by 2 instances.
  3. Final Answer:

    The VM Scale Set increases by 2 instances -> Option B
  4. Quick Check:

    CPU > 80% triggers increase by 2 [OK]
Hint: Look for 'Increase' and 'value' in scaleAction [OK]
Common Mistakes:
  • Confusing increase with decrease
  • Ignoring the value number
  • Assuming default increase of 1
4. You configured an autoscale rule to add 1 instance when CPU usage is above 60%, but the VM Scale Set never scales out. What is the most likely error?
medium
A. The metric name is misspelled or incorrect
B. The scale action direction is set to 'Decrease'
C. The threshold is set to 10 instead of 60
D. The VM Scale Set has no instances initially

Solution

  1. Step 1: Check metric name correctness

    If the metric name is wrong, the rule never triggers because it can't read CPU usage.
  2. Step 2: Verify other options

    Direction 'Decrease' would scale down, threshold 10 is too low but would still trigger, and initial instances are usually at least one.
  3. Final Answer:

    The metric name is misspelled or incorrect -> Option A
  4. Quick Check:

    Wrong metric name blocks scaling [OK]
Hint: Always verify metric names exactly match Azure docs [OK]
Common Mistakes:
  • Setting wrong scale direction
  • Ignoring metric name spelling
  • Assuming zero initial instances
5. You want to configure a VM Scale Set to maintain between 3 and 10 instances. It should add 2 instances when average CPU is above 75% and remove 1 instance when CPU is below 30%. Which setup correctly achieves this?
hard
A. Set minimum 5, maximum 15 instances; add 2 when CPU < 30%; remove 1 when CPU > 75%
B. Set minimum 1, maximum 5 instances; add 1 when CPU > 75%; remove 2 when CPU < 30%
C. Set minimum 3, maximum 10 instances; add 1 when CPU > 30%; remove 2 when CPU < 75%
D. Set minimum 3, maximum 10 instances; add 2 when CPU > 75%; remove 1 when CPU < 30%

Solution

  1. Step 1: Confirm instance limits

    The requirement is minimum 3 and maximum 10 instances, so only Set minimum 3, maximum 10 instances; add 2 when CPU > 75%; remove 1 when CPU < 30% matches this.
  2. Step 2: Check autoscale rules

    Set minimum 3, maximum 10 instances; add 2 when CPU > 75%; remove 1 when CPU < 30% adds 2 instances when CPU > 75% and removes 1 when CPU < 30%, exactly as required.
  3. Final Answer:

    Set minimum 3, maximum 10 instances; add 2 when CPU > 75%; remove 1 when CPU < 30% -> Option D
  4. Quick Check:

    Limits and rules match requirements [OK]
Hint: Match min/max and scale rules exactly to requirements [OK]
Common Mistakes:
  • Mixing up CPU thresholds for add/remove
  • Setting wrong min or max instance counts
  • Reversing scale directions
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard