Part 12: GCE Operations: Reliable SSH Access at Scale
AuthorEmmanuel Secretaria
Published Aug 19, 2025
Automate VM access by resolving zones correctly to avoid misrouting and interactive prompts.
Scope inspiration:
gce_ssh.sh, gce_foreach_vm.sh.
This series follows the repo’s GCP inventory flow so every step builds a repeatable, audit-friendly picture of your environment. Part 12 closes out the series with safe, automatable VM access for operations workflows.
What this script does (walkthrough)
gce_ssh.sh wraps gcloud compute ssh so the zone is always correct, even when your config points elsewhere.
- Accept a VM name or IP address and resolve IPs back to instance names.
- Unset inherited
to prevent accidental mis-targeting.CLOUDSDK_COMPUTE_ZONE - Determine the VM zone using
before SSH.gcloud compute instances list - Invoke
with the resolved zone to avoid interactive prompts.gcloud compute ssh
Operational caveats and gotchas
- Inherited
values can cause false “resource not found” errors if the VM is in another zone; the script clears that variable on purpose.CLOUDSDK_COMPUTE_ZONE - If the VM isn’t found, the script prints the current project and region so you can detect misconfigured context quickly.
- You can pass standard
flags (likegcloud compute ssh
) for automation or bulk operations.--command
Example command usage
# SSH to a VM by name with automatic zone resolution gcp/gce_ssh.sh my-vm-name
# SSH to a VM by IP address gcp/gce_ssh.sh 10.0.0.25
# Run a command across a fleet for x in {1..10}; do gcp/gce_ssh.sh vm-$x --command 'sudo systemctl restart myapp'; echo; done