GCP Compute Engine

Compute Engine is GCP's Infrastructure as a Service (IaaS) offering. It lets you create and run virtual machines (VMs) on Google's infrastructure. A virtual machine behaves exactly like a physical computer — it has a CPU, RAM, storage, and a network connection — but it runs inside Google's data center instead of sitting on a desk.

Think of a virtual machine like renting an apartment instead of building a house. The building (Google's hardware) already exists. A specific unit (VM) is rented, configured to needs, and moved out (deleted) when done.

Key Concepts in Compute Engine

Instance

An instance is simply a virtual machine running in GCP. Each instance has an operating system, CPU, memory, and one or more disks attached to it.

Machine Type

The machine type defines how much CPU and RAM an instance gets. GCP offers several families of machine types:

FamilyUse CaseExample
General Purpose (E2, N2)Web servers, development, small appse2-medium (2 vCPU, 4 GB RAM)
Compute Optimized (C2)High-performance computing, gaming serversc2-standard-4 (4 vCPU, 16 GB RAM)
Memory Optimized (M2)In-memory databases, SAP HANAm2-ultramem-208
Accelerator Optimized (A2)Machine learning, GPU workloadsa2-highgpu-1g (1 NVIDIA A100 GPU)

Boot Disk and Images

Every VM needs an operating system. Compute Engine uses disk images to set up the OS. GCP provides public images for Linux distributions (Ubuntu, Debian, CentOS) and Windows Server. Custom images can also be created from existing VMs.

Persistent Disk vs Local SSD

Persistent Disk:
┌──────────────────────────────────────┐
│  VM Instance                         │
│                                      │
│  Attached Persistent Disk            │
│  (survives VM restart/deletion)      │
│  ✓ Data persists independently       │
│  ✓ Can be resized                    │
│  ✓ Can be attached to multiple VMs   │
└──────────────────────────────────────┘

Local SSD:
┌──────────────────────────────────────┐
│  VM Instance                         │
│                                      │
│  Local SSD (physically attached)     │
│  (data lost when VM stops)           │
│  ✓ Extremely fast (low latency)      │
│  ✗ Not persistent                    │
└──────────────────────────────────────┘

Creating a VM Instance

Via the Console

  1. Go to Compute Engine → VM Instances
  2. Click Create Instance
  3. Enter an instance name (example: my-web-server)
  4. Select a region and zone (example: us-central1-a)
  5. Choose a machine type (example: e2-micro — free tier eligible)
  6. Select a boot disk image (example: Debian GNU/Linux 11)
  7. Under Firewall, check Allow HTTP traffic if running a web server
  8. Click Create

Via Cloud Shell

gcloud compute instances create my-web-server \
  --zone=us-central1-a \
  --machine-type=e2-micro \
  --image-family=debian-11 \
  --image-project=debian-cloud \
  --tags=http-server

Connecting to a VM

The simplest way to connect is via SSH directly from the Console:

  1. Go to Compute Engine → VM Instances
  2. Click the SSH button next to the instance

Or from Cloud Shell:

gcloud compute ssh my-web-server --zone=us-central1-a

Installing a Web Server on the VM

After SSH-ing into the VM, run these commands to install and start a basic web server:

# Update package lists
sudo apt-get update

# Install the Apache web server
sudo apt-get install -y apache2

# Start Apache
sudo systemctl start apache2

# Check it is running
sudo systemctl status apache2

Now open a browser and visit the VM's external IP address. The default Apache page will appear.

VM Lifecycle

Start
  │
  ▼
PROVISIONING  →  VM is being allocated hardware
  │
  ▼
STAGING       →  OS is booting
  │
  ▼
RUNNING       →  VM is active and accessible
  │
  ├──── STOP  →  TERMINATED (VM off, disk kept, not billed for CPU/RAM)
  │
  └──── DELETE →  Resources removed permanently

VM Pricing Options

OptionDescriptionBest For
On-DemandPay per second of use, no commitmentVariable workloads
Committed Use Discount1 or 3 year commitment for up to 57% discountPredictable, stable workloads
Spot VMs (Preemptible)Up to 91% cheaper, but GCP can stop them anytimeBatch jobs, fault-tolerant tasks

Startup Scripts

A startup script runs automatically every time a VM starts. This is useful for installing software or configuring settings without manual SSH access.

# Example startup script (add during VM creation under "Metadata")
#!/bin/bash
apt-get update
apt-get install -y apache2
echo "Hello from GCP VM!" > /var/www/html/index.html
systemctl start apache2

Key Takeaways

  • Compute Engine creates virtual machines (instances) on GCP's infrastructure.
  • Machine types define CPU and RAM — choose based on workload needs.
  • Persistent Disk stores data independently of the VM lifecycle.
  • VMs can be created via the Console or the gcloud CLI.
  • Spot VMs offer large discounts but can be stopped by GCP at any time.
  • Startup scripts automate instance configuration at boot time.

Leave a Comment