In this tutorial you will learn to create and configure Instance templates in Google Cloud and simplify Virtual Machine creation using GCP Instance Template.
What is Instance Template In GCP
An instance template is a GCP resource used to create a virtual machine (VM) or Managed Instance Group (MIG).
Instance template can be used to specify below list of configurations for virtual machines along with other metadata.
- Machine Type
- Machine Family
- Firewalls Rules
- Startup Script
- Network, subnetwork and disk configuration.
- OS Image and other software configuration.
To learn more about advance virtual machine configuration refer Set Up GCP Free VM In 10 Minutes – Google Cloud Tutorials.
GCP Instance Template is a global resource and not tied to any zone or region. However, if an instance template is used to specify zone specific configuration, then it is tied to a zone or region.
Example: If an instance template is configured to a read-only persistent disk from us-central1-b
than your instance template is tied to us-central1-b
and cannot be used to configured resources outside of this zone.
Instance templates are free in google cloud and you would not be charged for creating instance templates.
You will only be charged for Virtual Machines and Managed Instance groups created using GCP Instance Templates.
GCP Create Instance Template
This will be hands on tutorial, and you can follow along. Google Cloud free tier account offer free credits worth 400 $ to learn and explore GCP services. Learn more on How to Set Up Google Cloud Free trial and Claim Free Credits here Google Cloud Free Credits – Google Cloud Tutorials.
Log into https://console.cloud.google.com/ with the Gmail Account.
It is recommended to create a new GCP project for the demo. This will allow us to delete the project at the end of the demo to ensure all cloud services has been deleted and there are no recurring charges.
At the top left corner click on projects drop down and you will see below pop up.
Click on create new project and enter valid project name.
Google cloud free trail account supports maximum of 22 Projects.
Organization only exists for corporate accounts and there is no organization for individual accounts.
In Cloud Console search “Compute Engine” and enable Compute Engine API.
In Compute Engine under instance templates click on create a new instance template.
Specify instance template name and labels as per requirment.
Google cloud offers 4 types of machine family. Depending on type of workload anyone of the following can be selected.
- General Purpose: Machine Family suitable for all common workloads like Webserver, Database server.
- Compute Optimized: Machine Family suitable for compute intensive workloads like gaming applications.
- Memory Optimized: Machine Family suitable for memory intensive workloads like analytics and machine learning.
- GPU: GCP also supports dedicated GPU for graphics intensive workloads.
Machine type is used to specify exact number of CPU’s, disk and memory.
For each of machine family there are multiple machine types.
Machine Types for E2 family.
Shared Instance | Standard | High Memory | High CPU |
e2- micro 1 shared core 1 GB memory | e2- standard -2 2 VCPU 8 GB memory | e2- highmem -2 2 VCPU 16 GB memory | e2- highcpu -2 2 VCPU 2 GB memory |
e2- small 1 shared core 2 GB memory | e2- standard -4 4 VCPU 16 GB memory | e2- highmem-4 4 VCPU 32 GB memory | e2- highcpu -4 4 VCPU 4 GB memory |
e2- medium 1 shared core 3 GB memory | e2- standard -8 8 VCPU 32 GB memory | e2- highmem-8 8 VCPU 64 GB memory | e2- highcpu -8 8 VCPU 8 GB memory |
GCP Instance Template Start up script
Google cloud also allows start up scripts as a part of instance template.
Startup scripts are executed during the launch of virtual machines.
In this tutorial we will install Apache web server using start up script.
To configure start up script copy and paste below script in the startup script box under automation tab.
#!/bin/bash
apt update
apt -y install apache2
Google cloud offers 2 different types of Images.
Public Images: Provided and maintained by google and open-source communities.
Custom Image: Created by you for your project.
In boot disk configuration select OS image and version.
specify disk type and disk size.
Google cloud by default encrypts the data on server size before storing using google managed encryption keys.
Select data encryption type under encryption tab.
To learn more about data encryption options in google cloud refer Google Cloud Storage Encryption – Cloud KMS – Google Cloud Tutorials
We will use this virtual machine as web server for handling http requests.
Enable http and https traffic under firewall rules.
Specify live host migration to another VM in case of periodic maintenance activities.
Enable automatic restarts in case of hardware and software failures.
Review all the configurations and click on create button.
Create Virtual Machine using GCP Instance Template
Search Compute Engine in GCP console.
Navigate to instance templates tab.
You should see instance template created in step1.
Click on three dots in action and choose create VM.
Verify Virtual machine is using specified gcp instance template.
Verify if all configurations are prepopulated correctly and click on create VM.
To check new virtual machine status, navigate to virtual machine instances tab in left menu.
Create Manage Instance Group (MIG) using GCP Instance Template
Search Compute Engine in GCP console.
Navigate to instance templates tab.
ou should see instance template created in step1.
Click on three dots in action and choose create Instance Group.
In this tutorial we are creating stateless MIG. Stateless Manage Instance Group are used to host stateless workloads.
To learn more about stateless vs stateful MIG refer Instance groups | Compute Engine Documentation | Google Cloud.
Verify MIG is using specified gcp instance template.
Verify if all configurations are prepopulated correctly
Enable autoscaling for MIG.
Specify minimum and maximum instances to be used in auto scaling configuration.
Configure auto scaling signal.
In this tutorial we have used CPU Utilization for auto scaling signal.
Whenever the traffic on the workload increases and CPU Utilization crosses 60% new virtual machine would be added to be MIG.
This configuration supports maximum of 10 virtual machines.
When the traffic is less virtual machines are automatically removed from MIG.
This configuration supports minimum of 1 virtual machine.
At the end of demo remember to delete GCP project. If you are using existing GCP project delete MIG,virtual machines to prevent recurring charges from google cloud.
FAQ – GCP Instance Template
What is an instance template in GCP?
Instance template is global resource in Google Cloud platfom.It simplifies the process of Virtual machine and Manage Instance group configuration by prepopulating critical configuration and start up scripts.
How do I Create an instance template in GCP?
Refer step 1 of this article for more details.
Is instance template a global resource?
Yes. GCP Instance Template is global resource. If region- or zone-specific configuration is specified in instance template than it is tied to a zone or region.
Example: If an instance template is configured to a read-only persistent disk from us-central1-b
than your instance template is tied to us-central1-b
and cannot be used to configured resources outside of this zone.