Google Cloud CLI in Action: Essential Commands and Use Cases

Managing cloud resources through a browser UI can be slow, repetitive, and error-prone — especially for developers and DevOps engineers who value speed and automation. That’s where the Google Cloud CLI (also known as gcloud) comes in.

The gcloud command-line interface is a powerful tool for managing your Google Cloud Platform (GCP) resources quickly and programmatically. Whether you’re launching VMs, deploying containers, managing IAM roles, or scripting cloud operations, gcloud is your go-to Swiss Army knife.

What is gcloud CLI?

gcloud CLI is a unified command-line tool provided by Google Cloud that allows you to manage and automate Google Cloud resources. It supports virtually every GCP service — Compute Engine, Cloud Storage, BigQuery, Kubernetes Engine (GKE), Cloud Functions, IAM, and more.

It works on Linux, macOS, and Windows, and integrates with scripts, CI/CD tools, and cloud shells.

Why Use Google Cloud CLI?

Here’s what makes gcloud CLI indispensable:

1. Full Resource Control

Create, manage, delete, and configure GCP resources — all from the terminal.

2. Automation & Scripting

Use gcloud in bash scripts, Python tools, or CI/CD pipelines for repeatable, automated infrastructure tasks.

3. DevOps-Friendly

Ideal for provisioning infrastructure with Infrastructure as Code (IaC) tools like Terraform, or scripting deployment workflows.

4. Secure Authentication

Integrates with Google IAM, allowing secure login via OAuth, service accounts, or impersonation tokens.

5. Interactive & JSON Support

Use --format=json to get machine-readable output — perfect for chaining into scripts or parsing with jq.

Installing gcloud CLI

Option 1: Install via Script (Linux/macOS)

curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-cli-XXX.tar.gztar -xf google-cloud-cli-XXX.tar.gz
./google-cloud-sdk/install.sh

Option 2: Install via Package Manager

On macOS (Homebrew):

brew install --cask google-cloud-sdk

On Ubuntu/Debian:

sudo apt install google-cloud-sdk

Option 3: Use Google Cloud Shell

Open Google Cloud Console → Activate Cloud Shell → gcloud is pre-installed.

First-Time Setup

After installation, run:gcloud init

This:

  • Authenticates your account
  • Sets default project and region
  • Configures CLI settings

To authenticate with a service account:

gcloud auth activate-service-account --key-file=key.json

gcloud CLI: Common Commands & Examples

Here are popular tasks you can do with gcloud:

1. Compute Engine (VMs)

List instances:

gcloud compute instances list

Create a VM:

gcloud compute instances create my-vm \  --zone=us-central1-a \
  --machine-type=e2-medium \
  --image-family=debian-11 \
  --image-project=debian-cloud

SSH into a VM:

gcloud compute ssh my-vm --zone=us-central1-a

2. Cloud Storage

List buckets:

gcloud storage buckets list

Create bucket:

gcloud storage buckets create gs://my-new-bucket --location=us-central1

Upload a file:

gcloud storage cp ./file.txt gs://my-new-bucket/

3. BigQuery

List datasets:

gcloud bigquery datasets list

Run a query:

gcloud bigquery query \  "SELECT name FROM \`bigquery-public-data.usa_names.usa_1910_2013\` LIMIT 5"

4. Cloud Functions

Deploy function:


gcloud functions deploy helloWorld \  --runtime=nodejs18 \
  --trigger-http \
  --allow-unauthenticated

Call function:

gcloud functions call helloWorld

5. Kubernetes Engine (GKE)

Get credentials for a cluster:

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

Then you can use kubectl:

kubectl get pods

6. IAM & Permissions

List service accounts:

gcloud iam service-accounts list

Create a new role:

gcloud iam roles create customRole \
  --project=my-project \
  --title="Custom Viewer" \
  --permissions=storage.objects.list

Bind role to user:

gcloud projects add-iam-policy-binding my-project \
  --member=user:you@example.com \
  --role=roles/viewer

Useful Flags

  • --project=PROJECT_ID – override default project
  • --format=json|table|yaml – output formats
  • --quiet – disable prompts
  • --impersonate-service-account=EMAIL – temporary service account access

Advanced Tips & Tricks

Use Profiles (Configurations)

You can switch between different projects or environments using:

gcloud config configurations create dev-env
gcloud config set project my-dev-project
gcloud config configurations activate dev-env

Automate with Scripts

Use bash or Python to wrap commands for CI/CD pipelines:

#!/bin/bash
gcloud auth activate-service-account --key-file=key.json
gcloud functions deploy buildNotifier --source=. --trigger-topic=builds

Export Output to Files

gcloud compute instances list --format=json > instances.json

gcloud CLI vs SDK vs APIs

ToolPurpose
gcloud CLIHuman-readable command-line interface
Client SDKsProgrammatic access via Python, Go, Node.js
REST APIsRaw HTTPS API endpoints for automation
Cloud ShellWeb-based terminal with gcloud pre-installed

You can use them together in complex pipelines or tools.

Useful Links

Final Thoughts

The gcloud CLI is a must-have tool for anyone working with Google Cloud. Whether you’re an SRE managing infrastructure, a developer deploying code, or a data engineer querying BigQuery — gcloud simplifies your workflow and opens the door to powerful automation.

“With gcloud CLI, your terminal becomes your cloud control center.”

Once you learn the basics, you’ll find gcloud indispensable — especially when paired with automation, CI/CD, and Infrastructure as Code.

Comments

Leave a Reply

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