Show Menu
Cheatography

Google Cloud Cheat Sheet (DRAFT) by

Google Cloud commands I have used in Udemy Course

This is a draft cheat sheet. It is a work in progress and is not finished yet.

GCloud

Gcloud command syntax
gcloud <global flags> <se­rvi­ce/­pro­duc­t> <gr­oup­/ar­ea> <co­mma­nd> <fl­ags> <pa­ram­ete­rs>
global flags format
--project myprojid
OR
--project=myprojid
Set config property
gcloud config set <pr­ope­rty> <va­lue>
Check config property
gcloud config get-value <pr­ope­rty>
Remove config property
gcloud config unset <pr­ope­rty>
Intera­ctive config creation
gcloud init
List all properties in a config­uration
gcloud config list
List all config­ura­tions
gcloud config config­ura­tions list
Make new config­uration
gcloud config config­ura­tions create <Ne­wCo­nfi­gNa­me>
Switch to an existing config
gcloud config config­ura­tions activate <Ne­wCo­nfi­gNa­me>
Specify config in a command
--conf­igu­rat­ion­=IT­S_NAME
ex. gcloud --conf­igu­rat­ion­=ne­wconfig config list
Global Flags
--help, -h, --project <Pr­oje­ctI­D>, --account <Ac­cou­nt>, --filter, --format <JSON, YAML, CSV>, --quite, -q

Config Properties
* Set "­cor­e/a­cco­unt­" or "­acc­oun­t" to replace "­--a­cco­unt­"
* Set "­cor­e/p­roj­ect­" or "­pro­jec­t" to replace "­--p­roj­ect­"
* Set "­com­put­e/r­egi­on" to replace "­--r­egi­on"
* Set "­com­put­e/z­one­" to replace "­--z­one­"

Global Flags

--account
User account to use for invocation
--bill­ing­-pr­oject
GCP Object that will be charged quota for operations performed in gcloud.
Use this flag when you want to use resources from project, but billed to a different project.
--conf­igu­ration
config­uration to be used for this invoca­tion.
--flag­s-file
A YAML or JSON file that specifies a --flag­:value dictionary
--flat­ten­=[K­EY,...]
Flatten output resources slices in KEY into separate records for each item in each slice.
--format
Format for printing command output resources.
--help
Display detailed help
--project
Specify projectId to be used in this invocation
--quiet
Disable all intera­ctive prompts.
--verb­osity
one of debug, info, warning, error, critical, none
--version, -v
Print version and exit
-h
Print summary help and exit.
 

GCloud 2

Get info on a topic
gcloud topic filters

System Admini­str­ation

Get user
whoami
Get Machine name
hostname
Get IP address of current machine
curl api.ip­ify.org
Ping with limited repeti­tions
ping -c 3 myhappyvm
SSH to the instance
gcloud compute ssh myhappyvm
Get info stored by metadata service
curl -H "­Met­ada­ta-­Fla­vor­:go­ogl­e" metada­ta.g­oo­gle.in­ter­nal­/co­mpu­teM­eta­dat­a/v1/
We can ping a Compute instance only using the external IP of the instance.

It is not possible to SSH directly to an instance. Hence use gcloud compute ssh

We can drill down into the Metadata service file system using the Curl Output.

Project

Create a project
gcloud projects create [PROJE­CT_ID] [--fol­der­=FO­LDE­R_ID] [--lab­els­=[K­EY=­VAL­UE,...]­][­--n­ame­=NA­ME]­[--­set­-as­-de­fault]
List all projects for active account
gcloud projects list [--fil­ter­=EX­PRE­SSI­ON]­[--­lim­it=­LIM­IT]­[--­pag­e-s­ize­=PA­GE_­SIZ­E][­--s­ort­-by­=[F­IEL­D,...]­][-­-uri]]
Delete a project
gcloud projects delete PROJEC­T_I­D_O­R_N­UMBER
Print Metadata of a project
gcloud projects describe PROJEC­T_I­D_O­R_N­UMBER
Undelete a project
gcloud projects undelete PROJEC­T_I­D_O­R_N­UMBER
Update name of a project
gcloud projects update PROJECT_ID --name­=NAME
You can add Project Wide Flags at the end of all of the above commands.
--acco­unts, --bill­ing­-pr­oject, --conf­igu­ration, --flag­s-file, --flatten, --format, --help, --impe­rso­nat­e-s­erv­ice­-ac­count, --log-­http, --project, --quiet, --trac­e-t­oken, --user­-ou­tpu­t-e­nabled, --verb­osity

Run $ gcloud help for details

Compon­ents, SDK

List all installed components
gcloud components list
Install auto-c­omp­letion tool
sudo apt-get install google­-cl­oud-sdk
Enable auto-c­omp­letion tool
gcloud beta intera­ctive

PubSub

Create a topic
gcloud pubsub topics create myTopic
List all topics
gcloud pubsub topics list
Delete a topic
gcloud pubsub topics delete Test1
Create a subscr­iption for topic myTopic
gcloud pubsub subscr­iptions create --topic myTopic mySubs­cri­ption
Delete a subscr­iption
gcloud pubsub subscr­iptions delete Test1
Publish a message to a topic
gcloud pubsub topics publish myTopic --message "­Mes­sag­e"
Pull a published message
gcloud pubsub subscr­iptions pull mySubs­cri­ption --auto-ack
Pull all published messages
gcloud pubsub subscr­iptions pull mySubs­cri­ption --auto-ack --limit=3
Once a message is pulled, it is deleted from the queue.
Messages are pulled randomly.

Compute

Create an instance
gcloud compute instances create <my­vm>
Delete an instance
gcloud compute instances delete <my­vm>
Get List of instances
gcloud compute instances list
Get list of machine types
gcloud compute machin­e-types list
Limit list of machine types
gcloud compute machin­e-types list --filt­er=­"­NAM­E:f­1-micro AND ZONE~u­s-w­est­"
SSH to an instance
gcloud compute ssh myhappyvm
gcloud compute ssh will create the SSH keys if it is being run for the first time for an instance.

Services

Get list of services
gcloud services list
Get list of enabled services
gcloud services list --enabled
Get list of available services
gcloud services list --avai­lable
Get list of available services containing "­com­put­e"
gcloud services list --avai­lable | grep compute

Storage

CLI for accessing storage API
gsutil
List all storage buckets
gsutil ls
List objects in a bucket
gsutil ls gs://s­tor­age­-la­b-c­onsole
List all objects in a folder
gsutil ls gs://storage-lab-console/**
This lists all objects in a flat structure
Create Bucket
gsutil mb [-c class] [-l location] [-p proj_id] gs://n­ew-­buc­ket­-name
Get labels for bucket in JSON format
gsutil label get gs://s­tor­age­-la­b-cli/
Add label to bucket
gsutil label ch -l "­ext­ral­abe­l:e­xtr­ava­lue­" gs://s­tor­age­-la­b-cli/
Set labels for a bucket
gsutil label set labels.json gs://s­tor­age­-la­b-cli/
Check status of versioning on bucket
gsutil versioning get gs://s­tor­age­-la­b-cli
Turn on versioning for bucket
gsutil versioning set on gs://s­tor­age­-la­b-cli
Turn off versioning for bucket
gsutil versioning set off gs://s­tor­age­-la­b-cli
Delete a file from bucket
gsutil rm gs://s­tor­age­-la­b-c­li/­REA­DME.txt
Make a file public in a storage bucket
gsutil acl ch -u AllUsers:R gs://s­tor­age­-la­b-c­li/­Sel­fie.jpg
Remove public access for file
gsutil acl ch -d AllUsers gs://s­tor­age­-la­b-c­li/­Sel­fie.jpg

BigQuery

Examine schema of the Shakes­peare table in samples dataset
bq show bigque­ry-­pub­lic­-da­ta:­sam­ple­s.s­hak­espeare
Get help on query command
bq help query
Get list of all commands used by bigquery
bq help
List existing projects in the dataset
bq ls
List datasets in a public dataset project
bq ls bigque­ry-­pub­lic­-data:
Create a dataset
bq mk babynames
Run a query on BigQuery dataset
bq query --use_­leg­acy­=false 'SELECT word FROM ...'
Create or update a table AND load data
bq load <da­tas­et_­nam­e>.<­ta­ble­_na­me> <lo­cal­_fi­le> name:s­tri­ng,­gen­der­:st­rin­g,c­oun­t:i­nteger
List tables in a BQ dataset
bq ls babynames
Examine schema of a dataset table
bq show <da­tas­et_­nam­e>.<­ta­ble­_na­me>
--use_­leg­acy­=false is an optional flag.