Cloud costs can add up quickly, especially when running large workloads. If your workloads are fault-tolerant and can handle interruptions, Spot VMs in Google Compute Engine are a great way to save 60–91% compared to regular VMs.
In this guide, we’ll walk through how to create Spot VM instances step by step:
- Using the Google Cloud Console
- Using the gcloud CLI
We’ll also deploy a simple Nginx web server with a custom startup script to test our VM.
🔹 Step 01: Introduction to Spot VMs
Spot VMs are temporary instances offered at a significant discount.
They may be stopped or deleted anytime by Google Cloud if resources are needed elsewhere.
Before termination, you get a 30-second warning to save any data.
Best suited for: CI/CD pipelines, big data, analytics, batch jobs, GKE node pools.
🔹 Step 02: Create a Spot VM Instance (Google Cloud Console)
Go to Navigation Menu → Compute Engine → VM Instances → Create Instance
Fill in the details:
- Name: demo6-vm-spot
- Labels: environment: dev
- Region: us-central1
- Zone: us-central1-a
- Series: E2
- Machine Type: e2-micro
Operating systema and Storage
Other Settings
- Display Device → checked
- Confidential VM → unchecked
- Container → unchecked
- Boot Disk → default
- Service Account → Compute Engine default
- Access Scopes → Allow default access
- Firewall → ✅ Allow HTTP Traffic
Automation (Startup Script)
Copy-paste the script from webserver-install.sh:
#!/bin/bash
sudo apt install -y telnet
sudo apt install -y nginx
sudo systemctl enable nginx
sudo chmod -R 755 /var/www/html
HOSTNAME=$(hostname)
sudo echo "<!DOCTYPE html> <html> <body style='background-color:rgb(250, 210, 210);'> <h1>Welcome to Latchu@DevOps - WebVM App1 </h1> <p><strong>VM Hostname:</strong> $HOSTNAME</p> <p><strong>VM IP Address:</strong> $(hostname -I)</p> <p><strong>Application Version:</strong> V1</p> <p>Google Cloud Platform - Demos</p> </body></html>" | sudo tee /var/www/html/index.html
Availability Policies
- VM Provisioning Model: SPOT
- On VM termination: STOP (default) or DELETE
Click Create.
🔹 Step 03: Verify the VM
- Go to VM Instances → demo6-vm-spot → Details Tab.
- Under Availability Policies, confirm the provisioning model is SPOT.
- Notice the warning that Spot VMs may be terminated anytime.
🔹 Step 04: Access the Application
Once the VM is running, open the browser and go to:
http://<EXTERNAL-IP-OF-VM>
You should see the custom Nginx welcome page. 🎉
🔹 Step 05: Delete the VM
When done, delete the VM from the console or run:
gcloud compute instances delete demo6-vm-spot --zone us-central1-a
🔹 Step 06: Create a Spot VM using gcloud CLI
Now, let’s repeat the same steps using the gcloud CLI.
1️⃣ Set the Project
gcloud config set project PROJECT_ID
gcloud config set project gcpdemos
2️⃣ Create Spot VM Instance
gcloud compute instances create demo6-vm-spot-gcloud \
--provisioning-model=SPOT \
--instance-termination-action=STOP \
--zone=us-central1-a \
--machine-type=e2-micro \
--network-interface=subnet=default \
--tags=http-server \
--metadata-from-file=startup-script=webserver-install.sh
- --provisioning-model=SPOT → Creates a Spot VM.
- --instance-termination-action=STOP → VM stops when preempted (can also set DELETE).
3️⃣ Access Application
http://<EXTERNAL-IP-OF-VM>
4️⃣ Delete VM
gcloud compute instances delete demo6-vm-spot-gcloud --zone us-central1-a
📝 Final Thoughts
Spot VMs are a game-changer for cost optimization in GCP. They’re perfect for workloads that are:
- Stateless
- Fault-tolerant
- Interruptible
By combining Spot VMs with automation, autoscaling, and containerized workloads (GKE), you can achieve huge cost savings without sacrificing performance.
💡 Pro Tip: Always design your workloads for resiliency, since Spot VMs can be taken away anytime.
👉 That’s it! You’ve now learned how to create and test Spot VMs using both the Google Cloud Console and gcloud CLI.