Skip to content
Latchkey

gcloud compute instances list: Query VMs in CI

List Compute Engine VM instances, optionally filtered and formatted for scripting.

gcloud compute instances list enumerates VMs in a project. In CI it is most useful with --filter and --format to extract specific fields (like an instance IP) for later steps. Output formatting is the key to using it in automation.

Common flags

  • --filter=EXPRESSION - server- or client-side filter, e.g. status=RUNNING
  • --format=value(name) - emit raw values for scripting
  • --zones=ZONE - restrict to specific zones
  • --project=PROJECT_ID - target project

Example in CI

Capture the external IP of a tagged instance into a variable.

shell
IP=$(gcloud compute instances list \
  --filter="labels.role=web AND status=RUNNING" \
  --format="value(networkInterfaces[0].accessConfigs[0].natIP)")
echo "host=${IP}"

Common errors in CI

  • Compute Engine API has not been used ... or it is disabled - enable compute.googleapis.com
  • Required "compute.instances.list" permission ... denied - SA missing roles/compute.viewer
  • WARNING: The following filter keys were not present - typo in a --filter field name

Key takeaways

  • Lists VM instances; pair --filter with --format=value() for scripting.
  • Use it to pull dynamic values (IPs, names) into later pipeline steps.
  • Enable the Compute API and grant compute.viewer to read instances.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →