Cloud Computing Tutorial · Compute · Lesson 11 of 24

Virtual Machines and Auto Scaling

Choose instance types, use machine images, and scale fleets of VMs automatically with auto scaling groups.

Create a free account to save this page, take notes and track your progress. Sign in

Virtual machines

EC2 (Azure Virtual Machines, Google Compute Engine) gives you a server with the CPU, memory, storage and network you choose. Instance types come in families:

Family (AWS examples) Optimised for Typical use
General purpose (m, t) Balanced Web servers, small databases
Compute optimised (c) CPU Batch processing, high-traffic APIs
Memory optimised (r, x) RAM Caches, in-memory analytics, large databases
Accelerated (g, p, inf, trn) GPUs or ML chips AI training and inference
Arm-based (...g, e.g. m8g) Price-performance Most Linux workloads that support Arm

Treat VMs as cattle, not pets: build them from a machine image (AMI, Azure image, GCE image) and configuration code, never fix them by hand, and replace them instead of patching in place.

Auto scaling

An auto scaling group (Azure virtual machine scale set, Google managed instance group) keeps a fleet at the right size: it replaces unhealthy instances and adds or removes instances to follow a metric.

Target-tracking auto scalingTry it yourself
import math

TARGET_CPU = 50          # target tracking: keep average CPU near 50%
instances = 4
for load in [120, 260, 380, 300, 140, 90]:          # total load in "CPU percent units"
    current_avg = load / instances
    desired = max(2, min(20, math.ceil(load / TARGET_CPU)))
    action = "scale out" if desired > instances else "scale in" if desired < instances else "hold"
    print(f"load {load:3}: avg CPU {current_avg:5.1f}% on {instances:2} -> {action:9} to {desired}")
    instances = desired

Output

load 120: avg CPU  30.0% on  4 -> scale in  to 3
load 260: avg CPU  86.7% on  3 -> scale out to 6
load 380: avg CPU  63.3% on  6 -> scale out to 8
load 300: avg CPU  37.5% on  8 -> scale in  to 6
load 140: avg CPU  23.3% on  6 -> scale in  to 3
load  90: avg CPU  30.0% on  3 -> scale in  to 2

Scale on a metric that tracks load (CPU, requests per target, queue depth), set sensible minimums and maximums, and spread instances across availability zones.

Test yourself

What does an auto scaling group do when an instance fails its health check?

  1. Terminates it and launches a replacement
  2. Pages the on-call engineer and waits
  3. Reboots the whole group
  4. Nothing
Show answer

Answer: Terminates it and launches a replacement

Groups maintain the desired count of healthy instances automatically.

Was this page helpful?