My Avatar

Shoto

Full Stack Engineer

gcloud command on Google Cloud SDK

Posted at 2024/12/232 min to read

gcloud Command

cloud.google.com
gcloud  |  Google Cloud SDK  |  Google Cloud Documentation
gcloud  |  Google Cloud SDK  |  Google Cloud Documentation

initialize

gcloud init

Logging in

gcloud auth login

Logging out

gcloud auth revoke

Project Management

List all available projects:

gcloud projects list

Get the IAM policy for a specific project:

gcloud projects get-iam-policy PROJECT-ID

Set the active project:

gcloud config set project PROJECT-ID

Compute Engine

List all virtual machine instances:

gcloud compute instances list

Create a new virtual machine instance:

gcloud compute instances create INSTANCE-NAME \ --zone=ZONE \ --machine-type=MACHINE-TYPE \ --image=IMAGE-FAMILY \ --image-project=IMAGE-PROJECT

Delete an instance:

gcloud compute instances delete INSTANCE-NAME --zone=ZONE

Cloud Storage

List all buckets:

gcloud storage buckets list

Create a new bucket:

gcloud storage buckets create BUCKET-NAME --location=REGION

Delete a bucket:

gcloud storage buckets delete BUCKET-NAME

Upload a file to a bucket:

gcloud storage cp LOCAL-FILE gs://BUCKET-NAME/

Download a file from a bucket:

gcloud storage cp gs://BUCKET-NAME/FILE-NAME LOCAL-FILE

IAM Management

Add an IAM policy binding to a user:

gcloud projects add-iam-policy-binding PROJECT-ID \ --member="user:EMAIL" \ --role="ROLE"

Remove an IAM policy binding:

gcloud projects remove-iam-policy-binding PROJECT-ID \ --member="user:EMAIL" \ --role="ROLE"

Kubernetes Engine

List all clusters:

gcloud container clusters list

Create a new cluster:

gcloud container clusters create CLUSTER-NAME \ --zone=ZONE \ --num-nodes=NUMBER-OF-NODES

Delete a cluster:

gcloud container clusters delete CLUSTER-NAME --zone=ZONE

Cloud Functions

List all deployed functions:

gcloud functions list

Deploy a new function:

gcloud functions deploy FUNCTION-NAME \ --runtime=RUNTIME \ --trigger-event=EVENT-TYPE \ --trigger-resource=RESOURCE \ --entry-point=FUNCTION-ENTRY-POINT

Delete a function:

gcloud functions delete FUNCTION-NAME

Logs

View logs for a specific project:

gcloud logging read "logName=projects/PROJECT-ID/logs/LOG-NAME"

Stream logs in real-time:

gcloud logging tail