Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 45 ยท Compute

Compute Engine Virtual Machines

Learn Compute Engine: machine types, images, disks, creating VMs with the console and gcloud, SSH, startup scripts, spot VMs and cost, with a lifecycle simulator.

Compute Engine is Google Cloud's virtual machine service. A virtual machine (VM) is a computer that exists as software on Google's hardware: you pick how many CPUs and how much memory it has, choose an operating system, and it boots in under a minute. You get full control (and full responsibility) for everything inside, which makes Compute Engine the classic example of IaaS from What Is Cloud Computing?.

Choose a VM when you need an operating system you control, software that does not fit a container, or a lift-and-shift of an existing server. If you only need to run a web app, Cloud Run usually needs less work. This lesson is read-only for the commands (they need a billing-enabled project) and uses stand-ins you can run.

The parts of a VM

What you choose for a VM

Machine type How many vCPUs and how much memory.
Image The operating system the boot disk starts from.
Disks A boot disk plus optional data disks.
Zone The data center where it runs.

Name, network and service account complete the picture.

1 / 2

Machine types

A machine type is a preset combination of vCPUs and memory, grouped into families:

  • General purpose (for example the E2, N2 and newer N4 series): balanced for web servers, small databases and development. E2 is the budget-friendly starting point.
  • Compute optimized (C-series): more CPU speed for demanding computation.
  • Memory optimized (M-series): very large memory for in-memory databases.
  • Accelerator optimized (A and G series): GPUs for machine learning and graphics.

Names look like e2-medium or n2-standard-4: family, shape, vCPU count. Some families also allow custom machine types, where you choose the exact CPU and memory. The catalog changes as new generations arrive, so check the current list when you choose rather than trusting a table in a lesson.

Images and disks

A VM boots from a boot disk created from an image such as Debian, Ubuntu, Windows Server or Rocky Linux. Images belong to families (for example debian-12), and a family always points to the newest image in it.

Disks are network block storage (see Persistent Disk, Local SSD and Filestore). You pay for the disk's size whether the VM is running or not, and a larger or faster disk type costs more. Data disks can be added, resized and snapshotted without rebuilding the VM.

Creating a VM

In the console, open Compute Engine, click Create instance, and pick a name, region and zone, machine type and image. The page shows a monthly estimate and an Equivalent command line link, which is a good way to learn the CLI. The same VM from gcloud:

Terminal
gcloud compute instances create web-1 \
  --zone=us-central1-a \
  --machine-type=e2-medium \
  --image-family=debian-12 \
  --image-project=debian-cloud \
  --boot-disk-size=20GB \
  --tags=http-server

List it, then connect:

Terminal
gcloud compute instances list
gcloud compute ssh web-1 --zone=us-central1-a

gcloud compute ssh creates SSH keys if needed and connects using your IAM permissions, so you do not have to manage a list of keys by hand. The VM's network (VPC Networks, Subnets and Firewall Rules) decides whether the instance is reachable at all: the --tags value above lets a firewall rule target it.

Startup scripts

A startup script runs every time the VM boots, which is the simplest way to install software:

Terminal
cat > startup.sh <<'EOF'
#!/bin/bash
apt-get update
apt-get install -y nginx
echo "Hello from $(hostname)" > /var/www/html/index.html
EOF

gcloud compute instances create web-2 \
  --zone=us-central1-a \
  --machine-type=e2-small \
  --image-family=debian-12 --image-project=debian-cloud \
  --metadata-from-file=startup-script=startup.sh

Scripts must be safe to run repeatedly because they run on every boot. For many identical servers, bake the software into a custom image or use an instance template (Instance Groups, Autoscaling and Load Balancing).

The lifecycle and what you pay for

A VM moves through states: it is provisioned, staging, running, then stopping and terminated (stopped). A stopped VM is not charged for compute, but you still pay for its disks and for any reserved static IP address. Only delete removes those charges (a boot disk is usually deleted with the VM, but extra disks may remain).

Terminal
gcloud compute instances stop web-1 --zone=us-central1-a
gcloud compute instances start web-1 --zone=us-central1-a
gcloud compute instances delete web-1 --zone=us-central1-a
stateDiagram-v2
  [*] --> Running: create
  Running --> Stopped: stop
  Stopped --> Running: start
  Running --> [*]: delete
  Stopped --> [*]: delete

Compute is billed while Running; disks are billed until the VM and its disks are deleted.

Cheaper VMs: spot, discounts and sizing

  • Spot VMs use spare capacity at a large discount, but Google can stop them at any time with short notice. Use them for batch jobs, rendering and anything that tolerates interruption, never for the only copy of important state.
Terminal
gcloud compute instances create batch-1 \
  --zone=us-central1-a --machine-type=e2-standard-4 \
  --provisioning-model=SPOT \
  --instance-termination-action=STOP
  • Committed use discounts trade a one- or three-year commitment for a lower price (see Cost Management and Optimization).
  • Right-sizing: a VM at 5 percent CPU all week is too big. The console shows recommendations based on usage.
  • Schedules: stop development VMs overnight and at weekends.

A stand-in lifecycle and cost simulator

The simulator models the states above and bills by the hour. The prices are invented round numbers; real prices depend on machine type, region, discounts and disk type. What it demonstrates accurately is when charges apply.

JS Stand-in: VM lifecycle and bill

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice stopped-nights: it runs 120 hours but its disk is billed for all 240. Stopping saves compute, not storage.

A stand-in to compare machine sizes

This second example chooses the smallest machine from an invented catalog that meets a requirement. It is the thinking you do in the console's machine-type picker.

JS Stand-in: pick the smallest machine that fits

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Good habits

  • Name VMs and add labels (env=dev, owner=ana) so you can find and clean up resources later.
  • Run VMs as a dedicated service account, not the default one with broad access.
  • Avoid public IPs where you can; use a load balancer or Identity-Aware Proxy for access.
  • Delete what you no longer use. Set a budget alert before experimenting.

Best practices and typical workloads

Compute Engine is infrastructure as a service: you get full control and, in return, you carry the operations. These habits keep it cheap and healthy:

  • Use Spot VMs (the successor of the old preemptible VMs) for fault-tolerant work. Spot VMs have no fixed maximum runtime.
  • Turn on autoscaling with a managed instance group instead of leaving big VMs on all night.
  • Pick the right machine family and right-size from recommendations. Use custom machine types when the presets do not fit.
  • Keep VMs reproducible: boot from an image or a startup script, keep data on separate disks or in managed services, and never depend on one hand-configured server.
  • Use service accounts with minimal roles instead of keys on the VM (Service Accounts).

What it is used for: game servers that need custom networking and persistent processes, GPU accelerators for machine learning, rendering and scientific computing, lift-and-shift of existing applications and licensed software, and anything where you need a specific OS or kernel. Everything you can click in the console is also a REST API, so tools like Terraform, scripts and other services can create and manage VMs, and your own applications can integrate with external services in the same way.

Notebooks: from Datalab to Workbench

Cloud Datalab was an early managed notebook that ran on a VM in your project so you could explore BigQuery and Cloud Storage data with Jupyter. Google has retired it. The same job is now done by Vertex AI Workbench (a managed JupyterLab on a VM) and Colab Enterprise (a notebook service integrated with BigQuery). See Vertex AI and Gemini.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A VM is stopped (TERMINATED). What do you still pay for?

    Compute charges stop while the VM is stopped, but persistent disks and reserved addresses continue to bill until deleted.
  2. 2. When is a Spot VM a good choice?

    Spot VMs are discounted because Google can reclaim the capacity at any time, so the work must tolerate interruption.
  3. 3. What does a startup script do?

    It runs on every boot, so it should be written to be safe to run repeatedly.
  4. 4. What is an image family such as debian-12?

    Using a family lets you create VMs from the latest patched image without looking up its exact name.

Summary

  • Compute Engine VMs are IaaS: you choose machine type, image, disks and zone, and you manage the OS.
  • Create VMs in the console or with gcloud compute instances create; connect with gcloud compute ssh.
  • Startup scripts run on every boot; use images or templates for fleets.
  • Stopped VMs still cost for disks and reserved IPs; delete what you no longer need.
  • Spot VMs, committed use discounts, right-sizing and schedules lower the bill.

Next: Instance Groups, Autoscaling and Load Balancing