Google Cloud Series, Part 2: G-Cloud Command Line Tool

In Part One of our Google Cloud (GC) Series, we discussed GC in general, as well as how to create virtual machines. Part Two focuses, specifically, on gcloud Command Line Tool. GC Platform allows you access almost all services in three ways:

  1. Google Cloud Console(Your dashboard)
  2. GCloud(Command line tool available from google)
  3. RestApi(You can also create apps to control your resources)

Let’s get started.

What is gcloud ?

It’s a command line tool you can use to access google cloud services, either using scripts or from command line, and run other automations.

Yes, you can write scripts to spin up the instances according to your requirements, and you can even set triggers to run those scripts–but we will save those details for another day…

How can you access gcloud ?

GCloud is part of the Google Cloud SDK. You have to download and install the SDK on your system and initialize it before you can use gcloud. GCloud provides access to generally available commands by default.

There are several release levels of gcloud commands:

  1. General Availability – Fully stable and production-ready.
  2. Beta – Fully functional but may still have some issues (not covered in SLAs)
  3. Alpha – In early stage release and highly changeable.
  4. Preview – Unstable and may change without notice.

When it comes to access, if you don’t want to install the SDK, you can use the Google Command Shell. This provides you with gcloud command-line access to computing resources hosted on Google Cloud Platform and available through the Google Cloud Platform Console. Cloud Shell is a Debian-based virtual machine with a persistent 5GB home directory, which makes it easy for you to manage your Cloud Platform projects and resources. With Cloud Shell, the Cloud SDK gcloudand other utilities you need are pre-installed and always available.

Start Exploring

Open your cloud console and start Google Command Shell by clicking on the top right icon in the toolbar:

Then click “Start Cloud Shell”. This will take few moments to provision and connect to the environment. When you start Cloud Shell, it provisions a g1-small Google Compute Engine virtual machine running a Debian-based Linux operating system. Cloud Shell instances are provisioned on a per-user, per-session basis. The instance persists while your Cloud Shell session is active and terminates after an hour of inactivity.

After Cloud Shell is activated, you can use the command line to invoke the Cloud SDK gcloud command, or other tools, available on the virtual machine instance.

Getting started with commands

Here’s a favorite command:

gcloud -h

Output of this will be:

Usage: gcloud [optional flags] <group | command>
  group may be           alpha | app | auth | beta | components | compute |
                         config | container | dataflow | dataproc | datastore |
                         debug | deployment-manager | dns | domains |
                         endpoints | firebase | iam | kms | logging | ml |
                         ml-engine | organizations | projects | pubsub |
                         service-management | services | source | spanner |
                         sql | topic
  command may be         docker | feedback | help | info | init | version

For detailed information on this command and its flags, run:
  gcloud --help

You can clearly see all the gcloud commands are divided into groups. Projects, Compute, Container–all of these are different groups, and each group contains commands relevant to that group. Alpha group is used to access alpha level commands and beta group is used to access beta level commands.

Digging Deeper

With prior understanding of instances, it makes sense to explore the compute group. Running these commands is a far better method than controlling them from console. Start by running:

gcloud compute -h

Output will appear as follows:

Usage: gcloud compute [optional flags] <group | command>
  group may be           accelerator-types | addresses | backend-buckets |
                         backend-services | commitments | disk-types | disks |
                         firewall-rules | forwarding-rules | health-checks |
                         http-health-checks | https-health-checks | images |
                         instance-groups | instance-templates | instances |
                         interconnects | machine-types | networks | operations |
                         project-info | regions | routers | routes |
                         shared-vpc | snapshots | ssl-certificates |
                         target-http-proxies | target-https-proxies |
                         target-instances | target-pools | target-ssl-proxies |
                         target-tcp-proxies | target-vpn-gateways | url-maps |
                         vpn-tunnels | xpn | zones
  command may be         config-ssh | connect-to-serial-port | copy-files |
                         reset-windows-password | scp | ssh

Compute command is also divided into further groups. Compare these groups from the compute section on Google Cloud console:

Most of the groups available in the command output are same as they are available in console.

Creating a virtual machine, even if you don’t know any command using gcloud, is possible. Using the help command, you can follow the documentation available in the help manual of that command.

gcloud compute --help

This yields more information about command and presents a group named “instances” that can be used in reading and manipulating compute engine virtual machine instances.

groups:
.......
instance-groups 
        Read and manipulate Google Compute Engine instance groups.     
instance-templates
        Read and manipulate Google Compute Engine   instances             templates.     
instances
        Read and manipulate Google Compute Engine virtual machine instances.     
interconnects
        Read and manipulate Google Compute Engine interconnects.
........

Move inside that group and run the help command again:

gcloud compute instances --help

You’ll see a bunch of commands that can be executed on this group like start, stop, attach-disk, delete, create etc. Additionally, you’ll see the description of each command to avoid any confusion. Observe the “create” command.

COMMANDS
.........
attach-disk 
       Attach a disk to an instance. 
create 
       Create Google Compute Engine virtual machine instances. delete 
       Delete Google Compute Engine virtual machine instances. delete-access-config 
       Delete an access configuration from a virtual machine network interface.
..........

Run:

gcloud compute instances create --help

Output for above command will be something like

The exact format that gcloud expects and all the possible parameters that I can pass to it are visible. It’s possible to look into the description provided below with information of all the available flags and there possible values:

Now run:

gcloud compute instances create my-instance --zone us-central1-c

where my-instance is the name of my instance as specified in the description of the command and I will see something like

Verify the instance creation from the console:

Now look at the command and see how clearly it informs telling google cloud to use its compute service for creating instance with name my-instance inside zone us-central1-c.

Related Articles

Stay-Up-To-Date

Keep in the loop with the latest in emerging technology and Mutual Mobile