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.
Top comments (0)