Compute Engine & IAM
Compute Engine is GCP's virtual machine service; IAM is how you authorize every action. Learn machine types, images and zones, instance groups for scaling, service accounts for machine identity, and how IAM roles and VPC firewall rules keep access least-privileged.
What is this?
Compute Engine is GCP's virtual machine service. An instance is launched from an image (an OS template) and sized with a machine type (for example e2-micro: 2 vCPU, 1 GB RAM), placed in a zone and a VPC subnet, and secured with firewall rules and IAM. Instance groups — managed and unmanaged — treat a set of VMs as one unit for scaling and updates. IAM is the separate identity layer: users and groups represent humans, service accounts represent machines, and roles bundle the permissions each identity may use. VPC firewall rules filter the actual traffic at the network layer.
Why do you need it?
- Compute Engine is the compute workhorse of GCP — most production workloads, managed instance groups and GKE nodes are VMs under the hood.
- Machine types, zones and instance groups are daily DevOps decisions that affect cost, latency and availability.
- Service accounts are how VMs safely authenticate to Google APIs without keys on disk.
- IAM mistakes (over-broad roles, shared keys, blanket owner) cause most real GCP security incidents.
- Firewall rules are the first network defense layer for everything you deploy.
Mental model
A company fleet garage
Compute Engine and IAM
The garage rents you cars (VMs): you pick a model (machine type), a standard engine (image), and a parking bay (zone) in a specific compound (VPC). The gate (firewall rules) decides which cars may leave the lot. IAM is the badge system: your badge (user) opens certain lots, a valet card (service account) lets the car itself drive to specific destinations, and fleet passes (roles) bundle what each driver may do — least privilege means the badge only opens the lots you actually need.
How it works
- You create an instance by choosing a machine type, image, zone, subnet and firewall tags; Compute Engine provisions a VM from pooled hardware in seconds.
- The instance boots the image's OS; startup scripts can run at first boot to install packages and configure the machine.
- Managed instance groups enforce a target instance count, autoscale on metrics, and use instance templates for rolling updates.
- IAM authenticates every action: your CLI or Console call is checked against the roles attached to your user, group or service account.
- VPC firewall rules filter traffic by network, target tags and IP ranges, while the service account controls which Google APIs the VM may call.
Launching a Compute Engine instance and connecting over SSH.
Key commands
gcloud compute instances create my-vm --zone=us-central1-a --machine-type=e2-microCreate a VM with an explicit zone and machine type.
gcloud compute instances listList every VM in the project with its zone, type and status.
gcloud compute ssh my-vm --zone=us-central1-aOpen an SSH session; gcloud manages the key pair for you.
gcloud compute instance-groups listList managed and unmanaged instance groups in the project.
gcloud iam service-accounts listList the service accounts available as machine identities.
gcloud projects get-iam-policy my-projectDump the full IAM role bindings for a project.
Examples
Create a Compute Engine VM and connect over SSH
$ gcloud compute instances create my-vm \ --zone=us-central1-a \ --machine-type=e2-micro \ --image-family=ubuntu-2204-lts \ --image-project=ubuntu-os-cloud \ --tags=http-serverCreated [https://www.googleapis.com/compute/v1/projects/my-project/zones/us-central1-a/instances/my-vm].NAME ZONE MACHINE_TYPE PREEMPTIBLE INTERNAL_IP EXTERNAL_IP STATUSmy-vm us-central1-a e2-micro 10.128.0.2 34.122.5.10 RUNNING $ gcloud compute ssh my-vm --zone=us-central1-aWelcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.0-91-generic x86_64)gcloud compute instances create provisions the VM with a machine type, an Ubuntu 22.04 image, a zone and a network tag. gcloud compute ssh generates and injects a temporary SSH key pair into the project metadata and connects — the service manages keys for you instead of you storing a .pem file.
In production
- Run stateless web tiers in a managed instance group behind a load balancer, autoscaling on CPU or request count.
- Attach a service account to each VM so it can reach GCS, BigQuery or Pub/Sub without keys on disk.
- Use spot VMs for batch jobs and preemptible-tolerant workloads to cut compute cost.
- Build repeatable VMs with startup scripts and instance templates, and replace instead of patch.
- Provide access into private VMs through IAP (Identity-Aware Proxy) tunnels instead of public SSH exposure.
Best practices
- Follow least privilege in IAM: grant the minimum role on the minimum resource, and prefer predefined roles.
- Attach service accounts to VMs instead of downloading and storing service account keys.
- Use firewall rules with target tags so rules apply to groups of instances, not ad-hoc IP lists.
- Right-size instances: start small, watch utilization, and scale out before scaling up.
- Enable MFA on admin identities and use IAP for SSH instead of opening port 22 to the internet.
Common mistakes
- Opening firewall port 22 to 0.0.0.0/0 and then dealing with brute-force login attempts.
- Downloading service account JSON keys and committing them into the repository.
- Using the organization-level owner role for daily work because it is simpler than configuring IAM.
- Choosing an oversized machine type 'to be safe' and paying for idle vCPUs.
- Creating one VM per environment instead of using a managed instance group.
Common issues
Permission denied (publickey)The OS user is wrong, the key does not match the project metadata, or the key permissions are too open.
Use gcloud compute ssh my-vm, which manages the key automatically, and confirm the OS user for the image (ubuntu, debian, etc.).
Connection timed out on port 22The firewall rule blocks your IP, or the VM has no external IP or route.
Add a firewall rule allowing tcp:22 from your IP, confirm an external IP is assigned, and verify the subnet has a route to the internet.
PERMISSION_DENIED when creating an instanceThe IAM identity lacks compute.instances.create or compute.instances.setServiceAccount.
Grant the roles/compute.instanceAdmin role to the identity, or use an identity with broader permissions and retry.
Debugging tips
- Permission denied (publickey) almost always means wrong OS user, wrong key, or the project lacks your SSH key metadata.
- Check the firewall rule allows your source IP on port 22 before blaming the key.
- Use gcloud compute instances describe to read status, machine type and attached service accounts.
- Use gcloud compute instances get-serial-port-output to read boot logs when SSH is not reachable.
- Verify IAM with gcloud auth list and gcloud projects get-iam-policy before debugging a denied API call.
Interview checkpoint
Hands-on lab
- Identify the current user, groups and UID the way a VM identity works
- Inspect the OS image and kernel as a startup script would
- Enumerate local accounts the way IAM users are listed
- Measure compute capacity for right-sizing decisions
Inspect Local Identity and Compute Like a VM
25 min · beginner
Quick quiz
Which identity is designed for a machine or application to call Google APIs?
IAM roles granted at the project level apply to all resources in that project.
Your new VM is RUNNING but you cannot reach it over SSH. What should you check first?
Cheat sheet
Machine typee2-micro, n2-standard-2 — CPU/RAM sizingImageOS template, e.g. ubuntu-2204-ltsZoneIsolated data center placementInstance groupScale and update VMs as one unitStartup scriptRuns at first bootUser / groupHuman identitiesService accountMachine identity with short-lived credentialsRoleBundle of permissions, e.g. roles/compute.adminPolicy bindingWho gets which role on what resourceFirewall ruleNetwork-layer traffic filter by tag and port