Introduction to G-Cloud command line tool

In this article I will give a brief introduction about “gcloud” command line tool which can be used to access the Google Cloud Resources.

If this is the first time you are hearing about gcloud you should checkout this post “Create simple virtual machine in Google Cloud Platform” also you might wanna login to your google cloud account because I will start with the basics but then we will go deeper and explore it more so, If you want to see all the stuff in real time and remember it for lifetime you should run the commands that I will show you here.

In Google Cloud Platform you have the ability to access almost all the 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)

Our focus for this article is gcloud Let’s get started

What is gcloud ?

GCloud is a command line tool available from google that we 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, you can even set triggers to run those scripts but we will save those details for future.

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 will provide you access to for generally available commands by default. Now, you might be thinking that what are generally available commands. This is the release level of gcloud commands and yes there are more than one level.

Levels of gcloud commands

  1. General Availability
  2. Beta
  3. Alpha
  4. Preview
Commands at General Availability level are considered as fully stable and production ready.
Commands at Beta level are fully functional but may still have some issues and thus they are not covered in any SLA(Service level Agreement)
Commands at Alpha level are in early release and may change without notice
Commands at Preview level may be unstable may change without notice

Continuing on how to access the gcloud tool If you don’t want to install the SDK now you can use the Google Command Shell. It 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 when you need them.

Start Exploring

First of all 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. I will be showing you more features of Google Cloud Shell in future but for now lets focus on gcloud .

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

Let’s start with my 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 from this that 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 Deep

We will be exploring compute group as we have prior understanding of instances from this post. You will find that running these commands is way cooler than controlling things from console as these commands are very straight forward and we can always look into help manual of command. Start by running

gcloud compute -h

Output will be something like

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

You can see that compute command is also divided into further groups. Lets compare these groups from the compute section on Google Cloud console.

You will notice that most of the groups available in the command output are same as they are available in console.

I will show you how easy is to create a virtual machine even if you don’t know any command using gcloud. I will use only the help command and will follow the documentation available in the help manual of that command.

I know the basic part that instances are part of compute right. So, I will run

gcloud compute --help

This will give me more verbose information about command and in that information I can see that there is 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.
........

So, now I will move inside that group and run the help command again

gcloud compute instances --help

Now I will be seeing a bunch of commands that can be executed on this group like start, stop, attach-disk, delete, create etc. and I can also see the description of each command to avoid any confusion. So, I notice there is a command named “create”

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.
..........

So, I will now run

gcloud compute instances create --help

Output for above command will be something like

I can see the exact format that gcloud expects from me and all the possible parameters that I can pass to it. I can even look into the description provided below with information of all the available flags and there possible values

So, I will 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

I can also verify the instance creation from the console.

Now look at the command that we used carefully and see how clearly we are telling google cloud that I want to use its compute service for creating instance with name my-instance inside zone us-central1-c.

Believe me almost all the commands will be like this and you don’t have to learn any new language or you don’t have to remember anything all you have to do is use common sense and ask for help if required.