Getting started with Cloud Shell and gcloud

Cloud Shell is a Debian-base virtual machine with a persistent 5GB home directory, which makes it easy to manage GCP projects and resources.

The gcloud command is a part of Google Cloud SDK. You need to download and install it on your own system and initialize it. It’s pre-installed in Google Cloud Shell to access to computing resources hosted on the GCP.

For more in-depth resources, check here
Google Cloud Shell docs
gcloud tool docs

Understanding Regions and Zones

Certain Compute Engine resources live in regions or zones.

A region is a specific geographical location where you can run your resources. Each region has one or more zones. For example, the us-central1 region is a region in the Central United States that contains the zones us-central1-a, us-central1-b, us-central1-c and us-central1-f

Resources that live in a zone are called zonal resources. VM Instances and persistent disks live in a zone. To attach a persistent disk to a virtual machine instance, both resources must be in the same zone. Similarly, if you want to assign a static IP address to an instance, the instance must be in the same region as the static IP.

For more information, check here

To see regions and zone settings for a project from Cloud Shell

gcloud compute project-info describe --project <your-project-id>

gcloud commands

gcloud must be initialized with gcloud init, if used outside of Cloud Shell, before you can use its tools.

Setting environment variables

Define them to save time when writing scripts or APIs

export PROJECT_ID=<your-project-id> # how to create
echo $PROJECT_ID # how to consult

Create a VM with gcloud

This would be the same as creating it through the web browser

gcloud compute instances create gcelab2 --machine-type n1-standard-2 --zone $ZONE

Breaking it into parts:
gcloud compute - enables you to easily manage your Google Compute Engine resources instances create - creates a new instance
gcelab2 - this is the name for your VM
--machine-type n1-standard-2 - sets the machine type to use
--zone $ZONE - sets the zone to the one in your sys vars

Help and config

You may see help for any command with

gcloud -h # short help
gcloud config --help # verbose help
gcloud help config # equivalent to --help

View configurations and components

gcloud config list # view list of configurations for your environment
gcloud config list --all # to check how other properties are set
gcloud components list # lists your components

Interactive mode

gcloud interactive mode has auto-prompting and auto-complete for commands and flags, and displays inline help snippets in the lower section as the command is typed.

gcloud components install beta # install pre-requisites
gcloud beta interactive # enter interactive mode

SSH’ing into a VM

gcloud compute ssh <vm-name> --zone <zone>

Reference(s)

Qwiklabs Getting Started with Cloud Shell & gcloud course