GCP Instance Templates Explained: Reusable VM Blueprints in Compute Engine

An instance template is a saved VM configuration in Google Cloud Compute Engine. Instead of specifying machine type, OS image, startup script, and network settings every time you create a VM, you define them once in a template and reuse it indefinitely. Templates are especially important for managed instance groups, which rely on them to keep a fleet of identical VMs running.

Simple explanation

Think of an instance template as a recipe card for a VM. The card describes exactly what ingredients to use and how to prepare them, but it is not the finished dish. When you want a VM, you hand the recipe to Google Cloud and it creates one for you. Every VM made from the same recipe is identical.

Analogy

An instance template is like a mould in a factory. You design the mould once, and every item that comes out of it is identical. When you want a different shape, you build a new mould rather than modifying the existing one mid-production. The factory (the managed instance group) uses the mould to produce as many items as demand requires.

For example: you write a template that says “create an e2-medium VM running Debian 12, with 20 GB of disk, nginx installed at boot, and the http-server network tag.” Now you can create one VM from it for testing, or hand that same template to a managed instance group that creates twenty VMs automatically when traffic spikes.

You cannot edit a template after creating it. If you need to change something, you create a new template with a new name. This is deliberate: it makes your infrastructure auditable and predictable.

Why instance templates matter

Without templates, every new VM is a manual process. You choose a machine type, pick an image, write a startup script, add network tags, and configure a service account. And hope that the next VM you create is configured identically to the one before it. In practice, they drift. Over time you end up with a fleet where each VM has subtle differences, making debugging painful and scaling risky.

Instance templates solve this by moving configuration out of the moment of creation:

  • Consistency. Every VM from the same template is built the same way, with no manual variation.
  • Repeatability. You can re-run the same template days or months later and get an identical result.
  • Speed. Creating a VM from a template is one command. No decisions required at creation time.
  • Scale. Managed instance groups use templates to automatically create and replace VMs. Without a template, there is no autoscaling.
  • Safety. Because templates are immutable, you always know what configuration was used for any VM. There is a clear audit trail.
Tip

If you are planning to run more than one or two VMs, or if you want to use autoscaling, instance templates are not optional. They are the foundation everything else is built on.

How instance templates work

An instance template is a global resource. It lives at the project level, not in a specific region or zone. You create it once, and it can be used to launch VMs in any zone.

The template stores a configuration specification, not a running VM. Creating a template does not incur compute costs. No VM is started; only the configuration record is written.

Once created, the template is immutable. You cannot change any of its settings. To update a configuration, you create a new template with a new versioned name.

When a managed instance group needs a new VM — because the group is scaling up, because an unhealthy VM is being replaced, or because you triggered a rolling update — it reads the attached template and creates a VM from that specification. Every VM in the group is identical because they all came from the same source of truth.

What an instance template can contain

A template can specify everything you would normally configure when creating a VM directly:

  • Machine type — for example e2-medium or n2-standard-4. See the machine types guide for help choosing.
  • OS image or image family — such as debian-12 from debian-cloud. Image families always resolve to the latest version. See VM images explained.
  • Boot disk configuration — size, type (standard, balanced, or SSD), and whether to delete the disk on VM deletion.
  • Startup script — a shell script that runs when each VM boots. See VM startup scripts for guidance. Never put secrets in startup scripts; fetch them from Secret Manager at boot time instead.
  • Metadata key-value pairs — custom data available to the VM at runtime via the metadata server.
  • Network tags — used by firewall rules to control which traffic the VM accepts.
  • Labels — inherited by every VM created from the template, making cost attribution and filtering straightforward.
  • Service account — the identity the VM uses to call Google Cloud APIs. Always specify a dedicated, least-privilege service account. See least-privilege principles.
  • Access scopes — further restrict which Google Cloud APIs the service account can reach from this VM.
  • Additional disks — secondary persistent disks attached at creation time.
  • Network and subnetwork — which VPC and subnet to place the VM in.
Note

The zone is not stored in the template. Templates are global, so zone is specified when you create the VM or group, not in the template itself.

Creating an instance template

The gcloud compute instance-templates create command accepts the same flags as gcloud compute instances create. The key difference: no VM is created. Only the configuration is saved as a template.

This example creates a template for a simple web server. The startup script installs nginx, and the network tags allow HTTP and HTTPS traffic through the firewall.

# Create a web server template
gcloud compute instance-templates create web-server-template \
  --machine-type=e2-medium \
  --image-family=debian-12 \
  --image-project=debian-cloud \
  --boot-disk-size=20GB \
  --boot-disk-type=pd-balanced \
  --tags=http-server,https-server \
  --metadata=startup-script='#!/bin/bash
    apt-get update -y
    apt-get install -y nginx
    systemctl enable nginx
    systemctl start nginx'

# List all templates in the project
gcloud compute instance-templates list

# Inspect the full configuration of a template
gcloud compute instance-templates describe web-server-template

After running the create command, the template appears in the list with a status of READY. No VMs exist yet. The template is simply a saved configuration record in the project.

Creating a VM from a template

Use —source-instance-template to create a standalone VM from a template. You must specify a zone because templates are global and zone is not stored in them.

Any flag you add on the command line overrides the template value for that VM only. The template itself is not changed.

# Create a VM from a template (zone is required)
gcloud compute instances create my-web-server \
  --source-instance-template=web-server-template \
  --zone=us-central1-a

# Create the same VM but with a larger machine type for this instance only
gcloud compute instances create my-large-server \
  --source-instance-template=web-server-template \
  --zone=us-central1-a \
  --machine-type=e2-standard-4

Creating VMs directly from a template like this is useful for one-off machines in a consistent environment — a staging server that mirrors production, or a quick test VM. For fleets that need autohealing and scaling, use a managed instance group rather than creating individual VMs by hand.

Updating templates with versioning

Because templates are immutable, the update workflow always follows the same pattern: create a new template, point your infrastructure at it, then delete the old one.

Using version numbers or dates in template names is essential. A template named web-server-template with no suffix forces you to delete it before creating a replacement, leaving a window where your managed instance group has no valid template and cannot create new VMs.

# Create v2 of the template (for example, increasing disk size)
gcloud compute instance-templates create web-server-template-v2 \
  --machine-type=e2-medium \
  --image-family=debian-12 \
  --image-project=debian-cloud \
  --boot-disk-size=30GB \
  --boot-disk-type=pd-balanced \
  --tags=http-server,https-server \
  --metadata=startup-script='#!/bin/bash
    apt-get update -y
    apt-get install -y nginx
    systemctl enable nginx
    systemctl start nginx'

# Point the managed instance group at the new template BEFORE deleting the old one
gcloud compute instance-groups managed set-instance-template my-mig \
  --template=web-server-template-v2 \
  --zone=us-central1-a

# Now safe to delete the old template
gcloud compute instance-templates delete web-server-template
Warning

Updating the template on a MIG does not automatically replace existing VMs. Existing VMs continue running with the old configuration until you trigger a rolling update. The new template only applies to VMs created after the change. To replace the fleet, use gcloud compute instance-groups managed rolling-action start-update. See the managed instance groups guide for the full rolling update workflow.

Instance template vs machine image

These two concepts are easy to confuse. Both are related to VM configuration, but they serve very different purposes.

Instance templateMachine image
What it storesConfiguration only — machine type, image family, startup script, etc.Complete VM state — all disk contents, configuration, and optionally memory.
Contains disk dataNoYes
SizeNegligible — just a metadata record.Large — depends on disk size and contents.
Primary use caseCreating many identical VMs from a specification; required for MIGs.Cloning, migrating, or backing up a specific running VM.
MutabilityImmutable after creation.Point-in-time snapshot; create a new one to capture updated state.

Use a machine image when you have a VM with a specific installed state — applications, configuration files, database content — that you want to reproduce exactly. Use an instance template when you want a reproducible build process that starts from a clean OS image and installs what it needs via a startup script. See VM images explained for more context on how GCP public images and custom images relate to both.

Instance template vs creating VMs manually

If you only ever need one VM and you will not scale or replace it, creating it manually through the console or a single gcloud command is perfectly reasonable. The creating your first VM guide walks through this.

Templates become the right choice as soon as any of the following applies:

  • You need more than one VM with the same configuration.
  • You want to use a managed instance group for autohealing or autoscaling.
  • You need to be able to recreate VMs reliably after a failure.
  • You are managing infrastructure through automation or Terraform.
  • You want a consistent, auditable record of what each VM was built from.

When to use instance templates

  • Web server fleets. Define the machine type, nginx install, and health check tags once; let a managed instance group scale the fleet.
  • Stateless application backends. Any workload where individual VMs are replaceable and configuration should be consistent across the fleet.
  • Autoscaled services. Autoscaling requires a MIG, and a MIG requires a template. You cannot autoscale without one.
  • Repeatable test environments. Spin up a test VM quickly using the same template as production, run your tests, delete the VM. The next test uses the same template.
  • Infrastructure managed by Terraform or CI/CD. Templates are a natural fit for infrastructure-as-code workflows. See the Terraform for Google Cloud guide.

Service accounts and labels

Always specify a dedicated service account in your template rather than relying on the Compute Engine default. The default has the Editor role on the project — a compromised VM with that identity can modify nearly every resource in the project.

Labels set on the template are inherited by every VM created from it, making cost filtering and attribution straightforward across a large fleet.

# Create a minimal service account for the VMs
gcloud iam service-accounts create web-vm-sa \
  --display-name="Service account for web server VMs"

# Grant only the permissions it needs
gcloud projects add-iam-policy-binding PROJECT_ID \
  --member=serviceAccount:web-vm-sa@PROJECT_ID.iam.gserviceaccount.com \
  --role=roles/storage.objectViewer

# Create a template with a dedicated service account and labels
gcloud compute instance-templates create web-server-template-v3 \
  --machine-type=e2-medium \
  --image-family=debian-12 \
  --image-project=debian-cloud \
  --service-account=web-vm-sa@PROJECT_ID.iam.gserviceaccount.com \
  --scopes=cloud-platform \
  --tags=http-server \
  --labels=env=production,team=backend,app=web

# Filter all VMs created from templates with these labels
gcloud compute instances list \
  --filter="labels.env=production AND labels.app=web"

For a deeper look at structuring service account permissions, see IAM roles explained and the least-privilege guide.

Common mistakes

  1. Trying to edit a template in place. There is no update subcommand for instance templates. The only way to change any setting is to create a new template. Beginners sometimes try to work around this by editing VM metadata directly on instances inside a MIG, but the MIG treats VMs as disposable and will revert or recreate them.

  2. Not versioning template names. A template named my-template with no version suffix means you must delete it before you can create a replacement with the same name. This creates a gap where your MIG has no valid template and cannot create new VMs. Use names like web-template-v2 or web-template-2026-03.

  3. Embedding secrets in startup script metadata. Startup scripts in templates are readable by anyone with the compute.instanceTemplates.get permission. Never put passwords, API keys, or certificates directly in template metadata. Fetch secrets at boot time from Secret Manager using the VM’s service account.

  4. Deleting a template still referenced by a MIG. If you delete a template that a MIG is pointing at, the group cannot create replacement VMs during autohealing or scale-out. Always update the MIG to reference the new template before deleting the old one.

  5. Assuming a new template automatically updates existing VMs. It does not. Pointing a MIG at a new template changes what will be used for future VM creations only. VMs already running are unaffected until you trigger a rolling update.

  6. Using the Compute Engine default service account. The default has Editor-level access to the project. Every VM created from a template with the default identity is a potential lateral movement risk. Create a dedicated, minimal service account per workload.

  7. Confusing templates with machine images. A template is a configuration specification with no disk data. A machine image captures the full disk contents of a running VM. They are not interchangeable.

Security and operational best practices

A well-designed template is one of the most effective ways to enforce security standards across a VM fleet, because every VM is built from the same specification.

  • Use dedicated, least-privilege service accounts. Create one service account per application type and grant only the IAM roles it genuinely needs. Never use the default Compute Engine service account in a production template.

  • Never put secrets in metadata or startup scripts. Use Secret Manager and have the startup script fetch secrets at runtime using the instance’s service account identity.

  • Use labels for cost attribution and operational visibility. Set env, team, and app labels in the template so every VM inherits them. This makes it possible to filter VMs, set billing alerts per environment, and identify which team owns a given fleet.

  • Test new templates before rolling them into production. Create a VM from the new template, verify it boots correctly and the application starts, then update the MIG. A broken startup script will cycle the entire fleet if you push it without testing.

  • Keep templates under version control or infrastructure-as-code. Define templates in Terraform so every change is code-reviewed, tracked, and repeatable. See the Terraform for Google Cloud guide.

Danger

Changing the template does not retroactively change any running VM. Existing VMs continue with the configuration they were launched from. To propagate a change across a fleet, you must trigger a rolling update on the MIG, which replaces VMs one at a time using the new template. Missing this step is a common source of “why is production still broken after I updated the template?” confusion.

# Template with labels and a Secret Manager-based startup script
gcloud compute instance-templates create web-server-template-v4 \
  --machine-type=e2-medium \
  --image-family=debian-12 \
  --image-project=debian-cloud \
  --service-account=web-vm-sa@PROJECT_ID.iam.gserviceaccount.com \
  --scopes=cloud-platform \
  --labels=env=production,team=backend,app=web \
  --metadata=startup-script='#!/bin/bash
    # Fetch configuration at boot time — never bake secrets into the template
    SECRET=$(gcloud secrets versions access latest --secret=app-config)
    echo "$SECRET" > /etc/app/config.json
    apt-get update -y && apt-get install -y nginx
    systemctl enable nginx && systemctl start nginx'

Frequently asked questions

What is an instance template in GCP?

An instance template is a saved Compute Engine configuration that defines everything about a VM: machine type, OS image, boot disk, network tags, service account, metadata, and startup script. It is a global resource — you create it once and use it to launch as many identical VMs as you need, in any region. Instance templates do not create any running VMs on their own; they are a blueprint that other services, such as managed instance groups, use when they need to create a VM.

Can I edit an instance template after creating it?

No. Instance templates are immutable once created. There is no update or edit command. To change any setting, create a new template with the updated configuration and a new versioned name, then point your managed instance group or automation at the new template. This immutability is intentional — it means you always know exactly what configuration was used to create any given VM.

Do instance template changes affect existing VMs automatically?

No. Creating a new template version does not touch any VMs that already exist. Existing VMs continue running with the configuration they were created from. To apply a new template to a managed instance group, you must trigger a rolling update, which gradually replaces the old VMs with new ones built from the new template.

What is the difference between an instance template and a machine image?

An instance template is a lightweight configuration blueprint — it describes what a VM should look like, but contains no disk data. A machine image captures the complete state of a running VM, including all disk contents, configuration, and optionally memory state. Use a machine image when you want to clone or migrate a specific running system. Use an instance template when you want to create many identical VMs from a specification on demand.

Are instance templates required for managed instance groups?

Yes. A managed instance group (MIG) cannot exist without an instance template. Every VM the group creates — whether to meet a minimum size, respond to autoscaling, or replace an unhealthy instance — is created from the template attached to the group. This is what guarantees every VM in the group is identical.

Last verified: 22 March 2026 Cloud services change frequently. Verify details against official documentation before making infrastructure decisions.