Overview and What You Will Learn
Spot VMs are heavily discounted Compute Engine instances that Google can reclaim at any time, and custom machine types let you pick an exact vCPU and memory combination instead of the nearest predefined size. Spot cuts the price per hour, and custom shapes cut how much capacity you pay for.
In this lab, you will:
- Create a Spot VM with a shutdown script and confirm its provisioning model
- Build a custom 6 vCPU and 20 GB VM that no predefined size matches
- Compare it with the nearest predefined sizes
- Check Recommender for rightsizing advice
Why This Matters in Production
A data team at a company like Grab runs a nightly batch job on a predefined machine type larger than the job needs, because it was the closest size on the list. Moving that job to a Spot VM with a right-sized custom shape cuts the cost of a workload that runs every night, with no customer impact because a missed run can simply be retried.
Core Principles
Spot VMs and custom machine types solve two different cost problems, and the two choices are independent, so one workload can use both.
interruption?} Q1 -->|Yes| S[Spot VM
deep discount] Q1 -->|No| O[Standard VM
on-demand price] S --> Q2{Fits a predefined
size well?} O --> Q2 Q2 -->|Yes| P[Predefined type] Q2 -->|No| C[Custom machine type] classDef start fill:#D1FAE5,color:#1E293B classDef spot fill:#F97316,color:#FFFFFF classDef std fill:#326CE5,color:#FFFFFF classDef custom fill:#8B5CF6,color:#FFFFFF class W start class S spot class O,P std class C custom
| Option | Answers the question | Best for |
|---|---|---|
| Spot VM | How much do I pay per hour? | Batch jobs, CI runners, stateless workers |
| Custom machine type | What shape do I pay for? | Needs between two predefined sizes |
Spot VMs fit workloads that tolerate being stopped and restarted. They never fit anything customer-facing that must stay available. Google gives a short preemption notice, around 30 seconds, before reclaiming a Spot VM.
Detailed Step-by-Step Practical Lab
Set the variables used throughout the lab. Project IDs are globally unique, so change the suffix if the create command fails, and use your own Billing Account ID.
export PROJECT_ID=gcp-cost-lab-mumbai-2026export BILLING_ACCOUNT_ID=012345-6789AB-CDEF01- Create a project, link billing, and enable Compute Engine and Recommender:
gcloud projects create $PROJECT_ID --name="Compute Cost Lab"gcloud config set project $PROJECT_IDgcloud billing projects link $PROJECT_ID --billing-account=$BILLING_ACCOUNT_IDgcloud services enable compute.googleapis.com recommender.googleapis.com- Create a Spot VM for a batch workload. The shutdown script runs during the preemption notice, so it is the place to save progress:
gcloud compute instances create vm-batch-spot \ --zone=asia-south1-a \ --machine-type=e2-standard-4 \ --image-family=debian-12 \ --image-project=debian-cloud \ --provisioning-model=SPOT \ --instance-termination-action=STOP \ --metadata=shutdown-script='#! /bin/bashecho "preempted at $(date), checkpointing" >> /var/log/batch-checkpoint.log'Remember
--instance-termination-action=STOPmeans a reclaimed Spot VM is stopped, not deleted. Its disk and configuration are kept so you can restart it later. The other option isDELETE, which removes the VM entirely. Stopped VMs still pay for their disks.
- Confirm the VM uses the Spot provisioning model:
gcloud compute instances describe vm-batch-spot \ --zone=asia-south1-a \ --format="value(scheduling.provisioningModel)"The output should be SPOT.
- Create a custom machine type with 6 vCPUs and 20 GB of memory, a combination that does not exist as a predefined size. Without a family flag, custom shapes use the N1 family:
gcloud compute instances create vm-custom-shape \ --zone=asia-south1-a \ --custom-cpu=6 \ --custom-memory=20GB \ --image-family=debian-12 \ --image-project=debian-cloud- Check the custom shape, then list the nearest predefined N1 sizes. The nearest size that covers 6 vCPUs and 20 GB is
n1-standard-8, which carries 8 vCPUs and 30 GB:
gcloud compute instances describe vm-custom-shape \ --zone=asia-south1-a \ --format="value(machineType)" gcloud compute machine-types list \ --zones=asia-south1-a \ --filter="name~'^n1-standard-(4|8)$'" \ --format="table(name,guestCpus,memoryMb)"TipTo build a custom shape in another family, add
--custom-vm-type, for example--custom-vm-type=e2or--custom-vm-type=n2. Each family has its own vCPU and memory limits.
- Ask Recommender whether any existing VM looks over-provisioned. A brand new project returns no results because Recommender needs days of usage history, so run this against a long-lived project to see real suggestions:
gcloud recommender recommendations list \ --project=$PROJECT_ID \ --location=asia-south1-a \ --recommender=google.compute.instance.MachineTypeRecommender- Clean up:
gcloud compute instances delete vm-batch-spot vm-custom-shape \ --zone=asia-south1-a --quietgcloud projects delete $PROJECT_ID --quietProduction Best Practices & Common Pitfalls
Common MistakeRunning a customer-facing or stateful workload on a Spot VM to save money, then suffering an outage when Google reclaims the capacity. Use Spot only for work that can be interrupted and retried.
TipReview Recommender's machine type suggestions regularly. It analyses historical CPU and memory use and often finds VMs that were sized as a rough guess at launch and never revisited.
- Make Spot workloads restartable. Checkpoint progress in a shutdown script, or run workers in a managed instance group so a replacement starts automatically.
- Compare before switching to custom. Custom machine types carry a small per-unit price premium over predefined types, so they save money only when the custom shape is meaningfully smaller than the nearest predefined size.
- Stack both savings when it fits. A fault-tolerant batch job with unusual memory needs is a strong candidate for Spot pricing and a custom shape together.
- Spread Spot workers across zones. Spot capacity varies by zone, and a reclaim wave in one zone may not affect another.
- Use committed use discounts for steady workloads. Spot suits interruptible work, while always-on production VMs save more through commitments.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud compute instances create --provisioning-model=SPOT |
Create a Spot VM |
gcloud compute instances create --custom-cpu=N --custom-memory=NGB |
Create a custom machine type VM |
gcloud compute machine-types list --zones=ZONE |
List predefined machine types in a zone |
gcloud compute instances describe VM --format="value(scheduling.provisioningModel)" |
Check whether a VM is Spot |
gcloud recommender recommendations list |
Check machine type rightsizing suggestions |