プロジェクトで実行されている VM の概要を表示するには、VM のリストを取得します。このドキュメントでは、すべてのゾーンまたは特定のゾーンの VM のリストを取得する方法について説明します。
始める前に
-
まだ設定していない場合は、認証を設定します。認証とは、Google Cloud サービスと API にアクセスするために ID を確認するプロセスです。ローカル開発環境からコードまたはサンプルを実行するには、次のいずれかのオプションを選択して Compute Engine に対する認証を行います。
Select the tab for how you plan to use the samples on this page:
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
-
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
- Set a default region and zone.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
- Install the Google Cloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
Go
ローカル開発環境でこのページの Go サンプルを使用するには、gcloud CLI をインストールして初期化し、ユーザー認証情報を使用してアプリケーションのデフォルト認証情報を設定します。
詳細については Set up authentication for a local development environment をご覧ください。
Java
ローカル開発環境でこのページの Java サンプルを使用するには、gcloud CLI をインストールして初期化し、ユーザー認証情報を使用してアプリケーションのデフォルト認証情報を設定します。
詳細については Set up authentication for a local development environment をご覧ください。
Node.js
ローカル開発環境でこのページの Node.js サンプルを使用するには、gcloud CLI をインストールして初期化し、ユーザー認証情報を使用してアプリケーションのデフォルト認証情報を設定します。
詳細については Set up authentication for a local development environment をご覧ください。
Python
ローカル開発環境でこのページの Python サンプルを使用するには、gcloud CLI をインストールして初期化し、ユーザー認証情報を使用してアプリケーションのデフォルト認証情報を設定します。
詳細については Set up authentication for a local development environment をご覧ください。
REST
このページの REST API サンプルをローカル開発環境で使用するには、gcloud CLI に指定した認証情報を使用します。
Install the Google Cloud CLI, then initialize it by running the following command:
gcloud init
詳細については、 Google Cloud 認証ドキュメントの REST を使用して認証するをご覧ください。
-
VM のリストを取得する
Google Cloud コンソールで、[VM インスタンス] ページに移動します。
プロジェクトのすべてのゾーンのすべての VM を集計したリストは、gcloud compute instances list
コマンドを使用して取得します。
gcloud compute instances list
VM のリストを特定のゾーンに絞り込むには、上記のコマンドで --zones
フラグを使用します。
次の例では、プロジェクト内のすべてのゾーンのインスタンスのリストを取得します。
このサンプルを試す前に、Compute Engine クイックスタート: クライアント ライブラリの使用に記載されている Go の設定手順に沿って操作します。詳細については、Compute Engine Go API リファレンス ドキュメントをご覧ください。
Compute Engine に対して認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
特定のゾーン内のインスタンスのリストを取得することもできます。
次の例では、プロジェクト内のすべてのゾーンのインスタンスのリストを取得します。
このサンプルを試す前に、Compute Engine クイックスタート: クライアント ライブラリの使用に記載されている Java の設定手順に沿って操作します。詳細については、Compute Engine Java API リファレンス ドキュメントをご覧ください。
Compute Engine に対して認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
特定のゾーン内のインスタンスのリストを取得することもできます。
次の例では、プロジェクト内のすべてのゾーンのインスタンスのリストを取得します。
このサンプルを試す前に、Compute Engine クイックスタート: クライアント ライブラリの使用に記載されている Node.js の設定手順に沿って操作します。詳細については、Compute Engine Node.js API リファレンス ドキュメントをご覧ください。
Compute Engine に対して認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
特定のゾーン内のインスタンスのリストを取得することもできます。
次の例では、プロジェクト内のすべてのゾーンのインスタンスのリストを取得します。
このサンプルを試す前に、Compute Engine クイックスタート: クライアント ライブラリの使用に記載されている Python の設定手順に沿って操作します。詳細については、Compute Engine Python API リファレンス ドキュメントをご覧ください。
Compute Engine に対して認証を行うには、アプリケーションのデフォルト認証情報を設定します。詳細については、ローカル開発環境の認証を設定するをご覧ください。
特定のゾーン内のインスタンスのリストを取得することもできます。
プロジェクト内のすべてのゾーンにあるすべての VM の集計リストは、instances.aggregatedList
メソッドを使用して取得します。
GET https://compute.googleapis.com/compute/v1/projects/PROJECT /aggregated/instances
PROJECT
は、VM のリストを取得するプロジェクトの名前に置き換えます。
VM のリストを特定のゾーンに絞り込むには、instances.list
メソッドを使用します。
GET https://compute.googleapis.com/compute/v1/projects/PROJECT /zones/ZONE /instances
次のように置き換えます。
PROJECT
: VM のリストを取得するプロジェクトの名前ZONE
: VM のリストを取得するゾーン