Google Cloud Platform

Top 5 Power Features of the Google Cloud CLI

If you’re a heavy user of Google Cloud Platform, you probably already know about Google Cloud SDK, the powerful command line tool for working with all things Cloud Platform, available for Windows, Linux and OS X. In this post, the Cloud SDK engineering team, based in New York City, shares their favorite power features of the Google Cloud command-line interface (CLI).

#1. Using cloud service emulators

Whether you’re on your commute without connectivity, want a fast reliable way of running tests, or just want to test your application on your development machine without talking to a remote service, gcloud emulators is your friend. Emulators provide a local mock implementation of Google Cloud services, so you can develop and test core functionality. Currently the Cloud SDK includes Google Cloud Datastore and Google Cloud Pub/Sub emulators, with more to come.

You can start an emulator, such as the emulator for Cloud Datastore, like so:

  $ gcloud beta emulators datastore start
...
[datastore] To connect, set host to http://localhost:8967/datastore
[datastore] Admin console is running at http://localhost:8851/_ah/admin

Now, you’ve got a datastore running on your local machine! The API is available at the port on localhost, listed above.

Client libraries such as gcloud-node, gcloud-ruby, gcloud-python, and gcloud-java can be configured to use this local emulator by respecting the DATASTORE_LOCAL_HOST
environment variable.

The gcloud emulators command has a neat little trick for automatically setting environment variables like this for each service.

  $ $(gcloud beta emulators datastore env-init)
$ echo $DATASTORE_HOST 
http://localhost:8967

The emulator comes with a simple, web-based console which is also available on localhost. Read more in the gcloud emulators documentation.

(Pro tip from Vilas, Engineer on the Cloud SDK)

#2. Type like the wind with autocompletion


Another power feature of the Cloud CLI is tab auto-completion. You can tab-autocomplete gcloud subcommands, and also for many entities such as your instances and zones. Try it out!

(Pro tip from Mark, Engineer on the Cloud SDK)

#3. Using --format to filter, sort and transform output to CSV, JSON, and more


The Cloud CLI gives you a lot of information about your environment, which you might often want to use as input to another script or program. The --format flag provides an easy way to massage the output into a format that makes sense.

Here’s an example using the --format flag to list the zone and IP of your Google Compute Engine instances in CSV format, sorted by zone and name.

  $ gcloud compute instances list \
   --format='csv(zone:sort=1,name:sort=2,networkInterfaces[0].networkIP)' 
   > list.csv

You can then open the CSV file in a viewer such as Google Sheets:

top510w6.PNG

This is just a taste of what --format supports. You can also expose data in JSON and tabular format, and use projections to select, sort and filter your data. Read more in the Google Cloud SDK reference for the --format flag to learn some more neat tricks you can do with --format.

(Pro tip from Glenn, Engineer on the Cloud SDK)

#4. Using the gcloud tool with PowerShell


If you’re a user of PowerShell, then often it’s handy to work with PowerShell objects. Some tweaks to the --format command allow you to do this. For example, if you use this command to list all Compute Engine instances in Asia:

  PS> (gcloud compute instances list --format=json | Out-String | 
ConvertFrom-Json) | ?{$_.zone -match 'asia'} | select name
name
----
asia-ops-1
asia-ops-2

You can use this command to restart all the instances in Asia:

  PS> (gcloud compute instances list --format=json | Out-String |
ConvertFrom-Json) | ?{$_.zone -match ’asia’} | %{gcloud compute instances
reset $_.name --zone $_.zone}

(Pro tip from Valentin, Engineer on the Cloud SDK)

#5. Easily ssh to your Compute Engine instances


The gcloud CLI offers a number of ways to easily use secure shell to access any Linux-based Compute Engine instances you have. For example, you can run:

$ gcloud compute ssh my-instance-name

This command automatically connects you through SSH to any instance that can be accessed publicly. No more SSH keygen or looking up external IPs!

You can go a step further with:

$ gcloud compute config-ssh

This command creates an alias for each of your Compute Engine instances to your ~/.ssh/config file. These aliases are then available to system utilities that also use SSH, such as ssh, scp and sftp. Now you can type commands from your terminal such as:

$ ssh myvm.asia-east1-c.myproject

or

$ sftp myvm.asia-east1-c.myproject

Read more about the gcloud compute ssh command or the gcloud compute config-ssh command
in the documentation.