Google Cloud CLI Components and Basics of Command Usage

Microsoft 365・Azureカテゴリを表すパンダのイラスト Microsoft 365 / Azure

This article is a technical explanation and implementation example generated using AI. The published code and procedures are based on primary sources, but the author has not verified their operation on actual hardware. Operation may vary depending on the environment and version.

The Google Cloud CLI is a toolset for creating and managing Google Cloud resources. Through the command line, scripts, and other automation processes, you can perform many common platform tasks. This article outlines the overview of the Google Cloud CLI, command structures, release levels, and the mechanics of settings and properties based on primary sources.


Overview of Google Cloud CLI and Main Management Targets

By using the Google Cloud CLI, you can operate various resources on Google Cloud. According to primary sources, it primarily supports the management of the following resources and tasks.

  • Compute Engine virtual machine instances and related resources

  • Cloud SQL instances

  • Google Kubernetes Engine (GKE) clusters

  • Managed Service for Apache Spark clusters and jobs

  • Cloud DNS managed zones and record sets

  • Cloud Deployment Manager deployments

  • App Engine application deployment, authentication management, and local setting customization, among others

When using the Cloud Shell environment, the gcloud CLI is automatically available and manual installation is not required. In other environments, you need to download, install, and initialize it.

flowchart TD
    A["User / Automation Script"] --> B["gcloud CLI"]
    B --> C["Compute Engine"]
    B --> D["Cloud SQL"]
    B --> E["Google Kubernetes Engine"]
    B --> F["Other Resources App Engine / Cloud DNS etc."]

Version Management and Release Levels

Primary sources explain points of caution regarding gcloud CLI versions and command release levels.

About Versions

The current gcloud CLI version at the time of the primary source documentation is 582.0.0. Using the latest version is recommended to utilize the newest features and prevent errors that may occur in older versions. To check the installed version, use the following command.

gcloud version

Release Level Classification

The following release levels are available for gcloud CLI commands. By default, General Availability (GA) level commands are installed.

  • General Availability (none): A fully stable version intended for production use.

  • Preview (preview): A preview version aimed at gathering feedback. It is often excluded from SLAs and support, and usage in test environments is recommended. It stays in this stage for an average of about 6 months.

  • Beta (beta): A stage where features are functionally complete, but some issues may remain. Breaking changes may occur without prior notice.

  • Alpha (alpha): An initial release stage where specifications may change without notice.

alpha, beta, and preview components are not installed by default. To install them individually, use the gcloud components install command. If you run a corresponding command without the appropriate component installed, the gcloud CLI will prompt you to install it.


Command Composition Hierarchy and Groups

Within each release level, gcloud CLI commands are organized into a hierarchical structure of nested command groups. Each group represents a Google Cloud product, feature, or feature subgroup.

  • gcloud compute: General Availability commands for Compute Engine in general

  • gcloud compute instances: General Availability commands related to Compute Engine instances

  • gcloud beta compute: Compute Engine-related commands in Beta

  • gcloud preview: Google Cloud-related commands in Preview

  • gcloud alpha app: App Engine deployment management-related commands in Alpha


Mechanics of Properties and Configurations

Properties and configurations exist as settings that affect the behavior of the gcloud CLI.

Properties

Properties are settings that control the behavior of the gcloud CLI tool. Some properties can be set via global options or command options, with values specified via options taking precedence.

If you want to use accessibility features to optimize screen reader behavior, you can enable the property with the following command.

gcloud config set accessibility/screen_reader true

Configurations

A configuration is a collection of gcloud CLI properties, acting much like a profile. By default, a single configuration named default is used, which suffices for the majority of use cases.

If you want to work by switching between multiple projects and authenticated accounts, you can create and switch multiple configurations. An example of specifying a project within the active configuration is as follows.

gcloud config set project <project-id>

Command Execution Behavior and Input/Output Control

Specifications regarding command execution methods, output, and prompt control are as follows.

How to Write Long Commands

In reference documentation and the like, backslashes () are used to represent long commands. While they can be executed as-is, as a supplementary note [to be verified in Windows environment], you can use a caret (^) instead of in Windows environments. When removing backslashes, line breaks must also be removed simultaneously to read it as a single line.

Standard Output (stdout) and Standard Error (stderr)

The output of successful gcloud CLI commands is written to stdout. All other responses such as prompts, warnings, and errors are output to stderr. Since responses output to stderr are unstable, it is recommended not to reference them from scripts.

Prompts and Non-Interactive Processing

To prevent unintended destructive operations (e.g., gcloud projects delete), the gcloud CLI displays a confirmation prompt before execution. You may also be prompted to select a zone when creating a Compute Engine instance.

If you want to disable prompts for automated execution in scripts and prevent interactive input, use the --quiet (or -q) option. If there is no default value when input is required, an error will occur.

Furthermore, to suppress standard output or standard error output to the terminal, use the --no-user-output-enabled option, and to adjust log verbosity, specify the level with the --verbosity option.

Adjusting Output Structure (Formatting and Filtering)

When a list of resources is returned, it is formatted for human readability and displayed on standard output by default. To make the output better suited to your purpose, the following options are provided.

  • --format: Defines the output structure by specifying tabular, flat, or machine-readable formats (json, csv, yaml, value).

  • Projections: Formats by selecting specific keys from resource data.

  • Filter (--filter): Further narrows down output based on defined criteria.

The distinction between positional arguments and options is also organized: positional arguments define the target entity that the command operates on, while options are used to set variations in the command’s behavior.


References

ライセンス:本記事のテキスト/コードは特記なき限り CC BY 4.0 です。引用の際は出典URL(本ページ)を明記してください。
利用ポリシー もご参照ください。

コメント

Copied title and URL