Overview and What You Will Learn
In this lab, you will use Cloud Shell to run gcloud commands, write a small script that creates several resources in a loop, and use --format and filtering flags to extract exactly the data you need instead of scanning full output manually.
Why This Matters in Production
A team preparing infrastructure for a scheduled load test needs ten identical VMs. Clicking through the Console's VM creation wizard ten times invites naming inconsistencies and configuration mistakes; the same task as a gcloud script with a loop takes seconds, produces identical results every time, and can be reviewed and reused for the next test.
Core Principles
Cloud Shell is a browser-based terminal built directly into the Console with gcloud pre-installed and already authenticated to your account - no local setup needed to start practicing immediately, and it comes with a small amount of persistent storage that survives between sessions.
+------------------------------------------+| You, in Cloud Shell or a local terminal |+------------------------------------------+ | v+------------------------------------------+| gcloud CLI |+------------------------------------------+ | v+------------------------------------------+| Google Cloud APIs (the same APIs the || Console itself calls underneath) |+------------------------------------------+Every action available in the Console ultimately calls the same underlying APIs gcloud calls directly - this is why scripting a task with gcloud produces results identical to doing it manually through the Console, just faster and more consistently repeatable.
Detailed Step-by-Step Practical Lab
Open Cloud Shell from the Console's top navigation bar.
Confirm your active project and account:
gcloud config list- Create a project for this lab:
gcloud projects create gcp-cli-lab-2026 --name="CLI Lab"gcloud config set project gcp-cli-lab-2026gcloud services enable compute.googleapis.com- Write a small script that creates three VMs in a loop, rather than running the same command three times by hand:
for i in 1 2 3; do gcloud compute instances create vm-worker-$i \ --zone=asia-south1-a \ --machine-type=e2-small \ --image-family=debian-12 \ --image-project=debian-clouddone- List all three VMs to confirm the loop worked identically each time:
gcloud compute instances list --filter="name~'vm-worker'"- Use
--formatto extract only the specific fields needed, instead of scanning full output manually:
gcloud compute instances list \ --filter="name~'vm-worker'" \ --format="value(name)"Note
--format="value(field)"uses gcloud's own formatting syntax, conceptually similar to AWS CLI's--query(JMESPath) or Azure CLI's--query. This becomes essential once scripting against dozens of resources where manually parsing full JSON or table output would be slow and error-prone.
- Clean up:
gcloud compute instances delete vm-worker-1 vm-worker-2 vm-worker-3 \ --zone=asia-south1-a --quietgcloud projects delete gcp-cli-lab-2026 --quietProduction Best Practices & Common Pitfalls
Common MistakeUsing the Console's creation wizard to manually create many similar resources one at a time, "since it's just a few." Manual repetition is exactly where inconsistent naming, forgotten configuration options, and copy-paste mistakes creep in - a script produces the same correct result every single time.
TipSave frequently used gcloud commands as small shell scripts in Cloud Shell's persistent storage. Cloud Shell keeps a small file share between sessions specifically so your scripts and tools survive even after you close the browser tab.
- Use
--format="table(...)"while exploring interactively, and--format="value(...)"or--format="json"when scripting. Table output is easier for a human to scan; a value or JSON format is what a script should actually parse programmatically. - Test destructive commands (delete, especially with wildcards or filters) against a throwaway project first. A loop that deletes resources is exactly as fast and exactly as repeatable as a loop that creates them - verify the filter logic somewhere safe before running it against anything real.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud config list |
Show current active project and account |
gcloud config set project |
Set the active default project |
gcloud compute instances list --filter= |
Filter resource lists by name or property |
gcloud compute instances list --format="value(name)" |
Extract just specific fields from output |