While the GCP Console is great for interacting with GCP through a graphical interface, the gcloud CLI (Command Line Interface) is the primary tool for automating tasks and managing resources from your terminal.
In a nutshell, it allows you to interact with GCP services through text commands instead of clicking through the web interface. The gcloud CLI is also part of the Google Cloud SDK.
Why Use the CLI?¶
Speed: Perform complex tasks with a single command rather than clicking through multiple menus.
Automation: Write scripts to automate repetitive tasks like deploying dozens of virtual machines or storage buckets at once with scripts instead of manual clicking.
Consistency: Ensure that resources are created with the exact same configurations every time.
When To Use the CLI?¶
✅ Automation and scripting: Perfect for repetitive tasks that you don’t want to do by hand.
✅ Reproducible infrastructure setups: Ensure your environment is the same every time you deploy.
✅ Batch operations: Manage multiple resources (like 50 VMs) with a single command.
✅ Version-controlled infrastructure: Save your setup commands in a Git repository.
✅ Integration with other tools: Easily connect your cloud resources with Python scripts, CI/CD pipelines, or Git.
Where to Use It?¶
Cloud Shell: The
gcloudtool is pre-installed and authenticated in the browser-based Cloud Shell terminal we explored in the previous chapter. No installation required.Local Machine: You can install the Google Cloud SDK on your own laptop (Windows, macOS, or Linux) to manage your cloud projects without opening a browser.
Key Capabilities¶
Manage resources: Create, modify, delete resources like VMs, storage, and networks.
Configure projects: Quickly switch between projects or set a default project, region, and zone.
Authenticate: Log in and manage credentials for yourself or for Service Accounts.
Automate workflows: Write Bash or Python scripts to automate repetitive cloud infrastructure tasks.
Query resources: List and filter resources (e.g., “Find all VMs that are currently stopped”) and format the output as JSON, YAML, or a table.
Control permissions: Manage IAM roles and policies to decide who has access to what.
Anatomy of a gcloud Command¶
Most commands follow a simple structure:
gcloud [GROUP] [SUBGROUP] [ACTION] [NAME] --[FLAGS]gcloud: The base command that calls the Google Cloud SDK.[GROUP]: The broad service or resource category you are targeting (e.g.,compute,storage,auth,projects).[SUBGROUP]: (Optional) A specific type of resource within that group. For example, undercomputegroup, you might targetinstances,disks, ornetworks.[ACTION]: The “verb” or what you want to do to the resource (e.g.,create,list,delete,describe,stop).[NAME]: The specific identity of the resource you are acting upon (e.g., the name of your VM instance).--[FLAGS]: Optional parameters that modify the command, usually starting with double dashes. These specify details like--zone,--machine-type, or--format.
Example in Action¶
If you want to create a virtual machine, the command looks like this:
| Part | Command Fragment | Purpose |
|---|---|---|
| Group | compute | We are working with the compute engine service |
| Subgroup | instances | Specifically, we want a virtual machine |
| Action | create | We want to create a new virtual machine instance |
| Name | aims-vm | This is the name we are giving the instance |
| Flag | --zone=us-central1-a | This tells GCP what zone to create the instance |
The full command is:
gcloud compute instances create aims-vm --zone=us-central1-a🔑 Key Takeaways¶
While the console is great for exploring, the CLI is the superior choice for automating repetitive tasks, batch operations, and creating reproducible environments.
You don’t need to install anything immediately; the Cloud Shell provides a fully authenticated, pre-configured
gcloudenvironment directly in your browser.Every command follows a logical structure with
gcloud(the tool), Group (the service), Subgroup (the resource), and Action (the verb).
🚀 What’s Next?¶
We are moving into the practical hands-on phase of the GCP foundations part. In the next two chapters, you will complete structured hands-on tutorials:
Hands-on Tutorial 1: Using the Console
Create a VM: Manually configure and launch a virtual machine using the Compute Engine dashboard.
Browser-based SSH: Connect to your instance directly through the Google Cloud console.
Hands-on Tutorial 2: Using the gcloud CLI
Local Setup: Install and initialize the Google Cloud SDK on your own computer.
Automation: Use
gcloudto create VMs, manage SSH keys from the terminal, and set up a Python environment.Scripting: Write Bash scripts to create and stop multiple VMs at once to manage your credits efficiently.
📚 References & Further Reading¶
gcloud CLI Overview - The official documentation. A great starting point.
gcloud CLI Cheat Sheet - A high-value reference for the most common commands.
Cloud Shell Guide - Deeper details on the browser-based terminal.
SDK Installation Steps - Official guides for Windows, macOS, and Linux.