cheat-sheets

gcloud (Google Cloud CLI)

The gcloud CLI manages Google Cloud from the terminal. Compare AWS CLI and Azure CLI (az); for infrastructure as code see Terraform. Provider-level notes (free tier, pitfalls, backups): Google Cloud.

Documentation: gcloud reference


Install #

PLATFORMCOMMAND
Debian / Ubuntuadd Google’s apt repository and sudo apt install google-cloud-cli (see the install docs)
macOSbrew install --cask google-cloud-sdk (Homebrew)
Windowsthe installer, or choco install gcloudsdk (Chocolatey)

Sign In and Configuration #

COMMANDDESCRIPTION
gcloud initGuided login and project selection
gcloud auth loginSign in with your user account
gcloud auth login --no-browserSign in on a headless machine
gcloud auth application-default loginCredentials for client libraries and tools such as Terraform
gcloud auth listAccounts
gcloud config set project PROJECT_IDDefault project
gcloud config set compute/region europe-west2Default region (also compute/zone)
gcloud config listCurrent settings
gcloud config configurations create workSeparate named configuration (activate to switch)
gcloud projects listProjects

Compute #

COMMANDDESCRIPTION
gcloud compute instances listList VMs
gcloud compute instances create vm1 --zone=europe-west2-a --machine-type=e2-small --image-family=ubuntu-2204-lts --image-project=ubuntu-os-cloudCreate a VM
gcloud compute ssh vm1 --zone=europe-west2-aSSH (manages keys for you, see OpenSSH Cheat-Sheet)
gcloud compute instances stop|start|delete vm1 --zone=...Power and delete
gcloud compute firewall-rules listFirewall rules
gcloud compute images list --filter="family~ubuntu"Images

Kubernetes (GKE) #

COMMANDDESCRIPTION
gcloud container clusters listList clusters
gcloud container clusters create-auto mycluster --region=europe-west2Autopilot cluster
gcloud container clusters get-credentials mycluster --region=europe-west2Add to kubeconfig (Kubectl)
gcloud container clusters delete mycluster --region=europe-west2Delete

Storage, IAM and Secrets #

COMMANDDESCRIPTION
gcloud storage ls gs://bucketList
gcloud storage cp file gs://bucket/Upload (-r for directories)
gcloud storage rsync ./dir gs://bucket/dirSync
gcloud iam service-accounts listService accounts
gcloud projects add-iam-policy-binding PROJECT --member=user:me@example.com --role=roles/viewerGrant a role
gcloud secrets versions access latest --secret=NAMERead a secret

Formatting and Components #

OPTIONDESCRIPTION
--format=json|yaml|table|"value(name)"Output format (value() is good for scripts)
--filter="status=RUNNING"Server-side filter
--project=IDOverride the project for one command
gcloud components updateUpdate (not available when installed by a package manager; use apt/brew)
gcloud help <command>Help

Not Verified #

Written from knowledge of the CLI and not run against a project. Check gcloud <command> --help, as flags and image families change.

Search all sheets

Tip: / or Ctrl/Cmd+K to open, Esc to close.