gcloud list command example for a quick reference to the options
Table of contents
Background
- To quickly check on the resources deployed in Google Cloud environment, we could use list command in gcloud CLI with various options. In this article we would go through one command with a possible varaiations of common options.
What
- In this example, I assume that an organizantion is creating custom images based on standard images from Google. These images are stored in a specific project and prefixed with 3 letter organization code (org). We need to get the latest 10 custom ubuntu images.
How
I assume that You already have gcloud installed on windows/mac/linux.
Note: In below commands, Please change \ with ^ in case of windows
The command we are going to use is given below and we would go through options line by line.
gcloud compute images list --project "your project" \ --filter="name~'org-ubuntu-*'" \ --sort-by=~creationTimestamp \ --limit=10 \ --format="table[box](name,family,creationTimestamp.date(tz=LOCAL),status)"
To list compute image resources, we use the "compute images list" command along with project name specified using --project option.
gcloud compute images list --project "your project" \
We use the filter option specifiying the resource attribute and matching pattern. Here, we are mathcing image name (name) with starting pattern (org-ubuntu). "~" is the operator to check for match to regular expression.
--filter="name~'org-ubuntu-*'" \
To sort the list of images in descending order, we use the resource attribute Timestamp. "~" is used to specify the order is descending.
--sort-by=~creationTimestamp \
Since, we need only the latest 10 images, we use limit option.
--limit=10 \
For format:
- we are going to print output as a table
- with a box around it.
- We are only selcting the attributes that we are interested in, which are name, family, creationTimestamp and status).
- Finally, we want to show time in local timezone.
--format="table[box](name,family,creationTimestamp.date(tz=LOCAL),status)"
Test
- In the test we are listing the standard images, so as not to share organization specific images.
gcloud compute images list --project bookshelf-project-xxxxx \ --filter="name~'ubuntu-*'" \ --sort-by=~creationTimestamp \ --limit=10 \ --format="table[box](name,family,creationTimestamp.date(tz=UTC),status)" ┌────────────────────────────────────────┬──────────────────────────┬─────────────────────┬────────┐ │ NAME │ FAMILY │ CREATION_TIMESTAMP │ STATUS │ ├────────────────────────────────────────┼──────────────────────────┼─────────────────────┼────────┤ │ ubuntu-minimal-2004-focal-v20220406 │ ubuntu-minimal-2004-lts │ 2022-04-06T06:38:38 │ READY │ │ ubuntu-pro-2004-focal-v20220404 │ ubuntu-pro-2004-lts │ 2022-04-05T01:01:00 │ READY │ │ ubuntu-pro-fips-2004-focal-v20220404 │ ubuntu-pro-fips-2004-lts │ 2022-04-05T01:00:45 │ READY │ │ ubuntu-2004-focal-v20220404 │ ubuntu-2004-lts │ 2022-04-05T00:53:36 │ READY │ │ ubuntu-pro-fips-1804-bionic-v20220331a │ ubuntu-pro-fips-1804-lts │ 2022-04-01T12:12:24 │ READY │ │ ubuntu-pro-1804-bionic-v20220331a │ ubuntu-pro-1804-lts │ 2022-04-01T12:07:39 │ READY │ │ ubuntu-1804-bionic-v20220331a │ ubuntu-1804-lts │ 2022-04-01T11:47:33 │ READY │ │ ubuntu-minimal-2110-impish-v20220331a │ ubuntu-minimal-2110 │ 2022-04-01T10:34:22 │ READY │ │ ubuntu-minimal-1804-bionic-v20220331 │ ubuntu-minimal-1804-lts │ 2022-03-31T17:18:39 │ READY │ │ ubuntu-2110-impish-v20220309 │ ubuntu-2110 │ 2022-03-09T16:20:21 │ READY │ └────────────────────────────────────────┴──────────────────────────┴─────────────────────┴────────┘
Next
- The --format and --filter flags along with projections provide a rich set of variations to produce output using the desired formats.
- You could learn more using the following commands
gcloud topic formats gcloud topic filters
References
Disclaimer
- If you want something quick that worked for another person, please feel free to use the steps in this article at your own discretion
- Please refer to the official documentation for comprehensive and latest steps.
- Content/Opinions shared here belong to the author and do not reflect my current or previous employers.
Support
- If you liked the article or it helped you, please select a reaction (or) write in comments
- Thanks for Your time and support