Cloud and infrastructure as code
VMs, spot and autoscaling
Pick a VM family from the bottleneck, buy spot capacity for interruptible work, and let an autoscaling group keep a fleet at a target load.
A virtual machine (an EC2 instance or a Compute Engine VM) is the base layer for almost everything else, including Kubernetes nodes and many managed databases. Providers group VM types into families by how much memory each vCPU gets. Choosing the family and the size is mostly a question of which resource runs out first.
VM families and sizing
| Family | AWS | GCP | Memory per vCPU | Typical use |
|---|---|---|---|---|
| General purpose | m7i, m7g | n2, n4, e2 | ~4 GiB | Web and API servers |
| Compute optimised | c7i, c7g | c3, c4 | ~2 GiB | CPU-bound work such as encoding |
| Memory optimised | r7i, r7g | m3, n2-highmem | ~8 GiB+ | Databases and caches |
| Accelerated | g6, p5 | g2, a3 | varies | ML training and inference |
AWS names decode mechanically. In m7g.2xlarge, m is the family, 7 is the generation, g means Graviton (ARM), and 2xlarge is the size. Each size step doubles resources. On an m, 2xlarge is 8 vCPUs with 32 GiB.
Three rules help with sizing.
- Start from the bottleneck. If CPU hits 80% while memory sits at 20%, move to a compute family.
- Prefer the newest generation. Each one is usually 10 to 25% better in price-performance.
- Try ARM. AWS claims Graviton gives up to about 40% better price-performance. Node, Python, Java and Go usually run on it unchanged.
Spot capacity
Spot (AWS) and Spot VMs (GCP) sell spare capacity at a steep discount, typically 60 to 90% off. The provider can take it back. AWS gives a two-minute warning and GCP about 30 seconds.
Spot suits stateless, interruptible work: CI runners, batch jobs, and web tiers behind a load balancer with a few on-demand instances as a floor. It is a poor fit for a single database primary.
Batch fleet: 40 x m7i.xlarge, 24/7
On-demand at ~$0.20/hour: 40 x 0.20 x 730 h = $5,840/month
Spot at ~70% off: 40 x 0.06 x 730 h = $1,752/month
Savings: about $4,090/month, if the jobs checkpoint and retrySpread the fleet across several instance types and zones, so a shortage of one type does not drain it.
Autoscaling groups
You rarely launch VMs one by one. An Auto Scaling group (AWS) or managed instance group (GCP) keeps identical VMs running from a template. It replaces unhealthy instances and adds or removes them to hold a metric near a target.
This AWS command keeps average CPU near 50 percent.
aws autoscaling put-scaling-policy \
--auto-scaling-group-name api-asg \
--policy-name cpu50 \
--policy-type TargetTrackingScaling \
--target-tracking-configuration '{
"PredefinedMetricSpecification": {"PredefinedMetricType": "ASGAverageCPUUtilization"},
"TargetValue": 50.0
}'The target is 50%, not 80%, because a new instance takes minutes to boot and warm up. The headroom absorbs the spike while that happens.
Common mistakes
- Running a single database primary on spot. An interruption takes the database with it.
- Buying one instance type for spot. A shortage of that type then empties the fleet.
- Targeting 80% CPU on a web tier. There is no room left while new instances boot.
Check yourself
- Why target 50% CPU instead of 80%? Answer: New instances take minutes to boot and warm up, and the spare headroom absorbs the spike in the meantime.
- Which workload suits spot capacity? Answer: Stateless, interruptible work such as batch jobs and CI runners, ideally with checkpoints.
Key takeaways
- Choose a VM family from the resource that runs out first, and prefer the newest generation.
- Spot is 60 to 90% cheaper but can be reclaimed with a two-minute (AWS) or 30-second (GCP) warning.
- An autoscaling group keeps a fleet at a target metric and replaces unhealthy instances.
Further learning
Next, see the three kinds of storage the instances attach to: storage types.
Related
Cloud primitives on AWS and GCP
Regions, zones, compute, storage, managed databases and messaging, how the services map between AWS and GCP, and how to decide what to run yourself.
Kubernetes core concepts
The control loop at the heart of Kubernetes, the objects you use every day, and how a request reaches a pod. Start here for the map of Kubernetes pages.
Serverless and managed runtimes
Lambda, Cloud Run and friends - what you give up, what you gain and the cost crossover versus always-on containers.