Run Compute Engine VMs and managed instance groups
Create a VM with a dedicated service account and IAP-based admin access, and plan a managed instance group with health checks and autoscaling.
By Invicly Tech, , in Google Cloud Engineering and Services. About 9 minutes to read.
In short
Compute Engine gives you virtual machines on Google's hardware. You choose the size and image, attach a service account that sets what the VM may do, and reach it safely. A managed instance group (MIG) runs many identical VMs and can repair and scale them.
Think of it like this: One VM is a rented workshop. A managed instance group is a row of identical workshops with a supervisor who replaces broken ones and opens more when it gets busy.
Explained simply
Compute Engine lets you rent a virtual machine (VM) from Google. You pick how much CPU and memory it has, which operating system image it starts from, and which zone it lives in. It behaves like a normal Linux or Windows computer.
A VM needs an identity to call other Google Cloud services. That identity is a service account. Give each VM its own account with only the permissions it needs. The default account is easy to use but often has too much access.
You do not need a public IP to log in. Identity-Aware Proxy (IAP) can carry an SSH connection through Google's front door after checking who you are. You then open no SSH port to the whole internet.
When one VM is not enough, use a managed instance group. It builds many identical VMs from a template, replaces the ones that fail a health check, and adds or removes machines when load changes.
Press Play on a diagram to walk through it one step at a time.
- 1Pick machine type and image
For example e2-small and a Debian image family
- 2Create a service account
Grant only the roles the app needs
- 3Create the VM with no external IP
Attach the service account
- 4Allow IAP range on port 22
35.235.240.0/20
- 5SSH through IAP
gcloud compute ssh --tunnel-through-iap
- 1Autoscaler
Adds or removes VMs from a metric such as CPU
- 2Managed instance group
Keeps the target number of VMs running
- 3Health check and autohealing
Recreates VMs that fail the check
- 4Instance template
Machine type, image, disks, service account
- Machine
- Machine type
- Boot image or family
- Disks
- Identity
- Service account
- Scopes
- Network
- Subnet
- Tags
- No external IP
- Startup
- Startup script
- Metadata
In depth
Choosing machine type and image
Machine families group hardware by purpose. General purpose families (such as E2, N2 and newer N4) suit most web and app work. Compute-optimised families suit CPU-heavy jobs. Memory-optimised families suit big in-memory databases. Start with a small general purpose type, measure, then resize. For images, prefer an image family such as debian-12 in the debian-cloud project, because the family always points to the latest patched image in that line. Check which families are current before you pick one. Custom images or Shielded VM options add control for locked-down setups.
- Resize needs a stop in most cases.
- Spot VMs are cheaper but can be stopped by Google.
- Pick the zone near your users and data.
Minimal service accounts
Create a user-managed service account per workload, for example app-vm-sa. Grant it specific roles, such as Storage Object Viewer on one bucket. Attach it when you create the VM. Code on the VM then gets short-lived tokens from the metadata server, with no key files. The scope flag --scopes=cloud-platform is common, because IAM roles then decide what is allowed. Avoid giving Editor to the default Compute Engine service account. Your organization may also enforce policies that stop the default account from receiving broad roles.
- One service account per workload.
- Grant roles on the smallest resource possible.
- Never download keys for a VM; use the metadata server.
Admin access through IAP
With IAP TCP forwarding, gcloud opens a tunnel to the VM through Google. Three things are needed. First, a firewall rule allowing ingress on port 22 from 35.235.240.0/20. Second, the user has roles/iap.tunnelResourceAccessor plus permission to log in to the VM, such as OS Login roles. Third, the command uses the --tunnel-through-iap flag. The VM can have no external IP. This gives you identity checks and audit logs, which plain public SSH does not.
- Your laptop to IAP: Authenticate and request tunnel1Authenticate and request tunnel
- IAP to Your laptop: Check IAM permission, then allow2Check IAM permission, then allow
- IAP to VM (no public IP): Connect from 35.235.240.0/20 to port 223Connect from 35.235.240.0/20 to port 22
- VM (no public IP) to Your laptop: SSH session through the tunnel4SSH session through the tunnel
Health checks and autoscaling in a MIG
A managed instance group uses an instance template, which cannot be edited. To change settings you create a new template and roll it out. Autohealing uses a health check on a port and path. Set the initial delay longer than the app's start time, so new VMs get time to boot. The autoscaler follows a target such as 60 percent CPU, between a minimum and maximum VM count. The cool-down period tells it how long a new VM needs before its metrics count. Regional MIGs spread VMs across zones for resilience.
- 1Measure
Average CPU of the group
- 2Compare to target
For example 60 percent
- 3Add or remove VMs
Within min and max
- 4Wait for cool-down
New VM warms up
- Back to step 1
Learn it in 5 short steps
About 35m each. Track them with a timer in your plan.
- 1Choose machine type and image~35m
- 2Attach a minimal service account~35m
- 3Connect through IAP instead of public SSH~35m
- 4Add an autohealing health check~35m
- 5Autoscale a managed instance group~35m
Key points
- Pick a machine family for the job (general purpose, compute, memory) and start small; you can resize later.
- Give each VM its own service account with minimal roles, not the default Compute Engine service account.
- Use IAP TCP forwarding for SSH so VMs need no public IP or open SSH port.
- A MIG builds identical VMs from an instance template. A health check lets it repair failed VMs.
- An autoscaler changes the VM count from a metric. Set a minimum, a maximum and a cool-down period.
Key terms
Tap a card to flip it. 0/6 known
Examples
gcloud iam service-accounts create app-vm-sa
gcloud compute instances create app-vm --zone=us-central1-a \
--machine-type=e2-small --image-family=debian-12 --image-project=debian-cloud \
--network=lab-vpc --subnet=lab-us --no-address --tags=admin \
--service-account=app-vm-sa@<project-id>.iam.gserviceaccount.com \
--scopes=cloud-platform
gcloud compute ssh app-vm --zone=us-central1-a --tunnel-through-iapA private VM with its own service account, then SSH through IAP. IDs are placeholders.
Create a template and a group with autohealing
You have a web app on port 8080 with a health endpoint at /healthz. Build a template, create a two-VM group, add a health check, and then set autohealing with a 300 second initial delay. Cost: small VMs bill per second while running. Cleanup: delete the group, the template and the health check.
gcloud compute instance-templates create web-tpl --machine-type=e2-small \
--image-family=debian-12 --image-project=debian-cloud \
--network=lab-vpc --subnet=lab-us --no-address --tags=web
gcloud compute health-checks create http web-hc --port=8080 --request-path=/healthz
gcloud compute instance-groups managed create web-mig --zone=us-central1-a \
--template=web-tpl --size=2
gcloud compute instance-groups managed update web-mig --zone=us-central1-a \
--health-check=web-hc --initial-delay=300Turn on autoscaling
Keep between two and five VMs and aim for 60 percent average CPU. Test by running a CPU load on one VM and watching the group size. Evidence is more VMs listed after a few minutes. Rollback: set min and max equal to the size you want, or delete the autoscaler.
gcloud compute instance-groups managed set-autoscaling web-mig \
--zone=us-central1-a --min-num-replicas=2 --max-num-replicas=5 \
--target-cpu-utilization=0.6 --cool-down-period=90
gcloud compute instance-groups managed list-instances web-mig \
--zone=us-central1-aQuick reference
- Create a VM: gcloud compute instances create NAME --zone ZONE --machine-type TYPE.
- Use --no-address for a VM with no external IP.
- SSH through IAP: gcloud compute ssh VM --zone ZONE --tunnel-through-iap.
- IAP source range for the firewall rule: 35.235.240.0/20, port 22.
- IAP role: roles/iap.tunnelResourceAccessor.
- Instance templates are immutable; change by creating a new one.
- Autohealing initial delay must exceed the app start time.
- Autoscaler needs min, max and a utilization target.
Common mistakes
- Running VMs as the default Compute Engine service account, which often has broad permissions.
- Opening port 22 to 0.0.0.0/0 instead of using IAP.
- Setting the autohealing initial delay shorter than the app start time, so VMs are recreated in a loop.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write the command to SSH to a VM called app-vm in us-central1-a with no public IP.
- Exercise 2medium
Create a service account and attach it to a new VM, then check which account the VM uses.
- Exercise 3hard
Plan a MIG for a web app that needs two VMs at minimum and survives a zone outage. List the choices.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Do I need a public IP to log in to a VM?
- No. Use IAP TCP forwarding or OS Login through a private path, and keep the VM without an external IP.
- Is the default Compute Engine service account safe to use?
- It is risky because it often has broad access. Create a dedicated account with minimal roles instead.
- What is the difference between a zonal and a regional MIG?
- A zonal MIG lives in one zone. A regional MIG spreads VMs across zones in a region, so one zone failing does not stop the service.