Google Cloud CLI (gcloud) Cheatsheet


Authentication and Configuration

  • Login to Google Cloud
gcloud auth login
  • Login with service account
gcloud auth activate-service-account --key-file=<key-file.json>
  • List authenticated accounts
gcloud auth list
  • Set default project
gcloud config set project <project-id>
  • Get current project
gcloud config get-value project
  • Set default region
gcloud config set compute/region <region>
  • Set default zone
gcloud config set compute/zone <zone>
  • List all configurations
gcloud config list
  • Create new configuration
gcloud config configurations create <config-name>
  • Switch configuration
gcloud config configurations activate <config-name>

Project Management

  • List all projects
gcloud projects list
  • Create a new project
gcloud projects create <project-id> --name="<project-name>"
  • Delete a project
gcloud projects delete <project-id>
  • Get project information
gcloud projects describe <project-id>
  • Enable API service
gcloud services enable <service-name>
  • List enabled services
gcloud services list --enabled

Compute Engine

  • List all VM instances
gcloud compute instances list
  • Create a VM instance
gcloud compute instances create <instance-name> \
  --zone=<zone> \
  --machine-type=<machine-type> \
  --image-family=<image-family> \
  --image-project=<image-project>
  • Start a VM instance
gcloud compute instances start <instance-name> --zone=<zone>
  • Stop a VM instance
gcloud compute instances stop <instance-name> --zone=<zone>
  • Delete a VM instance
gcloud compute instances delete <instance-name> --zone=<zone>
  • SSH into VM instance
gcloud compute ssh <instance-name> --zone=<zone>
  • Get VM instance details
gcloud compute instances describe <instance-name> --zone=<zone>
  • List machine types
gcloud compute machine-types list --zones=<zone>
  • List available images
gcloud compute images list

Cloud Storage

  • List all buckets
gcloud storage buckets list
  • Create a bucket
gcloud storage buckets create gs://<bucket-name>
  • Delete a bucket
gcloud storage buckets delete gs://<bucket-name>
  • Upload file to bucket
gcloud storage cp <local-file> gs://<bucket-name>/
  • Download file from bucket
gcloud storage cp gs://<bucket-name>/<file> <local-path>
  • Sync directory with bucket
gcloud storage rsync <local-directory> gs://<bucket-name>
  • List objects in bucket
gcloud storage ls gs://<bucket-name>
  • Make bucket public
gcloud storage buckets add-iam-policy-binding gs://<bucket-name> \
  --member="allUsers" --role="roles/storage.objectViewer"

Google Kubernetes Engine (GKE)

  • List GKE clusters
gcloud container clusters list
  • Create a GKE cluster
gcloud container clusters create <cluster-name> \
  --zone=<zone> \
  --num-nodes=<node-count>
  • Get cluster credentials
gcloud container clusters get-credentials <cluster-name> --zone=<zone>
  • Resize cluster
gcloud container clusters resize <cluster-name> --num-nodes=<node-count> --zone=<zone>
  • Delete GKE cluster
gcloud container clusters delete <cluster-name> --zone=<zone>
  • List node pools
gcloud container node-pools list --cluster=<cluster-name> --zone=<zone>
  • Create node pool
gcloud container node-pools create <pool-name> \
  --cluster=<cluster-name> \
  --zone=<zone> \
  --num-nodes=<node-count>

App Engine

  • Deploy application
gcloud app deploy
  • Browse deployed application
gcloud app browse
  • View application logs
gcloud app logs tail -s default
  • List versions
gcloud app versions list
  • Set traffic allocation
gcloud app services set-traffic --splits=<version>=<traffic-percentage>
  • Delete version
gcloud app versions delete <version>

Cloud Functions

  • List all functions
gcloud functions list
  • Deploy a function
gcloud functions deploy <function-name> \
  --runtime=<runtime> \
  --trigger-http \
  --source=<source-path>
  • Call a function
gcloud functions call <function-name>
  • Get function details
gcloud functions describe <function-name>
  • View function logs
gcloud functions logs read <function-name>
  • Delete a function
gcloud functions delete <function-name>

Identity and Access Management (IAM)

  • List IAM policies
gcloud projects get-iam-policy <project-id>
  • Add IAM policy binding
gcloud projects add-iam-policy-binding <project-id> \
  --member="user:<email>" \
  --role="<role>"
  • Remove IAM policy binding
gcloud projects remove-iam-policy-binding <project-id> \
  --member="user:<email>" \
  --role="<role>"
  • List service accounts
gcloud iam service-accounts list
  • Create service account
gcloud iam service-accounts create <account-name> \
  --display-name="<display-name>"
  • Create service account key
gcloud iam service-accounts keys create <key-file.json> \
  --iam-account=<service-account-email>
  • List IAM roles
gcloud iam roles list

Networking

  • List VPC networks
gcloud compute networks list
  • Create VPC network
gcloud compute networks create <network-name> --subnet-mode=<mode>
  • List subnets
gcloud compute networks subnets list
  • Create subnet
gcloud compute networks subnets create <subnet-name> \
  --network=<network-name> \
  --range=<cidr-range> \
  --region=<region>
  • List firewall rules
gcloud compute firewall-rules list
  • Create firewall rule
gcloud compute firewall-rules create <rule-name> \
  --allow=<protocol>:<port> \
  --source-ranges=<cidr>
  • List static IP addresses
gcloud compute addresses list
  • Reserve static IP
gcloud compute addresses create <address-name> --region=<region>

Cloud SQL

  • List SQL instances
gcloud sql instances list
  • Create SQL instance
gcloud sql instances create <instance-name> \
  --database-version=<version> \
  --tier=<tier> \
  --region=<region>
  • Connect to SQL instance
gcloud sql connect <instance-name> --user=<username>
  • Create database
gcloud sql databases create <database-name> --instance=<instance-name>
  • List databases
gcloud sql databases list --instance=<instance-name>
  • Export database
gcloud sql export sql <instance-name> gs://<bucket-name>/<file-name> \
  --database=<database-name>

Monitoring and Logging

  • View logs
gcloud logging read "resource.type=<resource-type>"
  • List log entries
gcloud logging entries list --filter="<filter>"
  • Create log-based metric
gcloud logging metrics create <metric-name> --description="<description>" \
  --log-filter="<filter>"
  • List monitoring metrics
gcloud monitoring metrics list
  • Create alerting policy
gcloud alpha monitoring policies create --policy-from-file=<policy-file.yaml>

Billing and Quotas

  • List billing accounts
gcloud billing accounts list
  • Link project to billing account
gcloud billing projects link <project-id> --billing-account=<account-id>
  • List quotas
gcloud compute project-info describe --format="table(quotas[].metric,quotas[].limit,quotas[].usage)"
  • List regions and zones
gcloud compute regions list
gcloud compute zones list

Common Utilities

  • Get gcloud version
gcloud version
  • Update gcloud components
gcloud components update
  • Install additional components
gcloud components install <component-name>
  • List installed components
gcloud components list
  • Get help for command
gcloud <command> --help
  • Set output format
gcloud config set core/format <format>
  • Enable/disable command completion
gcloud info --show-log
  • Clear cache
gcloud config configurations list
  • Show current configuration
gcloud info