Remote access to a Compute Engine VM is the first operation an on-call engineer performs when a workload misbehaves, and the way that connection is established decides the audit story, the failure mode under network outage, and the IAM surface that gates the action. Google Cloud offers four practical access paths to a Linux VM:
- an
gcloud compute sshsession that proxies through Identity-Aware Proxy (IAP) when the instance has no external IP - a direct SSH over an external IP
- the in-browser SSH-in-browser console launcher
- serial console access for emergency recovery
For a Windows VM the equivalent paths are:
- Remote Desktop over an external IP
- RDP through IAP TCP forwarding
- serial console for boot debugging
Choosing among them is a matter of trade-offs between attack surface, audit fidelity, and the failure mode of the network plane.
How it works
The default-and-secure path on a Linux VM is gcloud compute ssh paired with IAP TCP forwarding and OS Login.
IAP TCP forwarding tunnels SSH inside a Google-fronted HTTPS connection, so the VM can sit on a fully private subnet with no external IP, no public DNS, and no inbound firewall rule from 0.0.0.0/0. The only firewall rule the operator needs is an ingress allowing TCP 22 from the IAP source range 35.235.240.0/20.
OS Login then maps the SSH session to the Google identity behind the principal, so every login is tied to a user account governed by IAM rather than to a key pair pasted into project metadata. The principal needs roles/iap.tunnelResourceAccessor on the IAP-protected resource and either roles/compute.osLogin (unprivileged shell) or roles/compute.osAdminLogin (sudo).
The result is that every SSH session shows up in Admin Activity audit logs against a known user, every login is governed by a centrally managed role, and the VM never needs a public IP.
gcloud compute instances create web-01 \
--zone=us-central1-a \
--machine-type=e2-medium \
--subnet=prod-web-subnet \
--no-address \
--metadata=enable-oslogin=TRUE \
--service-account=web-runtime@acme-prod-1234.iam.gserviceaccount.com \
--scopes=cloud-platform
gcloud compute firewall-rules create allow-iap-ssh \
--network=prod-vpc \
--direction=INGRESS \
--action=ALLOW \
--rules=tcp:22 \
--source-ranges=35.235.240.0/20
gcloud compute ssh web-01 --zone=us-central1-a --tunnel-through-iap
The --tunnel-through-iap flag does three things in one shot:
- It opens a local TCP listener bound to a random port.
- It negotiates an end-to-end HTTPS tunnel to the IAP proxy.
- It asks IAP to forward the bytes to TCP 22 on the target instance.
From ssh's perspective the connection is local; from the network's perspective there is no inbound SSH from the public internet at all. Use --internal-ip on the same command when the workstation lives inside the same VPC via VPN or Interconnect and IAP is not required.
In practice
OS Login deserves a careful read. When the project metadata enable-oslogin=TRUE is set, the legacy SSH key path inside instance and project metadata is ignored. Each user's public SSH key is stored in their Cloud Identity profile, and Linux logins resolve to a deterministic POSIX username derived from the user account. Two-factor authentication can be required on top by setting enable-oslogin-2fa=TRUE. Per-instance overrides win over project-level metadata, so a single VM can opt out for a forensic capture while every other instance stays on the centrally governed path.
The legacy alternative is the SSH-keys-in-metadata model where the operator pastes a key into instance or project metadata; the trade-off is that key rotation becomes the operator's problem and audit logging is limited to the act of editing metadata, not to the login itself.
💡 Exam Trap: A scenario that combines OS Login with an SSH key added through
gcloud compute instances add-metadata --metadata-from-file=ssh-keys=...is a distractor. Withenable-oslogin=TRUEthe metadata key is ignored at login time, and the operator must add the key to the user's Cloud Identity profile throughgcloud compute os-login ssh-keys addinstead.
⚠️ Anti-Pattern: Granting
roles/ownerto a developer so they can SSH into a VM. The narrow grant isroles/compute.osLoginplusroles/iap.tunnelResourceAccessor.roles/ownerlets the same developer delete the project's billing link.
Managing Horizontal and Vertical autoscaling configurations
Operational management of autoscaling sits next to remote access because the on-call engineer reaches for both in the same incident. For a managed instance group the autoscaler is configured against an instance group, not a template, so the autoscaling policy can be edited without rebuilding the template.
The four signals are CPU utilization, Application Load Balancer serving capacity, a Cloud Monitoring metric, and a schedule. Multiple signals can be combined, and the autoscaler always picks the highest required instance count among the signals, never the lowest.
gcloud compute instance-groups managed set-autoscaling web-mig \
--region=us-central1 \
--max-num-replicas=12 \
--min-num-replicas=3 \
--target-cpu-utilization=0.6 \
--cool-down-period=90 \
--scale-in-control=max-scaled-in-replicas=2,time-window=600
--scale-in-control rate-limits scale-in so a flapping metric does not strip the fleet below safe capacity; in this case at most two replicas can be removed per ten-minute window. For GKE, autoscaling is split across the Horizontal Pod Autoscaler (HPA), Vertical Pod Autoscaler (VPA), and Cluster Autoscaler.
HPA scales replica counts against CPU, memory, or custom metrics; VPA recommends or applies CPU and memory requests; Cluster Autoscaler then adds nodes when pending Pods cannot be scheduled.
Backing up and restoring database instances
Cloud SQL exposes two backup classes: automated daily backups inside a backup window, and on-demand backups taken before a schema migration or risky maintenance. Both retain up to a configured count, with point-in-time recovery (PITR) available when binary logging is enabled on MySQL or write-ahead logging on PostgreSQL.
gcloud sql backups create --instance=orders-mysql --description="pre-schema-migration"
gcloud sql backups list --instance=orders-mysql
gcloud sql backups restore 1737581234 --restore-instance=orders-mysql
Firestore Native mode supports scheduled exports to Cloud Storage and managed backups with a retention policy from one to fourteen days. A point-in-time recovery from a Firestore backup creates a new database; the original is never overwritten.
📊 Limit: Cloud SQL retains up to 365 automated backups per instance, depending on configuration; Firestore managed backups retain for up to 14 days, with PITR coverage on the past 7 days.
Exporting logs to external systems
Cloud Logging exports use log sinks. A sink has a filter, a destination, and an inclusion or exclusion rule. Destinations supported at the project level are Cloud Logging buckets, BigQuery datasets, Cloud Storage buckets, Pub/Sub topics, and another Google Cloud project's log bucket. An organization-level aggregated sink can collect logs from every project under a folder.
gcloud logging sinks create export-to-bq \
bigquery.googleapis.com/projects/acme-prod-1234/datasets/security_logs \
--log-filter='logName:"cloudaudit.googleapis.com" AND severity>=NOTICE' \
--use-partitioned-tables
The on-premises path is built by writing to a Pub/Sub topic and consuming with a pull subscriber inside the data center, or by writing to a Cloud Storage bucket and pulling with a scheduled job. There is no native push to an arbitrary HTTPS endpoint outside Google Cloud; the Pub/Sub bridge is the canonical pattern.
Configuring audit logs
Audit logs split into Admin Activity, Data Access, System Event, and Policy Denied. Admin Activity and System Event are always on and free. Data Access logging is off by default for most services and must be opted in per service, and it is billed against the project's Cloud Logging volume. The configuration lives in the project's IAM audit configuration.
gcloud projects get-iam-policy acme-prod-1234 --format=json > policy.json
# Edit the auditConfigs block to enable DATA_READ and DATA_WRITE for storage.googleapis.com
gcloud projects set-iam-policy acme-prod-1234 policy.json
🎯 Scenario: A regulator asks who downloaded a sensitive object from a Cloud Storage bucket. Admin Activity logs show the bucket creation and IAM grants, but the GetObject call only shows up after Data Access logging is enabled on
storage.googleapis.comwithDATA_READ. The fix is to update the audit config, replay the request, and confirm the entry in thecloudaudit.googleapis.com%2Fdata_accesslog.
💡 Exam Trap: "Cloud Audit Logs are enabled by default" is a partially correct statement. Admin Activity and System Event are on by default; Data Access and Policy Denied are not. Questions that pivot on which call is or is not visible turn on this distinction.
Choose gcloud compute ssh --tunnel-through-iap plus OS Login when the VM is private and centrally managed. Choose direct SSH over external IP only when a quick lab session is needed and the firewall is scoped tightly. Choose serial console only as a recovery path when the VM cannot boot or SSHd has crashed.
