DEV Community

Cover image for Part-16: To Create Spot VM Instances in Google Cloud Platform Console & gcloud CLI (GCP) πŸš€
Latchu@DevOps
Latchu@DevOps

Posted on

Part-16: To Create Spot VM Instances in Google Cloud Platform Console & gcloud CLI (GCP) πŸš€

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

si-1

Operating systema and Storage

si-1

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

si-3

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
Enter fullscreen mode Exit fullscreen mode

si-4

Availability Policies

  • VM Provisioning Model: SPOT
  • On VM termination: STOP (default) or DELETE

si-5

Click Create.

si-6


πŸ”Ή 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.

si-7

si-8


πŸ”Ή Step 04: Access the Application

Once the VM is running, open the browser and go to:

http://<EXTERNAL-IP-OF-VM>

Enter fullscreen mode Exit fullscreen mode

You should see the custom Nginx welcome page. πŸŽ‰

si-9


πŸ”Ή 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
Enter fullscreen mode Exit fullscreen mode

si-10


πŸ”Ή 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • --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>

Enter fullscreen mode Exit fullscreen mode

4️⃣ Delete VM

gcloud compute instances delete demo6-vm-spot-gcloud --zone us-central1-a
Enter fullscreen mode Exit fullscreen mode

πŸ“ 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)