Skip to content

Instantly share code, notes, and snippets.

@kylemcdonald
Created July 4, 2024 20:17
Show Gist options
  • Save kylemcdonald/b82bd1539b5729e22bb2e447a2f12d77 to your computer and use it in GitHub Desktop.
Save kylemcdonald/b82bd1539b5729e22bb2e447a2f12d77 to your computer and use it in GitHub Desktop.
List all compute engine instances on Google Cloud.
#!/usr/bin/env bash
# List all projects
projects=$(gcloud projects list --format="value(projectId)")
# Function to check if Compute Engine API is enabled
is_compute_api_enabled() {
local project=$1
gcloud services list --enabled --project="$project" --format="value(config.name)" | grep -q "compute.googleapis.com"
}
# Loop through projects and list compute engine instances if API is enabled
for project in $projects; do
echo "Project: $project"
if is_compute_api_enabled "$project"; then
echo "Compute Engine API is enabled. Listing instances:"
gcloud compute instances list --project="$project" --format="table(name,zone,status)"
else
echo "Compute Engine API is not enabled."
fi
echo ""
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment