gcloud compute usage tips

This page contains tips that might be useful when you use the gcloud command-line tool to manage your Compute Engine resources. For a complete list of all available gcloud compute flags and commands, you can use the built-in command help (--help) or the published reference documentation, or the gcloud core documentation.

Before you begin

Fetching information about resources

You can fetch information about Compute Engine resources in two ways: using the list command to return a list of resources and using the describe command to return details about one specific resource.

Fetching resources with list commands

The list commands are designed to return a human-readable table of the most relevant data for the requested resources. You can optionally filter your results to return a shorter list with more relevant results.

Regular expression filtering for names
You can use RE2 syntax to match resource names (for example, instance or disk names). Refer to the gcloud compute instances list.
Command Flags
--limit

The maximum number of results to return. This flag is particularly useful when used with the --sort-by flag described in the Fetching resources with describe commands section.

--sort-by SORT_BY

A field to sort by, if applicable. To perform a descending-order sort, prefix the value with a tilde ("~").

Fetching resources with describe commands

The describe commands are designed for displaying data about one resource. You must provide the name of the resource in the describe command. If you can't remember the resource name, you can run a list command to get a list of resources. For example, the following two commands illustrate a scenario when you can list images to get an image name and its associated project so that you can provide these as inputs to a describe command:

gcloud compute images list
NAME                                PROJECT        FAMILY     DEPRECATED STATUS
...
centos-7-v20170620                  centos-cloud   centos-7              READY
...
debian-9-stretch-v20170619          debian-cloud   debian-9              READY
...
gcloud compute images describe debian-9-stretch-v20170619 --project debian-cloud

The default output from describe commands is YAML format, but you can use the --format flag to choose between JSON, YAML, and text output formats. JSON formatted output can be useful if you are parsing the output, while text formatted output puts each property on a separate line.

gcloud compute regions describe us-central1 --format json
{
  "creationTimestamp": "2013-09-06T10:36:54.847-07:00",
  "description": "us-central1",
  "id": "6837843067389011605",
  "kind": "compute#region",
  "name": "us-central1",
  ...
  "status": "UP",
  "zones": [
    "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a",
    "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-b",
    "https://www.googleapis.com/compute/v1/projects/myproject/zones/us-central1-f"
  ]
}

Examples

Examples of list commands

When you list resources, you get an easy-to-read table of summary data. For example, to return summary data about instances in your project, use the instances list command:

gcloud compute instances list
NAME               ZONE          MACHINE_TYPE  INTERNAL_IP    EXTERNAL_IP     STATUS
example-instance   asia-east1-b  e2-standard-2 10.240.95.199  107.167.182.44  RUNNING
example-instance2  us-central1-a e2-standard-2 10.240.173.254 23.251.148.121  RUNNING
test-instance      us-central1-a e2-standard-2 10.240.118.207 23.251.153.172  RUNNING

You can filter results from list commands with regular expressions by including the --filter flag with a key ~ value operator. For example, filter the list of instances to include only the instances with "test" in the instance name:

gcloud compute instances list --filter="name ~ .*test.*"
NAME           ZONE          MACHINE_TYPE  INTERNAL_IP    EXTERNAL_IP     STATUS
test-instance  us-central1-a e2-standard-2 10.240.118.207 23.251.153.17