Top 10 Google Cloud CLI Commands Every DevOps Engineer Should Know

Google Cloud CLI Commands for DevOps

Master the top 10 Google Cloud CLI commands every DevOps engineer should know. Boost productivity with real-world examples, command tables, and automation tips.

Table of Contents

Introduction

In the fast-paced world of DevOps, efficiency and automation are paramount. Google Cloud’s Command-Line Interface (CLI), gcloud, is an indispensable tool for DevOps engineers, enabling them to manage resources, automate workflows, and streamline operations. This comprehensive guide delves into the top 10 gcloud commands every DevOps engineer should master, complete with practical examples and best practices.


Google Cloud CLI Commands for DevOps

1. Setting Up Your Project

Before diving into resource management, ensure you’re working within the correct Google Cloud project. The following command sets your active project:

				
					gcloud config set project PROJECT_ID
				
			

Replace PROJECT_ID with your actual project ID. To verify your current configuration, use:

				
					gcloud config list
				
			

2. Managing Compute Resources

Google Compute Engine (GCE) allows you to run virtual machines (VMs) on Google Cloud. Here are some essential commands:

CommandDescription
gcloud compute instances listLists all VM instances
gcloud compute instances create INSTANCE_NAME --zone=ZONECreates a new VM instance
gcloud compute instances delete INSTANCE_NAME --zone=ZONEDeletes a VM instance

Example:

				
					gcloud compute instances create my-vm --zone=us-central1-a
				
			

3. Handling Cloud Storage

Google Cloud Storage is ideal for storing and retrieving any amount of data. Use the gsutil tool for storage operations:

CommandDescription
gsutil mb gs://BUCKET_NAMECreates a new bucket
gsutil cp FILE gs://BUCKET_NAME/Uploads a file to a bucket
gsutil rm gs://BUCKET_NAME/OBJECT_NAMEDeletes an object from a bucket

Example:

				
					gsutil cp myfile.txt gs://my-bucket/
				
			

4. Interacting with Google Kubernetes Engine (GKE)

GKE simplifies the deployment and management of containerized applications. Key gcloud commands include:

CommandDescription
gcloud container clusters listLists all GKE clusters
gcloud container clusters create CLUSTER_NAME --zone=ZONECreates a new GKE cluster
gcloud container clusters get-credentials CLUSTER_NAME --zone=ZONEConfigures kubectl to use the cluster

Example:

				
					gcloud container clusters create my-cluster --zone=us-central1-a
				
			
				
					gcloud container clusters get-credentials my-cluster --zone=us-central1-a
				
			

5. Configuring Identity and Access Management (IAM)

IAM allows you to control who has access to your resources. Essential commands include:

CommandDescription
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME --display-name "DESCRIPTION"Creates a new service account
gcloud projects add-iam-policy-binding PROJECT_ID --member=MEMBER --role=ROLEAssigns a role to a member

Example:

				
					gcloud iam service-accounts create my-service-account --display-name "My Service Account"
				
			
				
					gcloud projects add-iam-policy-binding my-project --member="serviceAccount:my-service-account@my-project.iam.gserviceaccount.com" --role="roles/editor"
				
			

6. Deploying Cloud Functions

Cloud Functions let you run your code in response to events. Deploy a function using:

				
					gcloud functions deploy FUNCTION_NAME --runtime RUNTIME --trigger-http
				
			

Example:

				
					gcloud functions deploy my-function --runtime nodejs14 --trigger-http
				
			

7. Managing Cloud SQL Instances

Cloud SQL provides fully-managed relational databases. Use the following commands to manage instances:

CommandDescription
gcloud sql instances listLists all Cloud SQL instances
gcloud sql instances create INSTANCE_NAME --tier=TIER --region=REGIONCreates a new Cloud SQL instance
gcloud sql instances delete INSTANCE_NAMEDeletes a Cloud SQL instance

Example:

				
					gcloud sql instances create my-sql-instance --tier=db-f1-micro --region=us-central
				
			

8. Monitoring and Logging

Monitoring and logging are crucial for maintaining system health. Use these commands:

CommandDescription
gcloud logging read "LOG_FILTER"Reads log entries based on a specified filter
gcloud logging write LOG_NAME "LOG_ENTRY"Writes a log entry manually (useful for testing or custom logging)
gcloud monitoring metrics listLists available monitoring metrics in your project

Example:

				
					# Read recent error logs from Compute Engine
gcloud logging read "resource.type=gce_instance AND severity>=ERROR" --limit=10

# Write a test log entry
gcloud logging write my-custom-log "This is a test log entry from CLI"

# List available metrics
gcloud monitoring metrics list
				
			

This command helps you quickly pinpoint critical issues by filtering for high-severity logs across VM instances.

9. Automating with Deployment Manager

Google Cloud Deployment Manager allows you to specify all the resources needed for your application in a declarative format using YAML.

CommandDescription
gcloud deployment-manager deployments create DEPLOYMENT_NAME --config=CONFIG_FILECreates a new deployment
gcloud deployment-manager deployments update DEPLOYMENT_NAME --config=CONFIG_FILEUpdates an existing deployment
gcloud deployment-manager deployments delete DEPLOYMENT_NAMEDeletes a deployment

Example:

				
					gcloud deployment-manager deployments create my-deployment --config=config.yaml
				
			

This is especially useful for replicable infrastructure and DevOps automation pipelines.

10. Exporting Resources to Terraform

If you’re transitioning from manual gcloud commands to Infrastructure as Code (IaC) with Terraform, Google Cloud CLI supports exporting your resources:

				
					gcloud beta resource-config bulk-export --resource-format=terraform --path=./tf-export/
				
			

This command automatically generates Terraform configurations for existing GCP resources, accelerating your migration to fully managed IaC workflows.


Summary Table: Top 10 GCP CLI Commands

Use CaseCommand ExampleNotes
Set projectgcloud config set project PROJECT_IDEnsure correct context
Create VMgcloud compute instances create my-vm --zone=us-central1-aLaunch new VM
Upload to Storagegsutil cp file.txt gs://my-bucket/File upload
Create GKE Clustergcloud container clusters create my-cluster --zone=us-central1-aSpin up Kubernetes cluster
IAM Role Bindinggcloud projects add-iam-policy-binding ...Manage permissions
Deploy Functiongcloud functions deploy my-function --runtime nodejs14 --trigger-httpServerless deployment
Create SQL DBgcloud sql instances create my-sql-instance ...Managed DB instance
Read Logsgcloud logging read "severity>=ERROR"Troubleshooting
Deployment Managergcloud deployment-manager deployments create ...Declarative infra
Export to Terraformgcloud beta resource-config bulk-export ...Migrate to Terraform

Common Mistakes to Avoid

MistakeHow to Avoid
Forgetting project contextAlways run gcloud config list before operations
Not using zones or regionsExplicitly specify --zone or --region
Hardcoding secretsUse Secret Manager or environment variables
Ignoring IAM propagation delaysWait a few seconds after policy changes

Final Thoughts

The gcloud CLI is a vital part of any DevOps toolkit on Google Cloud Platform. By mastering these top 10 commands, DevOps engineers can automate workflows, troubleshoot efficiently, and manage infrastructure at scale. Don’t stop here—explore scripting and integration with other tools like Terraform, GitHub Actions, and Google Cloud Build for a full automation pipeline.

Did you find this article helpful? Your feedback is invaluable to us! Feel free to share this post with those who may benefit, and let us know your thoughts in the comments section below.


Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *