This page describes how to create and manage instance templates . Instance templates let you specify the machine type, boot disk image, network, and other VM properties that you want to use when creating virtual machine (VM) instances.
You can use instance templates to do the following:
- Create individual VMs .
- Create VMs in a managed instance group (MIG) .
- Create reservations for VMs .
Before you begin
- Read when and why to create deterministic instance templates.
- Read about regional and global instance templates .
- If you include Dynamic Network Interfaces (NICs) in the instance template, review the properties and limitations of Dynamic NICs as described in Dynamic NICs . For more information about creating instances with multiple network interfaces, see Create VMs with multiple network interfaces .
- 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 init
If 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 .
Terraform
To use the Terraform 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 .
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.
-
Limitations
- Shared VPC on interfaces other than
nic0for instance templates is supported in gcloud CLI and REST, but not in Google Cloud console. - You can't update an existing instance template or change an instance template after it has been created. If an instance template goes out of date, or you need to make changes to the configuration, create a new instance template.
- If you want to specify an image family in an instance template, you can't use the Google Cloud console. You can use the Google Cloud CLI or REST instead.
- If you want to specify regional disks instead of zonal disks in an instance template, you can't use the Google Cloud console. You can use the Google Cloud CLI or REST instead.
- You can use an instance template to create VMs with a Hyperdisk Balanced boot disk that is in a storage pool , if the storage pool exists in the same zone that the VM is created in. You can't use global instance templates to create VMs with non-boot disks that are in a storage pool.
Create an instance template
Most of the VM properties that you can specify in a request to create an individual VM instance can also be specified for an instance template, including any VM metadata, startup scripts, persistent disks, and service accounts. You must specify the machine type, boot disk, and network.
Create a regional or global instance template through the Google Cloud console , Google Cloud CLI , or the API . To create a global instance template, you can also use the Terraform or the Cloud Client Libraries .
Console
-
In the Google Cloud console, go to the Instance templatespage.
The remaining steps appear in the Google Cloud console.
- Click
Create instance template . - Select the Locationas follows:
- If you want to use the instance template across regions, choose Global.
- If you want to reduce cross-region dependency, choose Regional.
- If you chose regional, then select the Regionwhere you want to create your instance template.
-
For the following fields, either accept the default values or modify them as required. The default values change based on the machine family that you select.
- Select a
Machine type . - To update the boot disk type or image, in the
Boot disk section, click Change. - To update the network interface or IP address settings, click
Advanced options , then click Networking, and then click the network interface you want to edit.
- Select a
-
Optional: If you chose an image that supports Shielded VM, change the VM's Shielded VM settings:
- Click Advanced options, and then click the Securitytab.
- If you want to disable Secure Boot, clear the Turn on Secure Bootcheckbox. Secure Boot helps protect your VM instances against boot-level and kernel-level malware and rootkits. For more information, see Secure boot .
-
If you want to disable the virtual trusted platform module (vTPM), clear the Turn on vTPMcheckbox. The vTPM enables Measured boot, which validates the VM pre-boot and boot integrity. For more information, see Virtual Trusted Platform Module (vTPM) .
-
If you want to disable integrity monitoring, uncheck the Turn on Integrity Monitoringcheckbox. Integrity monitoring lets youn monitor the boot integrity of your Shielded VM instances by using Cloud Monitoring. For more information, see Integrity monitoring .
-
Optional: Under Advanced options, click the tabs to further customize your template. For example, you can add up to 15 secondary non-boot disks.
-
Optional: Click Equivalent RESTto view the REST request body, which includes the JSON representation of your instance template.
-
Click Createto create the template.
gcloud
To create a regional or global instance template, use the instance-templates create
command
.
For a regional instance template, you must use the --instance-template-region
flag to set the region of the template.
Create a regional instance template using the following command.
gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME \ --instance-template-region= REGION
Replace REGION
with the region where you
want to create the regional instance template.
Create a global instance template using the following command:
gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME
If you don't provide explicit template settings, gcloud compute
uses
the following default values:
- Machine type: the machine type—for example,
n1-standard-1 - Image: the latest Debian image
- Boot disk: a new standard boot disk named after the VM
- Network: the default VPC network
- IP address: an ephemeral external IPv4 address
- Stack type:
IPV4_ONLY
You can also explicitly provide these configuration settings. For example:
gcloud compute instance-templates create my-instance-template \
--machine-type=e2-standard-4 \
--image-family=debian-11 \
--image-project=debian-cloud \
--boot-disk-size=250GB
You can add up to 15 secondary non-boot disks. Specify the --create-disk
flag for each secondary disk you create. To create secondary disks from a
public or custom image, specify the image
and image-project
properties
for each disk in the --create-disk
flag. To create a blank disk, don't
include these properties. Optionally, include properties for the disk size
and type
. To
specify regional disks, use the replica-zones
property.
gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME \ --machine-type= MACHINE_TYPE \ --create-disk=image-family= DISK_IMAGE_FAMILY ,image-project= DISK_IMAGE_PROJECT ,size= SIZE_GB_DISK1 \ --create-disk=device-name= DISK_NAME ,type= DISK_TYPE ,size= SIZE_GB_DISK2 ,replica-zones=^:^ ZONE : REMOTE_ZONE ,boot=false
Tip: When specifying
the disk replica-zones
parameter, the characters ^:^
specify that
the separation character between values is a colon ( :
) instead of the
expected comma ( ,
).
Replace the following:
-
INSTANCE_TEMPLATE_NAME: the name for the template -
MACHINE_TYPE: the machine type of the VMs -
DISK_IMAGE_FAMILY: an image family to use as a non-boot diskFor more information about image families, see best practices when using image families on Compute Engine.
You can use instead the flag
--image= IMAGEto specify a specific version of an image.For blank disks, don't specify the
image-familyorimageproperty. -
DISK_IMAGE_PROJECT: the image project that contains the imageFor blank disks, don't specify the
image-projectproperty. For more information on public images, see Public images . -
SIZE_GB_DISK1andSIZE_GB_DISK2: the size of each secondary disk -
DISK_NAME: Optional: the disk name displayed to the guest OS after the VM is created. -
DISK_TYPE: Optional: the type of disk to create. If not specified, the default disk type that is used depends on the value of the--machine-typeflag. -
ZONEandREMOTE_ZONE: the zone to create the regional disk in and the zone to replicate it to.For zonal disks, don't include the
replica-zonesproperty.
If you chose an image that supports Shielded VM , you can optionally change the instance's Shielded VM settings using one of the following flags:
-
--no-shielded-secure-boot: turns off Secure BootSecure Boot helps protect your VM instances against boot-level and kernel-level malware and rootkits. For more information, see Secure Boot .
-
--no-shielded-vtpm: turns off the virtual trusted platform module (vTPM)The vTPM enables Measured Boot, which validates the VM pre-boot and boot integrity. For more information, see Virtual Trusted Platform Module (vTPM) .
-
--no-shielded-integrity-monitoring: turns off integrity monitoringIntegrity monitoring lets you monitor the boot integrity of your Shielded VM instances using Cloud Monitoring. For more information, see Integrity monitoring .
For a list of all available subcommands and flags, see the instance-templates
reference
.
Terraform
To create an instance template, use one of the following resources:
- For a regional instance template, use the
google_compute_region_instance_templateresource . - For a global instance template, use the
google_compute_instance_templateresource .
The following example creates a global instance template:
To learn how to apply or remove a Terraform configuration, see Basic Terraform commands .
Go
Java
Node.js
Python
REST
To create a regional instance template, make a POST
request to the regionInstanceTemplates.insert
method
as follows:
POST https://compute.googleapis.com/compute/v1/projects/ PROJECT_ID /regions/ REGION /instanceTemplates
To create a global instance template, make a POST
request to the instanceTemplates.insert
method
:
POST https://compute.googleapis.com/compute/v1/projects/ PROJECT_ID /global/instanceTemplates
You can add up to 15 secondary non-boot disks by using the disks
property, with a field for each additional disk. For each
additional disk, you can do the following:
- Create additional disks with a public or custom image.
- To add a blank disk, define the
initializeParamsentry with nosourceImagevalue. - To create regional disks, define the
initializeParamsentry with the propertyreplicaZones.
In the body of the request, provide the template properties:
{ "name": " INSTANCE_TEMPLATE_NAME ", "properties": { "machineType": " MACHINE_TYPE ", "networkInterfaces": [ { "network": "global/networks/default", "accessConfigs": [ { "name": "external-IP", "type": "ONE_TO_ONE_NAT" } ] } ], "disks": [ { "type": "PERSISTENT", "boot": true, "mode": "READ_WRITE", "initializeParams": { "sourceImage": "projects/ IMAGE_PROJECT /global/images/ IMAGE " } }, { "type": "PERSISTENT", "boot": false, "deviceName": " DISK_NAME ", "initializeParams": { "replicaZones": [ "projects/ PROJECT_NAME /zones/ ZONE ", "projects/ PROJECT_NAME /zones/ REMOTE_ZONE " ] } } ] } }
Replace the following:
-
PROJECT_ID: your project id -
REGION: the region where you want to create your regional instance template -
INSTANCE_TEMPLATE_NAME: the name of the instance template -
ZONE: the zone where VMs are located -
MACHINE_TYPE: the machine type of the VMs -
IMAGE_PROJECT: the image project that contains the imageFor more information on public images, see Public images .
-
IMAGEorIMAGE_FAMILY: specify one of the following:-
IMAGE: a specific version of the imageFor example,
"sourceImage": "projects/debian-cloud/global/images/debian-10-buster-v20200309" -
IMAGE_FAMILY: an image familyThis creates the VM from the most recent, non-deprecated OS image. For example, if you specify
"sourceImage": "projects/debian-cloud/global/images/family/debian-10", Compute Engine creates a VM from the latest version of the OS image in the Debian 10 image family.For more information on image families, see best practices when using image families on Compute Engine.
-
-
DISK_NAME: Optional: the disk name displayed to the guest OS after the VM is created. -
PROJECT_NAME: the project associated with the VM -
REMOTE_ZONE: the zone where the regional disk should be replicated to
You can specify one of the following options for the disks
property:
-
Specify
initializeParamsto create boot disks for each instance. You can create disks by using public or custom images (or image families ) using thesourceImageproperty, as shown in the preceding example. To add blank disks, don't specify asourceImage. You can also add up to 15 secondary non-boot disks by using theinitializeParamsproperty for each additional disk. -
Specify
sourceto attach an existing boot disk. If you attach an existing boot disk, you can only create one instance from your template.
Optionally, you can specify the diskSizeGb
, diskType
, and labels
properties for initializeParams
and the diskSizeGb
property for source
.
If you chose an image that supports Shielded VM , you can optionally change the VM's Shielded VM settings by using the following Boolean request body items:
-
enableSecureBoot: turns on or off Secure BootSecure Boot helps protect your VM instances against boot-level and kernel-level malware and rootkits. For more information, see Secure Boot .
-
enableVtpm: turns on or off the virtual trusted platform module (vTPM)The vTPM enables Measured Boot, which validates the VM pre-boot and boot integrity. For more information, see Virtual Trusted Platform Module (vTPM) .
-
enableIntegrityMonitoring: turns on or off integrity monitoringIntegrity monitoring lets you monitor and verify the runtime boot integrity of your Shielded VM instances by using Cloud Monitoring reports. For more information, see Integrity monitoring .
To learn more about request parameters, see the instanceTemplates.insert
method
.
Create an instance template based on an existing instance
You can use REST or gcloud CLI to save the configuration of an existing VM instance as an instance template. You can optionally override how the source disks are defined in the template.
If you need to override other properties, first create an instance template based on an existing instance, then create a similar template with additional overrides.
gcloud
Use the gcloud compute instance-templates create
command
with the --source-instance
and --source-instance-zone
flags. If you want
to create a regional instance template, then you must also use the --instance-template-region
flag to specify the region of the instance
template.
To create a regional instance template, use the following command:
gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME \ --source-instance= SOURCE_INSTANCE \ --source-instance-zone= SOURCE_INSTANCE_ZONE \ --instance-template-region= REGION
To create a global instance template, use the following command:
gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME \ --source-instance= SOURCE_INSTANCE \ --source-instance-zone= SOURCE_INSTANCE_ZONE
To override how the source instance's disks are defined, add one or more --configure-disk
flags.
The following example creates a global instance template from an existing instance and overrides the source instance's disk with the specifications that you provide.
gcloud compute instance-templates create INSTANCE_TEMPLATE_NAME \ --source-instance= SOURCE_INSTANCE \ --source-instance-zone= SOURCE_INSTANCE_ZONE \ --configure-disk= \ device-name= SOURCE_DISK , \ instantiate-from= INSTANTIATE_OPTIONS , \ auto-delete= AUTO_DELETE
Replace the following:
-
INSTANCE_TEMPLATE_NAMEis the name of the template to create. -
SOURCE_INSTANCEis the name of the instance to use as a model for the new template. -
SOURCE_INSTANCE_ZONEis the zone that contains the source instance. -
SOURCE_DISKis the name of a source-instance disk that you want to override within the template. -
INSTANTIATE_OPTIONSspecifies whether to include the disk and which image to use. Valid values depend on the type of disk:-
source-imageorsource-image-family(valid only for boot and other persistent read-write disks). Specify this option if you want to use the same source image or source image family that was used to create the disk in the source VM instance. -
custom-image(valid only for boot and other persistent read-write disks). If you want to retain applications and settings from the source VMs in your instance template, you can create a custom image and then specify it when you create the template. If specified, then provide the path or URL for the custom image, as shown in the following example. Alternatively, you can specify an image family using the following format:--configure-disk=device-name= DATA_DISK_NAME ,instantiate-from=custom-image, \ custom-image=projects/ PROJECT_ID /global/images/family/ IMAGE_FAMILY_NAME -
attach-read-only(valid only for read-only disks). -
blank(valid only for non-boot persistent disks and local SSDs). If specified, then, when the template is used to create a new instance, the disk is created unformatted. You must format and mount the disk in a startup script before you can use it in a scalable setup. -
do-not-include(valid only for non-boot persistent disks and read-only disks).
-
-
AUTO_DELETEspecifies whether the disk is auto-deleted when the instance is deleted. Valid values are:false,no,true, andyes.
For example, the following command creates an instance template based on my-source-instance
, with the option to use the original image from data-disk-a
, but set auto-delete to true
and replace data-disk-b
with a custom image.
gcloud compute instance-templates create my-instance-template \
--source-instance=my-source-instance \
--configure-disk=device-name=data-disk-a,instantiate-from=source-image,auto-delete=true \
--configure-disk=device-name=data-disk-b,instantiate-from=custom-image,custom-image=projects/cps-cloud/global/images/cos-89-16108-403-15
Go
Java
Node.js
Python
REST
To create a regional instance template, use the regionInstanceTemplates.insert
method
,
or, to create a global instance template, use the instanceTemplates.insert
method
.
In your request, you must specify the sourceInstance
field. To override how the source
instance's disks are defined, add one or more diskConfigs
fields.
For example, make the following call to create a global instance template from an existing instance.
POST https://compute.googleapis.com/compute/v1/projects/ PROJECT_ID /global/instanceTemplates { "name": " INSTANCE_TEMPLATE_NAME ", "sourceInstance": "zones/ SOURCE_INSTANCE_ZONE /instances/ SOURCE_INSTANCE ", "sourceInstanceParams": { "diskConfigs": [ { "deviceName": " SOURCE_DISK ", "instantiateFrom": " INSTANTIATE_OPTIONS ", "autoDelete": false } ] } }
Replace the following:
-
PROJECT_ID: your project id -
INSTANCE_TEMPLATE_NAME: the name for the new template -
SOURCE_INSTANCE_ZONE: the zone of the source instance -
SOURCE_INSTANCE: the name of the source instance to use as a model for this instance template -
SOURCE_DISK: the name of a source-instance disk that you want to override within the template -
INSTANTIATE_OPTIONS: specifies whether to include the disk and which image to useValid values depend on the type of disk:
-
source-imageorsource-image-family(valid only for boot and other persistent read-write disks). -
custom-image(valid only for boot and other persistent read-write disks). If you want to retain applications and settings from the source VMs in your instance template, you can create a custom image and then specify it when you create the template. If specified, then provide the path or URL for the custom image, as shown in the following example. Alternatively, you can specify an image family using the following format:"diskConfigs": [ { "deviceName": DATA_DISK_NAME , "instantiateFrom": custom-image, "customImage": "projects/ PROJECT_ID /global/images/family/ IMAGE_FAMILY_NAME " } ] -
attach-read-only(valid only for read-only disks). -
blank(valid only for non-boot persistent disks and local SSDs). If specified, then, when the template is used to create a new instance, the disk is created unformatted. You must format and mount the disk in a startup script before you can use it in a scalable setup. -
do-not-include(valid only for non-boot persistent disks and read-only disks).
-
The following example creates a new instance template based on my-source-instance
. In the instance template, the image for data-disk-a
is replaced with projects/cos-cloud/global/images/cos-89-16108-403-15
.
POST https://compute.googleapis.com/compute/v1/projects/my_project/global/instanceTemplates
{
"name": "my-instance-template",
"sourceInstance": "zones/us-central1-a/instances/my-source-instance",
"sourceInstanceParams":
{
"diskConfigs":
[
{
"deviceName": "data-disk-a",
"instantiateFrom": "custom-image",
"customImage": "projects/cos-cloud/global/images/cos-89-16108-403-15"
}
]
}
}
The following table shows how the options for overriding disks are defined in the template.
- [Default] Use the same source image or image family that was used to create the boot disk in the source instance.
- Use the URL of any image (custom or public) as described in the preceding example or specify an image family using the following format:
projects/exampleproject/global/images/family/ IMAGE_FAMILY_NAME
- [Default] Use the same source image/source image family that was used to create the disk in the source instance. Note: If the source instance's disk doesn't have a source image/source image family property, then it is included in the template as a blank disk.
- Use the URL of any image (custom or public) as described in the
preceding example or specify an image family using the following
format:
projects/exampleproject/global/images/family/ IMAGE_FAMILY_NAME - Use a blank disk in the template instead. When the template is used to create a new instance, this disk is created unformatted. You must format and mount the disk in a startup script before you can use it in a scalable setup.
- Don't include the disk.
- [Default] Include the disk in read-only mode.
- Don't include the disk.
- [Default] Include a blank local SSD. When the template is used to create a new instance, this disk is created unformatted. You must format and mount the disk in a startup script before you can use it in a scalable setup.
For each disk, you can also override the auto-delete
attribute to specify
whether the disk should be deleted when its associated instance is
deleted.
By default, if no override options are specified, the disk configuration in the template matches the source instance.
Create an instance template based on an existing template
You can't update an existing instance template. But, if an instance template goes out of date or if you need to make changes, you can create another one with similar properties by using the console.
-
Go to the Instance templatespage.
-
Click the instance template that you want to copy and update.
-
Click Create similar.
-
Update the configuration in the new template.
-
Click Create.
Create an instance template for GPU VMs
When creating an instance template, you can configure it for creating VMs that have attached GPUs by specifying the following:
-
A machine type that supports the GPU type you want . If you want to create a regional instance template, the GPU type that you specify in the template must be available in at least one zone in the region .
-
A maintenance policy that stops VMs during host maintenance events .
Note: If you want to run data science or machine learning workloads, consider using a Deep Learning VM image when you create an instance template. Deep Learning VM Images is a set of prepackaged VM images that comes with machine learning frameworks and essential tools. For more information about these images, see Choose an image in the Deep Learning VM Images documentation.
Permissions required for this task
To perform this task, you must have the following permissions :
- All permissions required to call the
instanceTemplates.insertmethod .

