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)