How to use instance templates for repeatable, consistent VM deployments on Google Compute Engine.
What Are Instance Templates?
An instance template is a reusable blueprint for creating VMs. It defines the machine type, boot image, disks, network, startup script, service account, and other properties. You can create as many VMs as you want from a single template, knowing they will all have the same configuration.
Instance templates are immutable. Once created, you cannot modify a template. To change the configuration, create a new template.
What’s in a Template
| Usually Defined in Template | Set at Creation Time or Commonly Overridden |
|---|---|
| Machine type | Zone |
| Boot image / source disk | Instance name |
| Disk configuration | Per-instance disk overrides |
| Network, subnetwork, and external IP behavior | Specific static external IP address |
| Startup script | Per-instance metadata overrides |
| Service account and scopes | |
| Labels and metadata | |
| Shielded VM settings | |
| GPU configuration | |
| Sole-tenant node affinity | |
| Min CPU platform | |
| Preemptibility (Spot/standard) |
Warning: Avoid putting a specific static external IP address into a reusable template. If you create a template from an existing VM that has a static external IP, that address can be copied into the template and restrict where or how the template can be used.
Why Use Instance Templates
- Managed Instance Groups (MIGs): Templates are required to create a MIG. The MIG uses the template to create and auto-scale a group of identical VMs.
- Consistent provisioning: Every VM created from the template has the same configuration. No manual drift.
- Blue/green and canary deployments: Create a new template with updated software, then gradually shift the MIG to the new template.
- Disaster recovery: If VMs in a MIG fail, the MIG automatically creates replacements from the template. No manual intervention.
- Quick scaling: When auto-scaling triggers, new VMs are created from the template in seconds.
In practice: If you’re building a production system with more than one VM, use instance templates. They are the foundation for auto-scaling, self-healing, and repeatable deployments on GCE.
Instance Templates vs Machine Images
| Aspect | Instance Template | Machine Image |
|---|---|---|
| What it stores | VM configuration only (no disk data) | Full disk state + VM configuration |
| Like a… | Recipe | Frozen meal |
| Disk source | References a public image or existing disk | Captures the disk contents at a point in time |
| Boot time | Normal (installs from scratch if using startup script) | Fast (disk already has everything) |
| Size | Small (just metadata) | Large (disk data) |
| Best for | Creating many identical new VMs, MIGs, auto-scaling | Cloning an existing VM, backup/restore, migrating VMs |
| Cost | No storage cost | Charged for disk snapshot storage |
When to use a template: You’re creating fresh VMs from a known image and configuring them with startup scripts. This is the standard approach for auto-scaling web servers, workers, etc.
When to use a machine image: You need to preserve the exact disk state of an existing VM (installed software, data, configuration) and clone it. This is better for migrating VMs or creating copies of a VM with complex pre-installed software.
Creating Templates
From Scratch
gcloud compute instance-templates create web-server-template \
--machine-type=e2-medium \
--image-family=debian-12 \
--image-project=debian-cloud \
--boot-disk-size=20GB \
--network=default \
--tags=http-server \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install -y nginx'From an Existing VM
gcloud compute instance-templates create web-server-v2 \
--source-instance=my-existing-vm \
--source-instance-zone=us-central1-aThis captures the VM’s configuration (machine type, disks, network, etc.) into a template. The disk source defaults to the VM’s current disk.
Using Templates
Create a Single VM from a Template
gcloud compute instances create my-vm \
--source-instance-template=web-server-template \
--zone=us-central1-aYou only need to specify the zone and optionally the instance name. Everything else comes from the template.
Create a Managed Instance Group from a Template
gcloud compute instance-groups managed create my-mig \
--template=web-server-template \
--size=3 \
--zone=us-central1-aThis creates 3 identical VMs from the template. The MIG monitors their health and replaces failed VMs automatically.
Updating Templates
Templates are immutable. You cannot edit an existing template. The update workflow is:
- Create a new template with the updated configuration
- Tell the MIG to roll out the new template
- Delete the old template when no longer needed
# Step 1: Create new template
gcloud compute instance-templates create web-server-v2 \
--machine-type=e2-medium \
--image-family=debian-12 \
--image-project=debian-cloud \
--boot-disk-size=20GB \
--network=default \
--metadata=startup-script='#!/bin/bash
apt-get update
apt-get install -y nginx-extras'
# Step 2: Update the MIG to use the new template
gcloud compute instance-groups managed rolling-action start-update my-mig \
--version=template=web-server-v2 \
--zone=us-central1-a
# Step 3: Delete old template (after rollout completes)
gcloud compute instance-templates delete web-server-templateNote: MIGs support rolling updates (gradual replacement) and canary updates (test on a subset of VMs first). This lets you deploy new versions with minimal risk.
Regional vs Global Templates
By default, instance templates are global resources. They can be used to create VMs in any zone in any region. Google also supports regional instance templates:
| Property | Global | Regional |
|---|---|---|
| Scope | Available in all regions | Available in one specific region |
| Best for | General use | Data residency requirements, hardware-specific configurations |
| Creation | Default (no --region flag) | Add --region=REGION flag |
# Regional template
gcloud compute instance-templates create regional-template \
--region=us-central1 \
--machine-type=e2-medium \
--image-family=debian-12 \
--image-project=debian-cloudGoogle recommends regional templates when you have data residency requirements or need to ensure hardware consistency within a region.
TL;DR
- Instance templates are immutable blueprints for VMs. Define machine type, image, disks, network, startup scripts, and more.
- Required for Managed Instance Groups (MIGs) and auto-scaling. Cannot create a MIG without one.
- Templates store configuration only (no disk data). Use machine images when you need to capture disk state.
- To update, create a new template and roll it out to the MIG. Old templates cannot be edited.
- Use regional templates for data residency or hardware-specific requirements.
Resources
Instance Templates Documentation Official reference for all template properties and creation options.
Create Instance Templates Step-by-step guides for creating templates from scratch and from existing VMs.
Machine Images Documentation When you need to capture full disk state instead of just configuration.
Google Compute Engine Overview of GCE features and architecture.
Startup Scripts Automate VM configuration with boot-time scripts.