Google Cloud Platform Command line cheat sheet

Sarath Tamminana
6 min readJan 13, 2020

Hi All,

As you all know the Significance and Market capture of Google Cloud Platform is increasing day-by-day, so in order to cope-up with the booming technologies we need to have a good understanding & usage of the products and its offering. I strongly believe that when compared to working with interactive console if we work at the command line background we always have an edge when compared to others not only at conceptual side but also at the debugging side.

So i felt to share my collection of commands which helps me in daily service configuration activities and for cracking GCP Cloud Engineer Certification

Here are the list of commands which i would like to share with you all as a reference guide for CLI activities and for GCP Associate Cloud Engineer Certification Exam.

I tried my best to list down the most frequently used commands while interacting with GCP Services using CloudShell, In this post i concentrated mostly on specific set of services like

  1. “Identity and Access Management”
  2. Compute Services like Google Compute Engine,Google Kubernetes Engine,App Engine and Cloud Function
  3. Storage Services like Cloud Storage,BigQuery and Datastore
  4. Data Life Cycle Services like Cloud DataProc and Cloud Pub/Sub

Authorization and Access related commands
1.To authenticate a user account with gcloud and minimal user output
gcloud auth login — brief

2.To list all credentialed accounts and identify the current active account
gcloud auth list

3.To revoke credentials for a user account (like logging out)
gcloud auth revoke
test@gmail.com

4.To authorize gcloud to access Google Cloud Platform using an existing service account while also specifying a project
gcloud auth activate-service-account
test-service-account@google.com — key-file=/path/key.json — project=sampleproject

5.To set an existing account to be the current active account, run:
gcloud config set core/account
your-email-account@gmail.com

6.If you don’t have an existing account, create one using:
gcloud init

7.To list the active account name:
gcloud auth list — filter=status:ACTIVE — format=”value(account)”

8.To list the inactive account names with prefix test:
gcloud auth list — filter=”-status:ACTIVE account:test*” — format=”value(account)”

9.To obtain access credentials for your user account
gcloud auth login

10.To list down the service accounts
gcloud iam service-accounts list

11.To create a role
gcloud iam roles create

12.To list down the roles
gcloud iam roles list

13.To list down the projects
gcloud projects list

14.To describe the roles
gcloud iam roles describe

15.To create a configuration
gcloud config configurations create quantiphi

16.To list down the configurations
gcloud config configurations list

17.To activate the configuration
gcloud config configurations activate quantiphi

18.To set an account
gcloud config set account
quant@quantiphi.com

19.To list down the projects
gcloud projects list

20.To set the projects
gcloud config set project quantiphi-project

21.To list down all the active configurations
gcloud config list

22.To set the region
gcloud config set compute/region us-west1

23.To set the zone
gcloud config set compute/zone us-west1-a

24.To list down the regions
gcloud compute regions list

25.To list down the zones using filter
gcloud compute zones list — filter=region:us-central1

26.To get the IAM policy into an yaml file
gcloud projects get-iam-policy (project_id) > filename.yaml

27.To set the IAM policy using yaml file
gcloud projects set-iam-policy project_id filename.yaml

28.To bind an IAM policy to a specific user
gcloud projects add-iam-policy-binding project_id — member user:
qaunt@quantiphi.com — role roles/editor

Billing
1.To list billing accounts
gcloud beta billing

2.To link a billing account with a project
gcloud alpha billing projects link my-project — billing-account 0X0X0X-0X0X0X-0X0X0X

Compute Service
1.To create a disk
gcloud compute disks create

2.To resize the disks
gcloud compute disks resize

3.To list down the deprecated images
gcloud compute images list/describe/create/deprecate

4.To create a instance disabling auto termination of disk
gcloud compute instances create instance-1 — no-auto-delete — disk exampledisk

5.To create a network in custom mode
gcloud compute networks create ace-exam-vpc1 — subnet-mode=custom

6.To create a network and enabling flow-logs
gcloud beta compute networks subnets create cert-exam-vpc-subnet1 — network=cert-
exam-vpc1 — region=us-west2 — range=10.10.0.0/16 — enable-private-ip-google-
access — enable-flow-logs

7.Expanding the IP address range
gcloud compute networks subnets expand-ip-range cert-exam-subnet1 — prefix-length 16

Google Kubernetes Engine

1.listing down the clusters
gcloud container clusters list

2.Getting details of the cluster
gcloud container clusters describe — zone us-central1-a standard-cluster-1

3.Listing down Kubernetes nodes
kubectl get nodes

4.Listing down the Kubernetes pods
kubectl get pods

5.Describing the nodes
kubectl describe nodes

6.Describing the pods
kubectl describe pods

7.Creating a cluster
gcloud container clusters create example-cluster

8.Resizing the cluster
gcloud container clusters resize standard-cluster-1 — node-pool default-pool — size 5 — region=us-central1

9.Auto-scaling
gcloud container clusters update standard-cluster-1 — enable-autoscaling — min-nodes 1 — max-nodes 5 — zone us-central1-a — node -pool default-pool

10.listing down the deployments
kubectl get deployments

11.Scale the deployments
kubectl scale deployment nginx-1 — replicas 5

12. Auto scaling the deployments
kubectl autoscale deployment nginx-1 — max 10 — min 1 — cpu-percent 80

13.Deleting the deployments
kubectl delete deployment nginx-1

14.List down the services
kubectl get services

15.Running the deployment
kubectl run hello-server — image=gcr.io/google/samples/hello-app:1.0 — port 8080

16.Exposing the deployment
kubectl expose deployment hello-server — type=”LoadBalancer”

17.Deleting the service
kubectl delete service hello-server

18.Listing down the images
gcloud container images list

19.Describing the images properties
gcloud container images describe gcr.io/appengflex-project-1/nginx

App engine
1.Deploying the application on to app engine
gcloud app deploy app.yml

2.Stopping the versions of app-engine
gcloud app versions stop v1 v2

3.Splitting the traffic
gcloud app services set-traffic serv1 — splits v1=.4,v2=.6

Cloud Function
1.To deploy a function with all the specifications
gcloud functions deploy cloud_storage_function_quant \
— runtime python37 \
— trigger-resource gcp-ace-quant-test-bucket \
— trigger-event google.storage.object.finalize

2.Deleting a cloud function
gcloud functions delete

Components
1.List down the components
gcloud components list

2.Update the components
gcloud components update

3.Install the components
gcloud components install <component-name>

CloudSQL
1.Create an SQL database
gcloud sql databases create

2.Connect to the sql
gcloud sql connect ace-exam-mysql –user=root

3.Create a backup
gcloud sql backups create — async — instance ace-exam-mysql

4.Export the data from cloudsql instance
gcloud sql instances export sql quantiphi-mysql1 gs://quantiphi-buckete1/quantiphi-mysqlexport.sql — database=mysql

Cloud PubSub
1.Topic creation
gcloud pubsub topics create [TOPIC-NAME]

2.Subscribtion create
gcloud pubsub subscriptions create [SUBSCRIPTION-NAME] — topic [TOPIC-NAME]

3.Publish a message to specific topic
gcloud pubsub topics publish topic1 — message “Quantiphi-AI/ML”

4.Pull the messages
gcloud pubsub subscriptions pull — auto-ack sub1

Dataproc
1.Creating a cluster
gcloud dataproc clusters create cluster-bc3d — zone us-west2-a

2.Submitting the jobs
gcloud dataproc jobs submit spark — cluster cluster-bc3d — jar Quantiphi.jar

BigQuery
1.Listing down the jobs
bq ls -j -a project

Datastore
1.Creating indexes in Datastore
gcloud datastore create-indexes

2.Export all kinds in the exampleNs namespace in the exampleProject project to the exampleBucket
gcloud datastore export gs://exampleBucket — namespaces=’exampleNs’ — project=’exampleProject’

CloudStorage
1.Lists all your buckets
gsutil ls

2.Help on the topic
gsutil help <topic>

3.Bucket creation
gsutil mb gs://quanti-bucket

4.Deletes the bucket.
gsutil rm gs://<bucket_name>

5.Files
copies the local filename into the bucket
gsutil cp <filename> gs://<bucket_name>/

6.Copies the local filename into the directory
gsutil cp <filename> gs://<bucket_name>/directory/

7.Moves the local src_filename to the directory and renames it as quanti-fi1
gsutil mv <src_filename> gs://<bucket_name>/directory/quanti-fi1

8.Deletes the file_or_dir object.
gsutil rm gs://<bucket_name>/file_or_dir

9.Changing the storage class
gsutil rewrite -s [STORAGE_CLASS] gs://[PATH_TO_OBJECT]

10.Modifying the access control list
gsutil acl ch -u [SERVICE_ACCOUNT_ADDRESS]:W gs://[BUCKET_NAME]

11.Assigning roles
gsutil iam ch user : <user_email>:<role1,role2> gs://<BUCKET>

12.Getting Versioning status
gsutil versioning get gs://bucket

13.Enabling versioning
gsutil versioning set on gs://bucket

14. Life cycle status
gsutil lifecycle get gs://bucket > filename.json

15.Setting the life cycle version
gsutil lifecycle set filename.json gs://bucket

For further reference please go through official GCP Documentation using below link https://cloud.google.com/sdk/gcloud/reference/

Inconvenience regretted for any typo and much appreciated for providing feedback specific to the content of this document.

Sarath Tamminana

--

--

Sarath Tamminana

Certified AWS & GCP Cloud Architect currently working at KPMG as a Assistant Manager. About 10 years of experience in Cloud, DevOps & Middleware Technologies