Google Cloud · CLI guide

gcloud Commands: A Practical Guide to Context, Logs and Troubleshooting

Investigate a Cloud Run service, keep the target explicit, and turn command output into a useful next action.

Begin with the target

Harborlane is the illustrative company in this guide. Its bookings API has an alert. Before changing the service, the engineer checks the active account, project and region, then inspects configuration and logs.

What is the Google Cloud CLI? · Google Cloud Tech, March 2022. Use current command help for syntax.

Use Bash or Cloud Shell and an existing service you are authorized to inspect. Replace every sample value. A command follows a readable pattern:

gcloud run services describe SERVICE_NAME --project=PROJECT_ID --region=REGION

Product → resource → action → scope. describe inspects a service; create, update and delete change resources.

Four questions before a cloud changeConfirm identity and scope, inspect evidence, then choose a result to verify.1Confirm identityWhich account will run the command?2Confirm scopeWhich project, region and resource?3Read the evidenceConfiguration · logs · permissions4Choose and verify the next actionExpected result · recovery for any change
Read the diagram

Before changing a cloud service, confirm which identity is active and which project and region the command will affect. Inspect the current state, then define the result you will verify after the change.

Named configurations organize CLI settings; they do not enforce a production-access boundary.

1. Check account and project

Use Google’s installation instructions or Cloud Shell. For a new local setup, gcloud init walks through authorization and configuration.

gcloud version
gcloud auth list
gcloud config list
gcloud config get-value project
LAB_PROJECT_ID="replace-with-your-lab-project-id"
LAB_REGION="us-central1"
LAB_SERVICE="replace-with-an-existing-service-name"

gcloud projects describe "$LAB_PROJECT_ID" --format="value(projectId)"

Set the real service region; us-central1 is an example. A successful project lookup proves only that you can describe that project.

2. Make saved context recognizable

Named configurations group properties such as account and project. These commands change local CLI settings:

gcloud config configurations list
gcloud config configurations create bookings-lab
gcloud config set project "$LAB_PROJECT_ID"
gcloud config configurations describe bookings-lab

If the name exists, activate it instead of creating it again. Inspect the account and project after switching; explicit flags can override defaults. For unattended automation, design suitable workload identity or permitted service-account impersonation rather than distributing long-lived keys.

3. Check the exact command’s help

gcloud help run
gcloud run services describe --help
gcloud topic filters
gcloud topic formats

Read required arguments, defaults and behavior-changing flags. Follow your installation method for updates; an old tutorial may use different syntax.

4. Locate the service and ready revision

gcloud services list --enabled --project="$LAB_PROJECT_ID"

gcloud run services list --project="$LAB_PROJECT_ID" --region="$LAB_REGION"

gcloud run services describe "$LAB_SERVICE" \
  --project="$LAB_PROJECT_ID" --region="$LAB_REGION" \
  --format="yaml(status.url,status.latestReadyRevisionName,status.conditions)"

Check the service name and ready revision. “Not found” can mean a wrong project, region, name or access level. Enabling an API is a separate cloud change.

No practice service? Follow Google’s container quickstart, including permissions, billing and cleanup.

5. Ask logs a bounded question

gcloud logging read \
  'resource.type="cloud_run_revision" AND severity>=ERROR' \
  --project="$LAB_PROJECT_ID" --freshness=1h --limit=20 --format=json

This returns recent Cloud Run errors across the project. Narrow by service label once you confirm its field and value in a sample entry. Empty output does not prove health: check time range, filters, permissions and whether the expected log was emitted.

Let the error choose the next branch: a missing configuration value and a denied runtime permission need different fixes.

6. Inspect the relevant access

gcloud projects get-iam-policy "$LAB_PROJECT_ID" --format=json

This shows the attached project policy, not all effective access. Inheritance, conditions, resource policies and other controls can matter. Invocation, deployment and runtime permissions are separate questions.

Record the principal, denied permission, resource and command. Request the needed scoped correction. Keep tokens, keys and secret values out of investigation notes.

7. Select and format the evidence

gcloud compute instances list --project="$LAB_PROJECT_ID" \
  --filter="status=RUNNING" --format="table(name,zone,status)"

gcloud compute instances list --project="$LAB_PROJECT_ID" \
  --limit=1 --format=json
gcloud run services describe "$LAB_SERVICE" \
  --project="$LAB_PROJECT_ID" --region="$LAB_REGION" \
  --format="value(status.url)"

Use tables for readers, JSON for programs and value projections for specific fields. Inspect a sample resource before selecting fields. Check missing values and exit status before chaining commands. Filtering does not necessarily reduce underlying API work.

Apply the pattern to related services

A small operational command map
QuestionStarting commandWhat to check next
Which VMs are running?gcloud compute instances listProject, zone, name and status
Which objects are in this bucket?gcloud storage ls gs://BUCKET_NAMEIntended bucket and permitted prefix
Which image was built?gcloud artifacts docker images list REGION-docker.pkg.dev/PROJECT_ID/REPOSITORYTags and immutable digest
What happened to the build?gcloud builds list --region=REGION --limit=10Build ID, result and linked logs
Which secret versions exist?gcloud secrets versions list SECRET_IDExpected version and state, without printing its payload
What is the database configuration?gcloud sql instances describe INSTANCE_NAMERegion, connectivity and relevant settings

Add supported --project and location flags. Cloud Build’s location can be global; use the actual build location.

gcloud container clusters get-credentials updates local kubeconfig and switches the current Kubernetes context. Verify the cluster scope first, then inspect kubectl config current-context. gcloud manages clusters; kubectl manages resources inside them.

Continue with Git to GKE or the production readiness checklist.

Automate an understood result

Keep scope explicit, reject missing inputs and check failures:

#!/usr/bin/env bash
set -euo pipefail
: "${LAB_PROJECT_ID:?Set the approved project ID}"
: "${LAB_REGION:?Set the service region}"
: "${LAB_SERVICE:?Set the service name}"

gcloud run services describe "$LAB_SERVICE" \
  --project="$LAB_PROJECT_ID" --region="$LAB_REGION" --format=json

Before adding a change, define its expected result, authorization and recovery. --quiet suppresses prompts; it is not a dry run.

Narrow a failure before changing anything

  • Authentication: check the active CLI account and session.
  • Authorization: record the denied permission and resource.
  • Not found: check project, location, name and visibility.
  • Invalid flag: compare installed help with current documentation.
  • Connectivity: check the supported network, proxy and trust settings.

Leave an investigation someone can continue

Record the command, sanitized error, service and revision, evidence and next action. The useful outcome is an explained problem and a controlled next step—not a long list of memorized commands.

Official command references

CLI overview · Authentication · Configurations · Scripting · Cheat sheet

Filters · Formats · Cloud Run describe · Logging read · Project IAM policy

Service map references: Compute Engine, Storage, GKE credentials, Artifact Registry, Cloud Build, Secret Manager, Cloud SQL, and enabled APIs.

Leave a Comment

Your email address will not be published. Required fields are marked *