Common GCP code snippets
list active account name
gcloud auth list
for i in range(8):
print(i)
list active project
gcloud config list project
show default settings for compute engine
gcloud compute project-info describe --project qwiklabs-gcp-00-8fce586ced0b
set environment variables
export PROJECT_ID=qwiklabs-gcp-00-8fce586ced0b
export ZONE=us-central1-a
create a virtual machine
gcloud compute instances create gcelab2 --machine-type n1-standard-2 --zone $ZONE
view help for compute create command
gcloud compute instances create --help
view help for gcloud
gcloud -h
view help for config command
gcloud config --help
View list of configurations in current environment
gcloud config list
See what components are available for use
gcloud components list
GCloud has an interactive mode that can be enables for auto completion.
To intstall additional components for auto completion
sudo apt-get install google-cloud-sdk
enter interactive mode
gcloud beta interactive
ssh into virtual machine
gcloud compute ssh gcelab2 --zone $ZONE
change default zone for compute
gcloud config set compute/zone us-central1-c
Big Query
show the details of the shakespeare data in big query
bq show bigquery-public-data:samples.shakespeare
see a list of commands
bq help query
perform a query on big query
bq query --use_legacy_sql=false \
'SELECT
word,
SUM(word_count) AS count
FROM
`bigquery-public-data`.samples.shakespeare
WHERE
word LIKE "%raisin%"
GROUP BY
word'
list datasets in current active project
bq ls
list datasets in specific project
bq ls bigquery-public-data:
make a dataset in current active project
bq mk babynames
create a table
bq load babynames.names2010 yob2010.txt name:string,gender:string,count:integer
query a table
bq query "SELECT name,count FROM babynames.names2010 WHERE gender = 'F' ORDER BY count DESC LIMIT 5"
remove a dataset
bq rm -r babynames
Google Cloud Storage
PROJECT_ID=`gcloud config get-value project`
BUCKET=${PROJECT_ID}-bucket
create a bucket
gsutil mb -c multi_regional gs://${BUCKET}
copy local folder "endpointslambda" into bucket
gsutil -m cp -r endpointslambda gs://${BUCKET}
copy item into a folder in the bucket
gsutil cp gs://$BUCKET/ada.jpg gs://$BUCKET/image-folder/
download object from bucket
gsutil cp -r gs://${BUCKET}/ada.jpg .
list items in a bucket
gsutil ls gs://${BUCKET}/*
rename local file
mv endpointslambda/Apache2_0License.txt endpointslambda/old.txt
delete local file
rm endpointslambda/aeflex-endpoints/app.yaml
sync changes with bucket
gsutil -m rsync -d -r endpointslambda gs://${BUCKET}/endpointslambda
set objects in bucket to be public
gsutil -m acl set -R -a public-read gs://${BUCKET}
set particular object to be pulic
gsutil acl ch -u AllUsers:R gs://$BUCKET/ada.jpg
remove public access on an object
gsutil acl ch -d AllUsers gs://$BUCKET/ada.jpg
upload a file to a bucket and set the storage class to be "nearline"
gsutil cp -s nearline ghcn/ghcn_on_bq.ipynb gs://${BUCKET}
check storage classes of items in bucket
gsutil ls -Lr gs://${BUCKET} | more
view details for a particular file in the bucket
gsutil ls -l gs://$BUCKET/ada.jpg
delete all objects in a bucket
gsutil rm -rf gs://${BUCKET}/*
delete specific object
gsutil rm gs://$BUCKET/ada.jpg
delete bucket
gsutil rb gs://${BUCKET}
Google Cloud Functions
deploy a cloud function
gcloud functions deploy helloWorld \
--stage-bucket qwiklabs-gcp-00-d68a427ec7ac-bucket \
--trigger-topic hello_world \
--runtime nodejs10
For the above there is a file in the current directory called index.js with the follwoing contents
/**
* Background Cloud Function to be triggered by Pub/Sub.
* This function is exported by index.js, and executed when
* the trigger topic receives a message.
*
* @param {object} data The event payload.
* @param {object} context The event metadata.
*/
exports.helloWorld = (data, context) => {
const pubSubMessage = data;
const name = pubSubMessage.data
? Buffer.from(pubSubMessage.data, 'base64').toString() : "Hello World";
console.log(`My Cloud Function: ${name}`);
};
Show the status of a cloud function
gcloud functions describe helloWorld
Read the logs of a cloud function
gcloud functions logs read helloWorld
Networks
create custom network called labnet
gcloud compute networks create labnet --subnet-mode=custom
create a sub-network
gcloud compute networks subnets create labnet-sub \
--network labnet \
--region us-central1 \
--range 10.0.0.0/28
list networks in project
gcloud compute networks list
view network details
gcloud compute networks describe labnet
list subnets in all networks in project
gcloud compute networks subnets list
create firewall rules
gcloud compute firewall-rules create labnet-allow-internal \
--network=labnet \
--action=ALLOW \
--rules=icmp,tcp:22 \
--source-ranges=0.0.0.0/0
view details of a firewall
gcloud compute firewall-rules describe labnet-allow-internal
IAM Permissions
download and run installer to install gcloud
curl https://sdk.cloud.google.com | bash
restart shell to allow use of the newly installed gcloud tool
exec -l $SHELL
start configuring gcloud
gcloud init
Not all components are installed. To see which components are installed run
gcloud components list
Install beta component
gcloud components install beta