Overview and What You Will Learn
The gcloud CLI lets you create, list, filter, and delete Google Cloud resources from a terminal, and Cloud Shell gives you that terminal in your browser with gcloud preinstalled and already signed in. Scripting with gcloud replaces repetitive Console clicks with a command you can review, rerun, and share.
In this lab, you will:
- Open Cloud Shell and check which account and project gcloud is using
- Create a project with billing enabled
- Write a reusable script that creates several VMs and is safe to rerun
- Use
--filterand--formatto pull out exactly the fields you need
Why This Matters in Production
Scripting infrastructure removes the naming mistakes and drift that come from repeating Console wizards by hand. Imagine a performance team at a retailer like Shopify preparing a scheduled Black Friday load test that needs ten identical worker VMs. Ten trips through the creation wizard invite a typo in a machine type or a forgotten label, while a script runs in seconds, gives identical results, and is reviewed and reused for the next test.
Core Principles
Cloud Shell is a free browser-based terminal with gcloud preinstalled and authenticated as your Google account. It includes a persistent home directory (5 GB), so scripts saved under $HOME survive between sessions, while anything stored outside it is lost when the session VM is recycled.
The Console and gcloud are two front ends for the same thing. Both call the same Google Cloud APIs, which is why a gcloud script gives the same result as the wizard, only faster and repeatable.
or local terminal] B --> C[gcloud CLI] C -->|REST calls| D[Google Cloud APIs] E[Cloud Console] -->|Same calls| D D --> F[(Your resources)] classDef person fill:#8B5CF6,color:#FFFFFF classDef tool fill:#326CE5,color:#FFFFFF classDef cloud fill:#10B981,color:#FFFFFF class A person class B,C,E tool class D,F cloud
gcloud output can be shaped in three ways, and choosing the right one is the core skill of this lab:
--filterkeeps only the resources that match a condition, such as a name pattern.--format="table(...)"prints readable columns for humans exploring interactively.--format="value(...)"or--format=jsonprints clean output that a script can parse safely.
Detailed Step-by-Step Practical Lab
How to Open Cloud Shell and Check Your Active Project
Click the Activate Cloud Shell icon in the Console's top navigation bar, then confirm which account and project gcloud is using:
gcloud config listgcloud auth listHow to Create a Project and Enable the Compute API
Compute Engine needs a project with billing linked, so create the project, attach your billing account, and then enable the API. Project IDs are globally unique, so change the suffix if yours is taken.
export PROJECT_ID="gcp-cli-lab-mumbai-2026" # Find your billing account ID (format: 0X0X0X-0X0X0X-0X0X0X)gcloud billing accounts list export BILLING_ACCOUNT_ID="0X0X0X-0X0X0X-0X0X0X" gcloud projects create $PROJECT_ID --name="CLI Lab"gcloud config set project $PROJECT_IDgcloud billing projects link $PROJECT_ID --billing-account=$BILLING_ACCOUNT_IDgcloud services enable compute.googleapis.comCommon MistakeEnabling the Compute API on a project with no billing account fails with a "billing must be enabled" error. Run the
gcloud billing projects linkcommand first, and make sure you hold the Billing Account User role on that billing account.
How to Create Multiple VMs with a gcloud Loop Script
Save the loop as a script in your persistent home directory so you can rerun it for the next test. The count is an argument, so the same file creates three VMs or thirty, and it skips any VM that already exists so a rerun does not fail.
mkdir -p ~/scriptscat > ~/scripts/create-workers.sh << 'EOF'#!/usr/bin/env bash# Stop on the first error, on unset variables, and on failed pipesset -euo pipefail ZONE="asia-south1-a"COUNT="${1:-3}" for i in $(seq 1 "$COUNT"); do NAME="vm-worker-$i" # Skip VMs that already exist so the script is safe to rerun if gcloud compute instances describe "$NAME" --zone="$ZONE" >/dev/null 2>&1; then echo "$NAME already exists, skipping" continue fi gcloud compute instances create "$NAME" \ --zone="$ZONE" \ --machine-type=e2-small \ --image-family=debian-12 \ --image-project=debian-cloud \ --labels=env=loadtest,owner=rahuldoneEOF chmod +x ~/scripts/create-workers.sh~/scripts/create-workers.sh 3TipYou do not always need a loop.
gcloud compute instances create vm-worker-1 vm-worker-2 vm-worker-3 --zone=asia-south1-aaccepts several names and creates them in a single call. Use a loop when each resource needs a different value.
How to Filter gcloud Output by Name or Label
The --filter flag keeps only the matching resources, so you can confirm the loop produced exactly the VMs you expect. The ~ operator matches a regular expression, and labels.key=value matches a label.
gcloud compute instances list --filter="name~'^vm-worker'"gcloud compute instances list --filter="labels.env=loadtest"How to Extract Specific Fields with gcloud --format
Use a table while exploring, then switch to value() when a script needs the data.
# Human-friendly columnsgcloud compute instances list \ --filter="name~'^vm-worker'" \ --format="table(name, zone.basename(), status)" # Just the names, one per line, ready for a scriptgcloud compute instances list \ --filter="name~'^vm-worker'" \ --format="value(name)" # The internal IP of every workergcloud compute instances list \ --filter="name~'^vm-worker'" \ --format="value(networkInterfaces[0].networkIP)"Remember
--formatis gcloud's own output syntax, similar in purpose to the--queryflag in the AWS CLI and Azure CLI. It becomes essential once you script against dozens of resources and cannot afford to parse full table or JSON output by hand.
How to Clean Up Safely
Preview what a filter matches before you destroy anything, then delete.
# Preview first: this must list only the three lab VMsgcloud compute instances list --filter="labels.env=loadtest" --format="value(name)" gcloud compute instances delete vm-worker-1 vm-worker-2 vm-worker-3 \ --zone=asia-south1-a --quiet # Deleting a project stops billing; it stays recoverable for 30 daysgcloud projects delete $PROJECT_ID --quietProduction Best Practices & Common Pitfalls
Common MistakeCreating many similar resources in the Console wizard "since it's just a few." Manual repetition is where inconsistent names, forgotten options, and copy-paste errors creep in, while a script gives the same correct result every time.
TipKeep frequently used commands as small scripts in
~/scripts. Cloud Shell's persistent home directory keeps them after you close the browser tab.
SecurityA loop that deletes resources is exactly as fast as a loop that creates them. Always preview a filter with
listfirst, and test destructive commands in a throwaway project before running them against anything real.
- Use
--format="table(...)"while exploring andvalue(...)orjsonwhen scripting. Tables are easy for humans to scan, while value and JSON formats are stable for programs to parse. - Always put
set -euo pipefailat the top of scripts. Without it, a failed creation in the middle of a loop is silently ignored and later steps run against missing resources. - Label everything a script creates. Labels such as
env=loadtestlet you filter, audit, and clean up by purpose instead of guessing from names. - Make scripts safe to rerun. Check with
describebefore creating, as the lab script does, so a second run does not fail on resources that already exist. - Move to Terraform for long-lived infrastructure. gcloud scripts suit tests, one-offs, and glue, while declarative tools track state and handle drift.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud config list |
Show the active account, project, and defaults |
gcloud config set project PROJECT_ID |
Set the default project for later commands |
gcloud compute instances list --filter="name~'^vm-'" |
Keep only resources matching a pattern |
gcloud compute instances list --format="value(name)" |
Print only chosen fields, one per line |
gcloud topic filters |
Read the full filter syntax built into gcloud |
gcloud topic formats |
Read every supported output format and transform |
gcloud billing projects link PROJECT_ID --billing-account=ID |
Fix "billing must be enabled" errors |