How to Create and Use Instance Templates on Google Cloud Platform

google cloud platform

Instance templates allow you to predefine a machine configuration and image to start up clones of your servers automatically. They’re commonly used with auto-scaling to facilitate High Availability services.

What Are Instance Templates?

Auto-scaling can seem like magic, but at the end of the day, you’re still creating and destroying servers multiple times per day, a feat that would kill any sysadmin if they were tasked with doing it manually.

Automating your job can seem like an insurmountable task, but really you’re just defining some startup configuration, and making a copy of your server’s files. If you want to automate it properly, you’ll either need to write a startup script to install the dependencies your server needs, or move to running in a container, which ends up being largely the same kind of configuration.

GCP’s Instance templates support both of these methods. You can make an instance template to deploy a container image in an autoscaling or managed instance group, or simply make a copy of your server to deploy another one. In both cases, Instance Templates also allow you to define what kinds of servers you want to deploy, either a specific instance type or a specific amount of memory and vCPUs.

Creating Instance Templates

You have two options for creating instance templates—either from a container image, or from an existing instance.

If you’re looking to deploy a container image, you can create this template from the Instance Templates tab in the Compute Engine console. Select the machine settings, such as the number of vCPUs and amount of memory.

Create this template from the Instance Templates tab in the Compute Engine console to deploy a container image

Then, under container settings, check “Yes” to deploying a container image, and enter in the URL of the image. You can use Google’s Container Registry for this, or use a public registry like the Docker hub.

On top of that, you have some options for setting the entry command, any parameters or environment variables, as well as running the container as privileged, which allows root access to storage and network devices as if it was running directly on the host. You can also configure volume mounts, and add additional disks to the container.


If you want to make a copy of an existing instance, you can do so from the gcloud command line. You’ll need a few options—the name of the template, name of the source instance, and the name of the disk you want to copy.

gcloud compute instance-templates create [TEMPLATE NAME]  
    --source-instance [SOURCE_INSTANCE] 
    --configure-disk= device-name=[DISK NAME], instantiate-from=source-image, 

Of course, you’ll need to repeat this process every time you want to make updates to your instance group.

This definitely isn’t the best way of handling updates, and you have two solutions to this problem: Add an install script to handle deployments of updates on startup, such as fetching the latest build from git, or move to containers. Any task that can be auto-scaled can be containerized, and there’s no performance hit to running in Docker. You can read our guide on moving your application to Docker to learn more.

Using Your Templates in a MIG

Managed Instance Groups (MIG) are groups of instances that support auto-scaling and auto-healing, of course, powered by Instance templates. You can select which template you want to use from the settings when creating a managed instance group.

Select which template you want to use from the settings when creating a MIG

If you’re just looking to run a set of instances behind a load balancer, you can use unmanaged instance groups, which don’t need to be configured with instance templates but require you to manually administrate the servers.