Launch and manage Compute Engine VMs: pick a machine type and image, attach persistent disks, give a VM a static IP, configure it at boot with a startup script, and cut costs with Spot VMs and committed use.
Compute Engine rents you virtual machines by the second. Why: it is the general-purpose workhorse — you launch one, it boots in seconds, and you pay while it runs. Everything below assumes the VPC and subnet from the networking lesson.
List the zones available (VMs launch into a specific zone)
gcloud compute zones list --format "value(name)" | headA region (e.g. us-central1) is a geographic area; inside it are zones (us-central1-a, -b, …) — separate data centers. Why it matters: pick a region near your users for low latency, and spread VMs across zones so one data-center failure does not take you down.
Set default region and zone so you can omit them in later commands
gcloud config set compute/region us-central1gcloud config set compute/zone us-central1-aA machine type (e.g. e2-micro, n2-standard-4) sets the vCPUs and memory. Families: e2 = cheap/general, n2 = balanced, c2 = compute, m2 = memory. Why: match the workload — a small site fits e2-micro; a database wants a memory-optimized type. You can also build a custom type.
List machine types in your zone with their CPU and memory
gcloud compute machine-types list --filter "zone:us-central1-a" \
--format "table(name, guestCpus, memoryMb)" | headAn image is the operating system a new VM copies onto its boot disk. Why: it decides what the server starts as — Debian, Ubuntu, or your own custom image. Images are grouped into "families" so you always get the latest patch of, say, Debian 12.
List image families you can boot from
gcloud compute images list --format "table(family, project)" | headCombine a machine type, an image family, and your network. Why the tag: the "web" tag makes the firewall rules from the networking lesson apply to this VM. This single command creates a real, billable VM. SSH keys are managed for you by gcloud.
Create a Debian VM in your subnet, tagged "web"
gcloud compute instances create my-vm \
--zone us-central1-a \
--machine-type e2-micro \
--image-family debian-12 --image-project debian-cloud \
--network my-vpc --subnet central-subnet \
--tags webSSH in — gcloud handles the keys
gcloud compute ssh my-vm --zone us-central1-aA startup script runs when a VM boots — the standard way to install software automatically. Why: instead of SSHing in to set up each VM by hand, you bake the setup into creation so VMs come up ready to serve.
Save this as startup.sh — installs and starts nginx
#!/bin/bash
apt-get update
apt-get install -y nginx
systemctl enable --now nginxPass it at create time with --metadata-from-file
gcloud compute instances create web-vm \
--zone us-central1-a --machine-type e2-micro \
--image-family debian-12 --image-project debian-cloud \
--network my-vpc --subnet central-subnet --tags web \
--metadata-from-file startup-script=startup.shA persistent disk is durable network storage that survives the VM; you can detach, reattach, resize, and snapshot it. Why types matter: standard (HDD) is cheap, balanced/SSD are faster. Snapshots are point-in-time backups you can restore from.
Create and attach a 50 GB balanced persistent disk
gcloud compute disks create data-disk --size 50GB \
--type pd-balanced --zone us-central1-agcloud compute instances attach-disk my-vm \
--disk data-disk --zone us-central1-aSnapshot the boot disk for backup
gcloud compute disks snapshot my-vm \
--snapshot-names my-vm-backup --zone us-central1-aAttach the static external IP you reserved earlier so the VM keeps the same public address across restarts. Why: stable DNS and firewall allow-lists. You set it at create time, or update an existing VM's access config.
Launch a VM using the reserved static IP "web-ip"
gcloud compute instances create stable-vm \
--zone us-central1-a --machine-type e2-micro \
--image-family debian-12 --image-project debian-cloud \
--network my-vpc --subnet central-subnet --tags web \
--address web-ipHow you pay changes the price a lot. On-demand: per-second, no commitment (default). Spot VMs: spare capacity up to ~90% cheaper, but Google can reclaim them anytime (great for batch jobs). Committed use discounts: commit to 1–3 years for steady workloads. Sustained-use discounts apply automatically for VMs left running.
Launch a Spot VM — same command plus the provisioning model
gcloud compute instances create batch-vm \
--zone us-central1-a --machine-type e2-micro \
--image-family debian-12 --image-project debian-cloud \
--provisioning-model SPOT \
--instance-termination-action STOP