A few months ago, I wrote an article about AWS CLI. Today I’m going to explore gcloud, Google Cloud’s CLI.

Installation

The gcloud CLI requires python 3.5 or later. Let’s verify our version will work:

1
python3 --version

If everything is good, we can download the cli:

1
curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-309.0.0-linux-x86_64.tar.gz

Extract it:

1
tar -xvf google-cloud-sdk-309.0.0-linux-x86_64.tar.gz

And install it:

1
./google-cloud-sdk/install.sh

The installation will prompt us to update an rc file. In my case, I updated ~./bashrc. The last step is to source this file:

1
. ~/.bashrc

Finally, we have the gcloud command installed.

Configuring gcloud

For gcloud to be useful, we need to connect it to an actual Google Cloud account. To do this, we need to get a JSON key file from our console. We can do this from the Service Accounts section:

GCP Service Accounts

Select Create Service Account:

GCP Create Service Account

Fill the form:

GCP Service Account Form

Assign permissions:

GCP Service Account Permissions

Click Done:

GCP Service Account Done

Then, we need to go to edit the account and Create new key:

GCP Create new key

And download it:

GCP download JSON key

Once we have the key, we need to pass it to gcloud:

1
gcloud auth activate-service-account --key-file=projectid-123456789.json

To verify the account was registered correctly, we can use this command:

1
gcloud auth list

The output looks like this:

1
2
3
               Credentialed Accounts
ACTIVE  ACCOUNT
*       gcloud@yoyo-1234567.iam.gserviceaccount.com

We can now log into the account:

1
gcloud auth login gcloud@yoyo-1234567.iam.gserviceaccount.com 

Using gcloud

There are a lot of options available on gcloud, a good way to explore them is to use the help:

1
gcloud --help

The output is pretty long. One thing to note is that the GROUPS section holds the different Google Cloud services that can be managed with the CLI:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
GROUPS
    GROUP is one of the following:

     access-context-manager
        Manage Access Context Manager resources.

     bigtable
        Manage your Cloud Bigtable storage.

     builds
        Create and manage builds for Google Cloud Build.

     compute
        Create and manipulate Compute Engine resources.

     config
        View and edit Cloud SDK properties.

     ...

From here, we can use the help to figure out how to use the different services. For example:

1
gcloud compute --help

We can start a new compute instance with this command:

1
gcloud compute instances create my-new-instance --zone=europe-west1-b

Since we didn’t specify a machine type, a default will be used (In my case: n1-standard-1).

Conclusion

In this article I covered how to set up gcloud cli and how to navigate the help to achive our needs. The help menu is organized in a way that makes it easy to find what we are looking for, but if that doesn’t work, there is always the option of asking Google.

[ gcp  productivity  ]