GCP Compute Engine Cheat Sheet
Commands for creating, connecting to, and managing virtual machine instances and disks on Google Compute Engine.
Instance Lifecycle
Create, list, and control VM instances.
gcloud compute instances create my-vm \ --zone=us-central1-a \ --machine-type=e2-medium \ --image-family=debian-12 --image-project=debian-cloud \ --boot-disk-size=20GBgcloud compute instances listgcloud compute instances stop my-vm --zone=us-central1-agcloud compute instances start my-vm --zone=us-central1-agcloud compute instances delete my-vm --zone=us-central1-a
SSH & File Transfer
Connect to and copy files to/from an instance.
gcloud compute ssh my-vm --zone=us-central1-agcloud compute scp ./localfile.txt my-vm:~/remote.txt --zone=us-central1-agcloud compute scp my-vm:~/remote.txt ./localfile.txt --zone=us-central1-a
Disks & Images
Manage persistent disks and custom images.
gcloud compute disks create my-disk --size=100GB --zone=us-central1-agcloud compute instances attach-disk my-vm --disk=my-disk --zone=us-central1-agcloud compute images create my-image --source-disk=my-disk --source-disk-zone=us-central1-a
Machine Families
Key machine families to know.
- E2- Cost-optimized, general purpose, shared-core options available
- N2 / N2D- Balanced price/performance for general workloads
- C2 / C3- Compute-optimized, high per-core performance
- A2 / A3- GPU-accelerated instances for ML/AI workloads
- Preemptible / Spot VM- Short-lived, discounted instances that can be reclaimed by GCP anytime
Key Concepts
Key key concepts to know.
- Instance Template- Reusable VM configuration used to create instances or managed instance groups
- Managed Instance Group (MIG)- Auto-scaling, self-healing group of identical VM instances
- Persistent Disk- Durable network-attached block storage independent of instance lifecycle
- Startup Script- Shell script run automatically on instance boot via metadata
Instance Template + Managed Instance Group
Define a reusable template and an auto-healing, auto-scaling MIG from it.
gcloud compute instance-templates create web-template \ --machine-type=e2-medium \ --image-family=debian-12 --image-project=debian-cloud \ --metadata-from-file=startup-script=startup.shgcloud compute instance-groups managed create web-mig \ --base-instance-name=web \ --template=web-template \ --size=3 --zone=us-central1-agcloud compute instance-groups managed set-autoscaling web-mig \ --zone=us-central1-a \ --max-num-replicas=10 --min-num-replicas=2 \ --target-cpu-utilization=0.6 --cool-down-period=90
Health Check + Load-Balanced Backend Service
Wire a MIG to an HTTP(S) load balancer with automatic instance replacement on failed checks.
gcloud compute health-checks create http web-hc \ --port=80 --request-path=/healthz \ --check-interval=10s --unhealthy-threshold=3gcloud compute instance-groups managed update web-mig \ --zone=us-central1-a --health-check=web-hc --initial-delay=60gcloud compute backend-services create web-backend \ --global --protocol=HTTP --health-checks=web-hcgcloud compute backend-services add-backend web-backend \ --global --instance-group=web-mig --instance-group-zone=us-central1-a
Per-Instance Metadata & Guest Attributes
Pass config into a VM via metadata and read it back from inside the guest.
# Set custom metadata at creationgcloud compute instances create my-vm \ --zone=us-central1-a \ --metadata=app-env=production,release=v2.3.1# Inside the VM, read metadata via the metadata servercurl -H "Metadata-Flavor: Google" \ "http://metadata.google.internal/computeMetadata/v1/instance/attributes/app-env"# Query it back externally without SSHgcloud compute instances describe my-vm --zone=us-central1-a \ --format='get(metadata.items[0].value)'
Shielded VM & Confidential Computing Flags
Harden a VM against boot-level rootkits and encrypt memory in use.
gcloud compute instances create secure-vm \ --zone=us-central1-a \ --machine-type=n2d-standard-4 \ --shielded-secure-boot \ --shielded-vtpm \ --shielded-integrity-monitoring \ --confidential-compute-type=SEV \ --maintenance-policy=TERMINATE
Networking & Placement Concepts
Concepts that matter once you move past a single default-network VM.
- VPC Network Tags- Labels attached to instances used as firewall-rule targets instead of per-IP rules
- Alias IP Ranges- Additional IP ranges routable to a VM's NIC, commonly used for container IPs (e.g. GKE)
- Sole-Tenant Nodes- Dedicated physical servers for instances with licensing or compliance isolation requirements
- Placement Policy- Controls physical proximity (compact) or spread (max distribution) of instances for latency or fault tolerance
- Live Migration- GCP transparently moves a running VM off hardware needing maintenance without rebooting it (standard VMs only, not Spot)
- OS Login- IAM-managed SSH key/user access to instances instead of manually managed project/instance SSH metadata keys
Use Spot VMs for fault-tolerant batch or stateless workloads — they cost up to 60-91% less than standard on-demand pricing, but design your workload to checkpoint progress since GCP can reclaim them with only 30 seconds notice.