11 - Machine Types and Cost Optimization
Welcome to Day 11 of Learn GCP in 30 Days! Yesterday, you launched your first Virtual Machine and served a live web page. Today, we learn how to pick the right computer size and save up to 90% on cloud costs using GCP Machine Types & Spot VMs.
Today's Goal Today, you will learn how to select the perfect virtual computer size for your application, understand the difference between cheap HDDs and fast SSD disks, and use Google's secret 90% discount trick (Spot VMs) to run servers for pennies!
The Core Problem: Overpaying for Giant Computers
Imagine you are starting a small online shop. On day one, you expect 10 visitors a day.
What Existed Previously:
In old physical offices, if a company needed a server for a small task (like testing a new app or running a personal blog), hardware stores only sold expensive, high-power server towers.
Or, if a company needed a super-fast computer for just 3 days a month to calculate employee salaries, they had to buy a massive physical computer that sat completely idle for the remaining 27 days of the month.
Problems Faced:
- πΈ Wasted Money (Over-provisioning): Companies paid hundreds of dollars a month for giant servers when their application only used 5% of the computer's power.
- π One Size Doesn't Fit All: Different jobs need different strengths (some apps need raw CPU calculation speed, some need massive RAM memory, some need graphics GPUs for AI).
- π Zero Discounts: Traditional hardware vendors charged full price regardless of whether you used the computer 1 hour or 24 hours a day.
How Present Technology Solves It:
Google Cloud provides Smart Machine Families & Cost-Optimization Tools:
- Pre-built Machine Families: Choose pre-sized virtual computers tailored to your specific job (General Purpose for web apps, Memory-Optimized for large databases, Compute-Optimized for gaming/math).
- Custom Machine Types: Pick exact CPU cores and RAM gigabytes (e.g. 3 vCPUs and 7 GB RAM) so you pay only for what you use.
- Spot VMs (Preemptible VMs): Rent Google's idle, unused data center servers at a 60% to 90% discount!
Real-World Analogy: The Vehicle Rental Agency
Imagine walking into a Vehicle Rental Agency. You don't rent a 10-ton semi-truck just to buy groceries!
π Everyday Cloud Terms Explained in Plain English
Let's break down 5 essential machine sizing & cost terms into everyday concepts:
1. Machine Families (The 4 Categories)
Google groups virtual computers into 4 main families so you can pick the right tool for the job:
| Machine Family | Real-World Equivalent | Best Used For | Example Codes |
|---|---|---|---|
| General Purpose | π Standard Sedan | Web apps, blogs, small databases, beginner testing | e2-micro, e2-medium, n2 |
| Compute-Optimized | ποΈ Sports Car | Gaming servers, video encoding, high-speed math | c2, c3 |
| Memory-Optimized | π Moving Truck | Giant in-memory databases (SAP HANA, Redis) | m2, m3 |
| Accelerator-Optimized | π Heavy Crane | AI model training, machine learning, 3D graphics | a2, g2 (with GPUs) |
The Beginner's Default Choice: E2 Family
95% of the time as a beginner or web developer, you will use the E2 General Purpose family (like e2-micro or e2-medium). It is flexible, fast, and extremely cheap!
2. Standard Disks (HDD) vs. SSD Disks
When you attach a hard drive to your VM, GCP gives you 2 main disk storage choices:
- π’ Standard Persistent Disk (HDD): Slow spinning magnetic hard drive. Very cheap! Great for background backups and file archives.
- β‘ SSD Persistent Disk: Fast solid-state flash drive. High-speed reading and writing! Great for databases and active web servers.
3. Spot VMs (Save Up to 90%!)
Google operates millions of physical servers worldwide. At any given moment, thousands of CPUs sit idle waiting for customers.
Instead of letting those servers sit empty, Google offers Spot VMs:
Anatomy Breakdown:
- π° 33% to 90% Discount: Depending on machine size and region, renting idle capacity drops costs drastically! (Standard provisioning model has zero discount; Spot model unlocks instant discounts).
- β οΈ The One Catch (Preemption): If a full-paying customer needs that server, Google gives you a 30-second advance warning and reclaims the server.
- π― Best Used For: Non-critical background jobs, batch data processing, software testing, and personal learning labs. Never use Spot VMs for main customer production databases!
4. Custom Machine Types (Build Your Own Size)
If GCP's pre-built sizes don't fit your needs (e.g. you need 3 vCPUs and 11 GB RAM), you can select Custom Machine Type to dial in the exact CPU cores and RAM memory you wantβsaving money by not paying for extra unused capacity!
5. Resizing a VM (Upgrading CPU & RAM in Seconds)
What if your small e2-micro website suddenly gets popular next month?
You don't need to rebuild your website from scratch! You simply Stop the VM, change the machine type dropdown to e2-standard-4, and Start the VM back up. Your server now has 4 vCPUs and 16 GB RAM in under 30 seconds!
π§ͺ Hands-on Lab Activity: Launching a Discount Spot VM
You can create a Spot VM using either the Web Console Website (UI) or Cloud Shell (CLI)!
Activity 1: Launch a Spot VM
Method A: Web Console Website (UI Click-by-Click)
- Log into console.cloud.google.com.
- Open the left menu
β°Click Compute Engine Click VM instances. - Click Create Instance at the top bar.
- Fill in basic details:
- Name:
cost-saver-vm - Region:
asia-south1 (Mumbai)| Zone:asia-south1-a - Machine Configuration: Series
E2, Machine typee2-micro.
- Name:
- Select Spot Provisioning Model:
- Scroll down to VM Provisioning Model.
- Change the radio button from Standard (no discount) to Spot.
- Notice the estimated price on the right side instantly shows a discount badge (e.g. 33% off for
e2-micro, up to 90% for larger instances)!
- Click Create!
Method B: Cloud Shell CLI (Command Terminal)
- Click the Activate Cloud Shell (
>_) icon at the top right of your GCP Console. - Run this command to launch the Spot VM:
bash
gcloud compute instances create cost-saver-vm \ --zone=asia-south1-a \ --machine-type=e2-micro \ --provisioning-model=SPOT \ --instance-termination-action=STOP \ --description="My cost-optimized Spot VM lab instance"
Output:
Creating instance cost-saver-vm...done.
NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUS
cost-saver-vm asia-south1-a e2-micro true 10.160.0.3 34.93.200.10 RUNNING
Activity 2: Inspect Machine Types
- Via Web Console UI: Click Create Instance Under Machine Configuration, open the Machine type dropdown menu to browse sizes and prices live.
- Via Cloud Shell CLI:
bash
gcloud compute machine-types list --filter="zone:asia-south1-a AND name:e2*"
Activity 3: Log into your Spot VM using SSH
Cloud Shell CLI vs. SSH into VM (What's the Difference?)
- Cloud Shell Terminal (
>_): Like runningcmdon your management computer to manage Google Cloud (creating VMs, creating firewalls, deleting projects). - SSH Terminal: Like running
cmdINSIDE the remote virtual server you rented from GCP (running Linux software, checking system logs, managing files).
Method A: Web Console Website (UI 1-Click)
- Go to Compute Engine VM instances.
- Next to
cost-saver-vm, click the SSH button under the Connect column. - A popup browser window opens connected directly inside your Linux server!
Method B: Cloud Shell CLI
- Run
gcloud compute ssh cost-saver-vm --zone=asia-south1-a. - Type
hostnameinside the VM prompt to confirm you are logged intocost-saver-vm, then typeexitto disconnect.
π§Ή Clean Up Step (Credit Safety Guarantee)
Delete your test Spot VM to keep your learning environment clean and costs at $0.00:
- Via Web Console UI: Check
cost-saver-vmClick Delete at the top bar. - Via Cloud Shell CLI:
bash
gcloud compute instances delete cost-saver-vm --zone=asia-south1-a --quiet
Signal vs. Noise: Key Concepts & Noise Filter
Good to Know (Key Concepts)
E2General Purpose: The standard default machine family for beginners and web apps.- Spot VMs: Rent idle Google servers at a 33%β90% discount with a 30-second reclamation warning.
- HDD vs. SSD: HDDs are cheap for backups; SSDs are fast for active databases and web servers.
- Custom Machine Types: Pick exact CPU and RAM numbers to avoid paying for unused capacity.
- Cloud Shell vs SSH: Cloud Shell manages GCP; SSH opens the terminal inside your VM.
- VM Resizing: Stop VM change size Start VM to upgrade specs in 30 seconds.
Noise Filter (Don't Memorize)
- Do NOT try to memorize exact hourly pricing cents (e.g. $0.0084/hr)βprices adjust dynamically and are calculated automatically in the GCP console pricing calculator.
- Do NOT memorize hardware processor clock speeds (GHz)βGCP handles physical CPU allocation automatically.
Common Doubts & Interview Traps
Q1: Should I run my main company database on a Spot VM to save money?
- Answer: Absolutely NOT! Spot VMs can be reclaimed by Google with a 30-second warning whenever demand spikes. Use Spot VMs for background jobs, testing, and batch processingβnever for critical databases!
Q2: What is the difference between Cloud Shell and SSH into a VM?
- Answer: Cloud Shell is your management terminal to command GCP (creating/deleting VMs). SSH is the terminal doorway into your rented virtual server to run Linux software.
Q3: Can I upgrade a running VM's CPU and RAM without deleting it?
- Answer: Yes! You stop the VM, edit its machine type (e.g. from
e2-microtoe2-medium), and start it again. You do NOT need to delete or recreate the VM!
Daily Practice Drill & Self-Check
Test your understanding of today's lesson:
// Try answering these:
1. Which machine family is best suited for everyday web apps, blogs, and beginner learning labs: General Purpose (E2), Compute-Optimized (C2), or Memory-Optimized (M2)?
2. What is the difference between Cloud Shell terminal and SSH terminal?
3. True or False: Standard provisioning model offers a Spot discount automatically.
π‘ Click for Solutions
- General Purpose (
E2)! It is balanced, flexible, and cost-effective for general web workloads. - Cloud Shell manages GCP resources (like
cmdon your laptop); SSH logs into your rented remote server to run Linux commands inside it! - False! Standard provisioning model is regular full-price billing. You must select Spot to unlock discounts!
π Awesome job! You have completed Day 11.
You now understand machine families, HDD vs. SSD disks, Spot VM savings, Cloud Shell vs SSH, and how to resize virtual computers using both the Web Console and Cloud Shell. Take a break, let today's concepts sink in, and come back tomorrow fresh!
Tomorrow on Day 12, we will explore Managed Instance Groups and Autoscaling so your application can automatically add or remove servers when traffic spikes!
β 10 - Compute Engine VMs - Launching Virtual Servers | Next Topic β 12 - Managed Instance Groups and Autoscaling