このページでは、インスタンス テンプレートを使用して VM インスタンスを作成する方法について説明します。インスタンス テンプレートとは、API リソースの 1 つであり、VM インスタンスのプロパティを定義するものです。インスタンス テンプレートで定義されるプロパティとしては、マシンタイプ、OS イメージ、永続ディスク構成、メタデータ、起動スクリプトなどがあり、そのインスタンス テンプレートを使用して個々の VM インスタンスを作成することも、マネージド インスタンス グループを作成することもできます。
VM インスタンスをインスタンス テンプレートから作成するときのデフォルトの動作では、作成される VM インスタンスはテンプレートで指定されているプロパティと同一です。ただし、VM インスタンス名とそのインスタンスが存在するゾーンを除きます。インスタンスを作成するときに、インスタンス テンプレートで定義したプロパティよりも優先される値を指定することもできるので、用途に応じてインスタンス テンプレートの一部のプロパティを変更できます。
このドキュメントの内容は、使用できる状態のインスタンス テンプレートがあることを前提としています。インスタンス テンプレートがない場合は、手順に従って新しいインスタンス テンプレートを作成してください。
準備
- インスタンス テンプレートのドキュメントを読みます。
- インスタンス テンプレートを作成します。
-
まだ設定していない場合は、認証を設定します。認証とは、 Trusted Cloud by S3NS サービスと API にアクセスするために ID を確認するプロセスです。ローカル開発環境からコードまたはサンプルを実行するには、次のいずれかのオプションを選択して Compute Engine に対する認証を行います。
Select the tab for how you plan to use the samples on this page:
Console
When you use the Trusted Cloud console to access Trusted Cloud by S3NS services and APIs, you don't need to set up authentication.
gcloud
-
After installing the Google Cloud CLI, configure the gcloud CLI to use your federated identity and then initialize it by running the following command:
gcloud init
- Set a default region and zone.
- Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Browser-based sign-in with the gcloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
- Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Browser-based sign-in with the gcloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
- Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Browser-based sign-in with the gcloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
- Install the Google Cloud CLI.
-
Configure the gcloud CLI to use your federated identity.
For more information, see Browser-based sign-in with the gcloud CLI.
-
To initialize the gcloud CLI, run the following command:
gcloud init
-
Create local authentication credentials for your user account:
gcloud auth application-default login
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
Trusted Cloud コンソールで [インスタンスの作成] ページに移動します。
[
VM を作成...] メニューで、[インスタンス テンプレート] を選択します。表示された [テンプレートから VM を作成] ウィンドウで、次の操作を行います。
テンプレートを選択します。
VM を作成して起動するには、[作成] をクリックします。
VM_NAME
: インスタンスの名前。INSTANCE_TEMPLATE_NAME
: 使用するインスタンス テンプレートの名前。リージョン インスタンス テンプレートの場合は、テンプレートの完全な URL または部分的な URL を指定する必要があります。完全な URL の例はhttps://www.googleapis.com/compute/v1/projects/example-project/regions/us-central1/instanceTemplates/example-regional-instance-template
、部分的な URL の例はprojects/example-project/regions/us-central1/instanceTemplates/example-regional-instance-template
になります。Trusted Cloud コンソールで [インスタンスの作成] ページに移動します。
[
VM を作成...] メニューで、[インスタンス テンプレート] を選択します。表示された [テンプレートから VM を作成] ウィンドウでテンプレートを選択し、[
カスタマイズ] をクリックします。省略可: その他の構成オプションを指定します。詳細については、インスタンス作成時の構成オプションをご覧ください。
インスタンスを作成して起動するには、[作成] をクリックします。
- 基本フィールドをオーバーライドすると、インスタンス テンプレートの対応する基本フィールドがリクエストの基本フィールド値に置き換えられます。基本フィールドには、
machineType
、sourceImage
、name
などがあります。 - 繰り返しフィールドをオーバーライドする場合は、そのプロパティのすべての繰り返し値が、リクエストで指定された対応する値で置き換えられます。繰り返しフィールドは一般的に、型が
list
のプロパティです。たとえば、disks
とnetworkInterfaces
は繰り返しフィールドです。 nested object
をオーバーライドする場合は、インスタンス テンプレート内のオブジェクトが、リクエスト内の対応するオブジェクト指定とマージされます。ネストされたオブジェクトが繰り返しフィールド内に存在する場合は、そのフィールドは繰り返しフィールドのルールに従って処理されることに注意してください。ラベルはこのルールの例外であり、その型がobject
であっても繰り返しフィールドとして扱われます。- プリエンプティブ VM インスタンスのドキュメントを参照する。
- シャットダウン スクリプトについて読む。
- プリエンプティブル インスタンスの料金を確認する。
- インスタンスに接続する。
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 に指定した認証情報を使用します。
After installing the Google Cloud CLI, configure the gcloud CLI to use your federated identity and then initialize it by running the following command:
gcloud init
詳細については、 Trusted Cloud 認証ドキュメントの REST を使用して認証するをご覧ください。
インスタンス テンプレートから VM インスタンスを作成する
リージョンまたはグローバルのインスタンス テンプレートを使用して、VM インスタンスを作成できます。インスタンス テンプレートでの定義のとおりにインスタンスを作成するには、次の手順を行います。
コンソール
gcloud
リージョンまたはグローバルのインスタンス テンプレートから VM を作成するには、通常のインスタンスの作成に使用するのと同じ
gcloud compute instances create
コマンドを使用しますが、--source-instance-template
フラグを追加します。gcloud compute instances create VM_NAME \ --source-instance-template INSTANCE_TEMPLATE_NAME
次のように置き換えます。
例:
gcloud compute instances create example-instance \ --source-instance-template my-instance-template
Go
Java
Node.js
Python
REST
リージョンまたはグローバルのインスタンス テンプレートから VM を作成するには、インスタンスを作成する通常のリクエストを作成しますが、
sourceInstanceTemplate
クエリ パラメータを含め、続けてインスタンス テンプレートへの修飾パスを指定します。POST https://compute.googleapis.com/compute/v1/projects/ PROJECT_ID/zones/ZONE/ instances?sourceInstanceTemplate=INSTANCE_TEMPLATE_NAME
リクエストの本文に、VM インスタンスに対する
name
を指定します。{ "name": "example-instance" }
たとえば、次のスニペットには、テンプレートへの完全修飾パス
https://compute.googleapis.com/compute/v1/projects/myproject/global/instanceTemplates/example-instance-template
が含まれています。POST https://compute.googleapis.com/ compute/v1/projects/myproject/zones/us-central1-a/instances?sourceInstanceTemplate= https://compute.googleapis.com/compute/v1/projects/myproject/global/ instanceTemplates/example-instance-template
{ "name": "example-instance" }
オーバーライドを使用してインスタンス テンプレートから VM インスタンスを作成する
インスタンス テンプレートを使用して VM インスタンス起動するときのデフォルトの動作では、作成される VM インスタンスはインスタンス名とゾーンを除いてインスタンス テンプレートに記述されているとおりとなります。
インスタンスを作成するときに、基本的にはインスタンス テンプレートと同じで少しだけ変更したいという場合は、オーバーライド動作を使用できます。オーバーライド動作を使用するには、既存のインスタンス テンプレートの属性のうちオーバーライドしたいものをインスタンスの作成時に渡します。
コンソール
gcloud
gcloud CLI を使用して、
--source-instance-template
フラグを使用してインスタンスを作成するリクエストを作成し、該当するgcloud
フラグを使用して、必要なプロパティをオーバーライドします。使用できるフラグのリストについては、gcloud
のリファレンスをご覧ください。たとえば、インスタンス テンプレートのマシンタイプ、メタデータ、オペレーティング システム、Persistent Disk ブートディスク、セカンダリ ディスクをオーバーライドするには、次のフラグを指定します。
gcloud compute instances create example-instance \ --source-instance-template example-instance --machine-type e2-standard-2 \ --image-family debian-9 --image-project debian-cloud \ --metadata bread=butter --disk=boot=no,name=my-override-disk
Go
Java
Node.js
Python
REST
API で、
sourceInstanceTemplate
クエリ パラメータを使用して、インスタンスを作成するための通常のリクエストを作成するときに、リクエスト本文でオーバーライドするフィールドを指定します。API のオーバーライド動作は、RFC 7396 で記述されている JSON マージパッチ ルールに従います。
詳細は以下のとおりです。
たとえば、あるインスタンス テンプレートで非ブートディスクが 2 つ定義されているものの、このディスクの 1 つをオーバーライドするとします。保存するディスクを含め、
disks
仕様全体をリクエストに含める必要があります。このリクエストの URL:
POST https://compute.googleapis.com/compute/v1/projects/myproject/zones/us-central1-a/instances? sourceInstanceTemplate=https://compute.googleapis.com/compute/v1/projects/myproject/global/instanceTemplates/example-instance-template
リクエスト本文:
{ "disks": [ { # Since you are overriding the repeated disk property, you must # specify a boot disk in the request, even if it is already # specified in the instance template "autoDelete": true, "boot": true, "initializeParams": { "sourceImage": "projects/debian-cloud/global/images/family/debian-8" }, "mode": "READ_WRITE", "type": "PERSISTENT" }, { # New disk you want to use "autoDelete": false, "boot": false, "mode": "READ_WRITE", "source": "zones/us-central1-f/disks/my-override-disk", "type": "PERSISTENT" }, { # Assume this disk is already specified in instance template, but # you must specify it again since you are overriding the disks # property "autoDelete": false, "boot": false, "mode": "READ_WRITE", "source": "zones/us-central1-f/disks/my-other-disk-to-keep", "type": "PERSISTENT" } ], "machineType": "zones/us-central1-f/machineTypes/e2-standard-2", "name": "example-instance" }
次のステップ
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-07-03 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-07-03 UTC。"],[[["Instance templates define the properties of VM instances, such as machine type, OS image, and disk configurations."],["Creating a VM from an instance template results in an identical VM, except for the instance name and zone, although certain fields can be overridden."],["The gcloud CLI and the Google Cloud console, along with several other programing languages, can be used to create VM instances from instance templates."],["Authentication is necessary for accessing Google Cloud services and APIs, and varies slightly depending on whether you are using the console or other methods such as `gcloud` CLI, Go, Java, Node.js, Python, and REST."],["Overriding properties when creating a VM from a template allows customization, but repeated fields in the template will be replaced entirely by the overridden values, necessitating the re-specification of any desired existing values."]]],[]] -