Command Structure in Gcloud simplified.

The typical Command Structure in Gcloud is “gcloud <group> <subgroup> <action>”. “Group” is usually the service you are working with like, compute, container, config or iam etc… “Subgroup” is the component we are referring to in the group. for example, if we take compute as a group then the instances, images, instance-templates, regions & zones are all subgroups. “action” is the action you want to take like list, create, delete & describe. Now let us look at some examples.

gcloud compute instances create <name> // creates a new compute instance.
gcloud compute instances list // lists the existing compute instances.
gcloud compute instances delete <name> // delete the existing instance.
gcloud compute machine-types list // gives the types of machine types available in gcloud.

The output of these commands can be very lengthy, and we might want to filter it sometimes. for example, below command lists the machine types that are available in that particular zone.

gcloud compute machine-types list --filter zone:asia-southeast2-a

if you want to filter from multiple zones then you would have to put the values in parenthesis & quotations like shown below. This will output all the machine types in both the zones.

gcloud compute machine-types list --filter "zone:(asia-southeast2-a asia-southeast2-c)"

There are multiple options available. for example, if we want to create a machine of certain amount ram, a specific type of CPU & a specific machine type, you can use create and use the below options.

gcloud compute instances create [Name of the Instance] // Command used for creating a compute instance.

This command can be clubbed with other options available which are listed below. It must be noted that are many options available and only a few are listed below.

Name of the parameterThe parameter to be passed
machine type –machine-type
custom cpu –custom-cpu
memory –custom-memory
image or image family–image or –image-family
restart machine on failure–restart-on-failure
Zone–zone=us-central-1b
machine is preemptible–preemptible

refer to this page to view the complete list of options that are available under the gcloud compute instances create command.

Compute instances default region :

Option 1:

There are 3 places in google cloud where you can set a default region. You can define the default region globally under the compute engine settings. You can either configure this using the CLI or from the GUI

gcloud compute project-info add-metadata --metadata=[google-compute-default-region=<region>]
gcloud compute project-info add-metadata --metadata=[google-compute-default-zone=<zone>]
Option 2:

The other option is to set the local Gcloud configuration, we saw this in previous posts.

gcloud config set compute/region <region> // sets the configuration for that particular local gcloud. in other words, we can say specific user.
Option 3:

The third option is to set it specifically for the command. whenever we are executing the command, we can sepcify the –zone & –region values.

it should be noted option three has the highest priority compared to option two followed by option one. So if all the options are set, then option one will be given preference.