GCP-101 · Lesson 6 of 12 · 19 min
Interacting with Google Cloud: console, Cloud Shell, gcloud and APIs
▶ Video coming soon A video version of this lesson is on the way.
Owen has spent his first week clicking through the Google Cloud console. It is friendly, and he has learned a lot from it, but on Monday Farah asks him to create the same small set of resources in three projects, exactly the same way, and to write down what he did so that someone else could repeat it. By the second project he understands why she asked. "Clicking is fine for learning and for looking," she says. "For anything we do more than once, we want it written down as commands or code."
Everything in Google Cloud, whether it is created in the console, from a terminal or by a program, goes through the same Google Cloud APIs. The tools differ only in how a person or program talks to those APIs. This lesson tours the four main ways in: the console, Cloud Shell, the Google Cloud CLI and the APIs themselves, with client libraries and infrastructure as code on top.
The Google Cloud console
The Google Cloud console is the web interface at console.cloud.google.com. It shows projects, resources, billing, logs and monitoring in one place, with a project selector at the top that decides which project you are looking at. Most products have their own page, and a search bar finds products, resources and documentation. The console is the easiest way to explore, to see the state of a system at a glance, and to do one-off tasks.
The console has helpful features that Owen relies on. Many creation forms have a button that shows the equivalent command line or REST request for the settings chosen, which is an excellent way to learn gcloud. Dashboards can be customized per project. The Activity view and the logs explorer show recent changes. And recommendations, such as idle resources or oversized VMs, appear beside the resources they concern.
The console's limitation is repeatability. A setting chosen by hand on Tuesday is easy to forget on Thursday, and two people clicking the same form often choose slightly different options. That is why Birchline uses the console mainly for viewing and investigation in production, while changes go through commands, scripts and code that can be reviewed.
Cloud Shell
Cloud Shell is a command-line environment that runs in the browser. Clicking its icon in the console opens a terminal connected to a small temporary virtual machine that Google provides at no charge. It comes with the Google Cloud CLI and common tools already installed, including kubectl, Terraform, Git, Python and Docker, and it is already signed in as the person using the console. There is nothing to install and nothing to configure, which makes it ideal for quick tasks, for following tutorials and for working from a locked-down laptop.
Cloud Shell gives each user a small persistent home directory, 5 GB, that survives between sessions, while the rest of the virtual machine is reset. Files saved in the home directory, such as scripts and configuration, are still there next time; software installed elsewhere on the system is not. Cloud Shell also includes a browser-based code editor for editing files in that home directory. Sessions end after a period of inactivity, so it is not a place to run long jobs.
The Google Cloud CLI
The Google Cloud CLI is the set of command-line tools for Google Cloud. Its main command is gcloud, which manages most Google Cloud resources. Related commands include bq for BigQuery, kubectl for Kubernetes clusters (installed as a component), and gcloud storage for Cloud Storage, which has replaced the older gsutil tool for most tasks. The CLI can be installed on Windows, macOS and Linux, and it is already present in Cloud Shell and on Google's standard VM images.
gcloud commands follow a consistent shape: gcloud, then a product group, then sometimes a subgroup, then a verb, then the resource name and flags. Once Owen saw the pattern, he could often guess commands he had never used, and gcloud's built-in help confirmed them.
gcloud compute instances list
gcloud compute instances create web-test-1 --zone=northamerica-northeast2-a --machine-type=e2-small
gcloud run services describe shop-web --region=northamerica-northeast2
gcloud storage ls gs://bl-photos-prod
gcloud help compute instances create| Part | Example | Meaning |
|---|---|---|
| Product group | compute, run, storage, sql, iam, projects | Which service the command is about |
| Subgroup | instances, disks, firewall-rules | Which kind of resource in that service |
| Verb | list, create, describe, update, delete | What to do |
| Resource name | web-test-1 | Which resource |
| Flags | --zone, --region, --project, --format | Options and where the resource lives |
Configurations and authentication
gcloud keeps settings in configurations, named sets of properties such as the account, the default project, and the default region and zone. A person who works across several projects can create one configuration for each and switch between them, which avoids the classic mistake of running a command against the wrong project. The --project flag on any command overrides the default for that command alone.
gcloud init
gcloud config configurations create shop-dev
gcloud config set project bl-shop-dev
gcloud config set compute/region northamerica-northeast2
gcloud config configurations list
gcloud config configurations activate shop-devBefore gcloud can act, it must know who is calling. gcloud auth login signs a person in through the browser and stores credentials for the CLI. Separately, application default credentials are used by client libraries and tools such as Terraform: on a developer's laptop, gcloud auth application-default login sets them up for local testing; on Google Cloud, a workload uses its attached service account automatically, as lesson 4 explained. Either way, every request carries an identity, and IAM checks it.
Output you can use
gcloud prints human-readable tables by default, but the --format flag can return JSON, YAML, CSV or just the fields you choose. That makes gcloud output easy to feed into scripts and reports. Owen saves a project's VM list as JSON and processes it with a short Python script; here the JSON is included directly so the example runs anywhere.
import json
output = """[
{"name": "web-1", "zone": "zone-a", "machineType": "e2-medium", "status": "RUNNING", "labels": {"env": "dev", "team": "ecommerce"}},
{"name": "web-2", "zone": "zone-b", "machineType": "e2-medium", "status": "TERMINATED", "labels": {"env": "dev", "team": "ecommerce"}},
{"name": "batch-1", "zone": "zone-a", "machineType": "n2-standard-4", "status": "RUNNING", "labels": {"env": "dev"}}
]"""
vms = json.loads(output)
for vm in vms:
missing = {"env", "team"} - set(vm.get("labels", {}))
flag = "missing " + ", ".join(sorted(missing)) if missing else "ok"
print(f"{vm['name']:8} {vm['status']:10} {flag}")web-1 RUNNING ok web-2 TERMINATED ok batch-1 RUNNING missing team
What this code does
A sample list of virtual machines, in the JSON format the command-line tool returns, is read into Python. For each machine the program checks whether the required env and team labels are present. It prints each machine's name and status and flags batch-1 as missing its team label.
In real use the JSON would come from a command such as gcloud compute instances list --format=json. The script finds the VM with a missing team label, the kind of check that helps keep Ravi's cost report clean.
APIs and client libraries
Underneath every tool are the Google Cloud APIs. Each service exposes an API, usually as REST over HTTPS and often also as gRPC, with documented methods such as instances.insert or buckets.list. Requests are authenticated with OAuth 2.0 access tokens tied to a principal. Programs can call the APIs directly, but most use Cloud Client Libraries, which Google provides for languages including Python, Java, Go, Node.js, C#, Ruby and PHP. Client libraries handle authentication, retries and data formats, so developers write a few lines of code instead of raw HTTP requests.
An API must be enabled in a project before the project can use the service, for example the Compute Engine API or the Cloud SQL Admin API. Enabling an API does not by itself cost anything, but it lets the project create billable resources. Some APIs are enabled by default; others are enabled from the console's API Library or with gcloud services enable. Each enabled API also has quotas, such as requests per minute, which lesson 5 introduced.
gcloud services list --enabled
gcloud services enable sqladmin.googleapis.com run.googleapis.comInfrastructure as code
Scripts of gcloud commands are a big step up from clicking, but they describe steps, not results. If a script half-fails, it is not always clear what exists. Infrastructure as code (IaC) describes the desired end state of resources in files, and a tool works out what to create, change or delete to reach it. The files live in version control, changes are reviewed like code, and the same files can build identical development, test and production environments.
Terraform is the most widely used IaC tool for Google Cloud, with Google maintaining the Google Cloud provider. Google also offers Infrastructure Manager, a managed service that runs Terraform configurations in Google Cloud and keeps their state. Birchline's platform team builds folders, projects, networks, IAM grants and organization policies with Terraform, and the shop team defines its Cloud Run services the same way. The console is still open to everyone for viewing, but changes to production come through reviewed pull requests.
resource "google_storage_bucket" "photos" {
name = "bl-photos-prod"
location = "NORTHAMERICA-NORTHEAST2"
uniform_bucket_level_access = true
labels = { env = "prod", team = "ecommerce", system = "shop", cost-centre = "4120" }
}Other ways in
A few more tools round out the picture. The Google Cloud mobile app shows incidents, billing and resource status, and can run some actions from a phone, which the on-call engineer appreciates. Cloud Code extensions bring Google Cloud tools into popular code editors. Gemini Cloud Assist and other AI assistance features in the console can explain resources, suggest commands and help investigate problems; Farah encourages Owen to use them, and to check the suggestions as carefully as anything else he runs against production.
Common mistakes
MistakeRunning a gcloud command against production when you meant development.
InsteadUse named configurations, check the active project, and add --project to commands that change things.
MistakeStoring long-lived keys on laptops so scripts can authenticate.
InsteadUse gcloud auth login for people and attached service accounts or workload identity for code.
MistakeBuilding production by clicking in the console and hoping to remember the settings.
InsteadDescribe resources in Terraform or scripts kept in version control and reviewed.
MistakeInstalling tools into Cloud Shell outside the home directory and expecting them next time.
InsteadOnly the home directory persists; reinstall or script the setup.
Habits for working safely at the command line
Farah gives Owen a short list of habits she learned the hard way. First, read the prompt before pressing Enter: gcloud asks for confirmation before deleting things, and the confirmation names the resource and project, so it is worth reading rather than typing Y by reflex. The --quiet flag skips those prompts, which is useful in scripts that have been tested and dangerous when typed by hand.
Second, make the current context visible. Cloud Shell and many terminals can show the active gcloud configuration and project in the prompt, and Owen sets his production configuration to use a different colour so that he notices when it is active. Third, prefer describe and list commands before update and delete commands: look at the resource first, confirm it is the one you mean, then change it.
Fourth, write scripts so they can run twice without harm. A script that creates a bucket should check whether the bucket already exists, or use a tool that handles that automatically, so that re-running it after a partial failure does not stop with an error halfway through. This property, called idempotence, is one of the main reasons the platform team prefers Terraform for anything permanent.
Finally, keep a record. Commands run in Cloud Shell are not an audit trail, but Cloud Audit Logs are: every change made through gcloud, the console or an API is recorded with the identity that made it. When Owen makes a manual change in an emergency, he writes the exact commands in the incident ticket as well, so that the fix can be turned into code afterwards.
Worked problem
Choosing the right tool
Owen has four tasks: check why a VM is using lots of CPU right now; create the same three buckets with the same labels in five projects; run a quick bq query while on a borrowed laptop; and let the order API upload invoices to a bucket. Which tool suits each?
Show solution
- Investigating a live problem is a viewing task: the console's VM page and monitoring charts are quickest.
- Repeating a configuration across projects should be infrastructure as code, such as a Terraform module applied to each project.
- A quick query on a borrowed laptop suits Cloud Shell, which needs no installation and already has bq.
- An application uploading files should use a Cloud Client Library, authenticating as its attached service account.
Answer: Console, Terraform, Cloud Shell and a client library, respectively.
Summary and key terms
- All Google Cloud tools work through the same APIs, and IAM checks every request.
- The console is best for exploring, viewing and one-off tasks and can show the equivalent command.
- Cloud Shell is a free browser terminal with tools preinstalled and a small persistent home directory.
- The Google Cloud CLI provides gcloud, bq, gcloud storage and kubectl, following a group-verb-resource-flags pattern, with configurations for projects and regions and flexible output formats.
- APIs must be enabled per project and are usually called through client libraries.
- Infrastructure as code, typically Terraform or Infrastructure Manager, describes desired resources in reviewed files.
- Google Cloud console
- The web interface for managing Google Cloud.
- Cloud Shell
- A browser-based terminal on a temporary VM with Google Cloud tools and a persistent home directory.
- Google Cloud CLI
- The command-line tools for Google Cloud, including gcloud and bq.
- gcloud configuration
- A named set of properties such as account, project, region and zone.
- Application default credentials
- The credentials client libraries find automatically in their environment.
- API
- The interface a service exposes for programs to create and manage its resources.
- Client library
- A language-specific package that makes calling Google Cloud APIs easier.
- Infrastructure as code
- Describing resources in files that a tool applies to reach the desired state.
- Terraform
- A widely used infrastructure-as-code tool with a Google Cloud provider.
Practice problems
Work each problem on paper first, then reveal the answer to check.
What persists between Cloud Shell sessions?
Show answer
The home directory (5 GB). The rest of the VM is reset.
How can a person avoid running commands against the wrong project?
Show answer
Use named gcloud configurations, check the active one, and pass --project on commands that change resources.
What must happen before a project can create Cloud SQL instances?
Show answer
The Cloud SQL Admin API must be enabled in the project, and the person needs a suitable IAM role.
Lesson quiz · 5 questions
Score 60% or more to complete this lesson.
Create a free account to save your progress, take quizzes and earn a certificate.