This page explains how to use an instance template to create a VM instance. An instance template is an API resource that defines the properties of VM instances. You define properties like the machine type, OS image, persistent disk configurations, metadata, startup scripts, and so on, in an instance template and then can use the instance template to create individual VM instances or groups of managed instances.
When you create a VM instance from an instance template, the default behavior is to create a VM instance that is identical to the properties specified in the template, with the exception of the VM instance name and the zone where the instance will live. Alternatively, you can also optionally override certain fields during instance creation if you want to change certain properties of the instance template for specific uses.
This document assumes that you have an instance template ready to use. If you do not have an instance template, follow the instructions to create a new instance template.
Before you begin
- Read the Instance Template documentation.
- Create an instance template.
-
If you haven't already, set up authentication.
Authentication verifies your identity for access to Google Cloud services and APIs. To run
code or samples from a local development environment, you can authenticate to
Compute Engine by selecting one of the following options:
Select the tab for how you plan to use the samples on this page:
Console
When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.
gcloud
-
Install the Google Cloud CLI. After installation, initialize the Google Cloud CLI by running the following command:
gcloud initIf you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
- Set a default region and zone.
Go
To use the Go samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
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.
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.
For more information, see Set up authentication for a local development environment.
Java
To use the Java samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
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.
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.
For more information, see Set up authentication for a local development environment.
Node.js
To use the Node.js samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
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.
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.
For more information, see Set up authentication for a local development environment.
Python
To use the Python samples on this page in a local development environment, install and initialize the gcloud CLI, and then set up Application Default Credentials with your user credentials.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
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.
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.
For more information, see Set up authentication for a local development environment.
REST
To use the REST API samples on this page in a local development environment, you use the credentials you provide to the gcloud CLI.
Install the Google Cloud CLI.
If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.
For more information, see Authenticate for using REST in the Google Cloud authentication documentation.
-
Create a VM instance from an instance template
You can use either a regional or a global instance template to create a VM instance. To create an instance exactly as described in the instance template, follow these instructions.
Console
In the Google Cloud console, go to the Create an instance page.
In the Create VM from ... menu, select Instance templates.
In the Create VM from template window that appears, do the following:
Select a template.
To create and start the VM, click Create.
gcloud
To create a VM from a regional or global instance template, use the same
gcloud compute instances create command
that you would use to create a normal instance, but add the
--source-instance-template flag:
gcloud compute instances create VM_NAME \
--source-instance-template INSTANCE_TEMPLATE_URL
Replace the following:
VM_NAME: the name of the instance.INSTANCE_TEMPLATE_URL: the URL of the instance template that you want to use to create VMs in the MIG. The URL can contain either the ID or name of the instance template. Specify one of the following values:- For a regional instance template:
projects/PROJECT_ID/regions/REGION/instanceTemplates/INSTANCE_TEMPLATE_ID - For a global instance template:
INSTANCE_TEMPLATE_ID
- For a regional instance template:
For example:
gcloud compute instances create example-instance \
--source-instance-template 1234567890
Go
Java
Node.js
Python
REST
To create a VM from a regional or global instance template, construct a
normal request to create an instance
but include the sourceInstanceTemplate
query parameter followed by a qualified path to an instance template.
POST https://compute.googleapis.com/compute/v1/projects/ PROJECT_ID/zones/ZONE/ instances?sourceInstanceTemplate=INSTANCE_TEMPLATE_URL
In the request body, provide a name for the VM instance:
{ "name": "example-instance" }
For example, the following snippet includes a fully-qualified path to the
template: https://compute.googleapis.com/compute/v1/projects/myproject/global/instanceTemplates/1234567890.
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/1234567890
{ "name": "example-instance" }
Create a VM instance from an instance template with overrides
When you use an instance template to start a VM instance, the default behavior is to create a VM instance exactly as described in the instance template with the exception of the instance name and zone.
If you want to create an instance primarily based on an instance template but with a few changes, you can use the override behavior. To use the override behavior, you pass in attributes to override for the existing instance template when creating the instance.
Console
In the Google Cloud console, go to the Create an instance page.
In the Create VM from ... menu, select Instance templates.
In the Create VM from template window that appears, select a template, and then click Customize.
Optional: Specify other configuration options. For more information, see Configuration options during instance creation.
To create and start the instance, click Create.
gcloud
Using the gcloud CLI, make a request to create an instance with
the --source-instance-template flag and override any property you want
with the appropriate gcloud flag. To see a list of applicable flags,
review the gcloud reference.
For example, provide the following flags to override the machine type, metadata, operating system, Persistent Disk boot disk, and a secondary disk of an instance template:
gcloud compute instances create example-instance \
--source-instance-template 1234567890 --machine-type e2-standard-2 \
--image-family debian-9 --image-project debian-cloud \
--metadata bread=butter --disk=boot=no,name=my-override-disk