In this guide, we’ll walk through creating and using Instance Templates in Google Cloud Compute Engine. Instance Templates allow you to define VM configurations once and reuse them for consistent, repeatable deployments.
We’ll cover:
- Creating an Instance Template
- Launching a VM from the template
- Cloning templates using Create Similar
- Using gcloud CLI to automate template creation
🔹 Step 01: Introduction
Here’s what we’ll do in this demo:
- âś… Create an Instance Template
- âś… Create a VM using the Instance Template
- âś… Clone the template using Create Similar option
- âś… Automate Instance Template creation using gcloud CLI
🔹 Step 02: Create an Instance Template
Navigate to:
- Compute Engine → Virtual Machines → Instance Templates → Create Instance Template
- Provide the following details:
- Name: demo4-instance-template
- Location: Regional
- Region: us-central1
- Machine Configuration:
Series: E2
Machine Type: e2-micro
- Availability Policies:
VM Provisioning Model: Standard
- Display Device: unchecked (default)
- Confidential VM Service: unchecked (default)
- Container: unchecked (default)
- Boot Disk: leave defaults
- Identity and API Access:
Service Account: Compute Engine default service account
Access Scopes: Allow default access
- Firewall: check Allow HTTP traffic
- Advanced Options → Management:
Description: demo4-vm-startupscript
Reservations: leave default
- Startup Script: copy-paste content from webserver-install.sh
#!/bin/bash
sudo apt install -y telnet
sudo apt install -y nginx
sudo systemctl enable nginx
sudo chmod -R 755 /var/www/html
HOSTNAME=$(hostname)
sudo echo "<!DOCTYPE html> <html> <body style='background-color:rgb(250, 210, 210);'> <h1>Welcome to Latchu@DevOps - WebVM App1 </h1> <p><strong>VM Hostname:</strong> $HOSTNAME</p> <p><strong>VM IP Address:</strong> $(hostname -I)</p> <p><strong>Application Version:</strong> V1</p> <p>Google Cloud Platform - Demos</p> </body></html>" | sudo tee /var/www/html/index.html
👉 Finally, click Create.
🔹 Step 03: Create a VM Instance using Instance Template
You can launch a VM using the Instance Template in two ways:
Option 1:
- Go to Compute Engine → VM Instances → Create Instance
- Select New VM instance from template → choose demo4-instance-template
- Click Continue
- Provide a VM name: demo4-vm-from-instance-template
- Verify all configurations are loaded from the template
- Click Create
Option 2:
- Go to Instance Templates → demo4-instance-template → Create VM
🔹 Step 04: Verify by Accessing Webserver Pages
- Once the VM is up, you can SSH into it for verification.
- Or simply test the webserver:
http://<external-ip-of-vm>
🔹 Step 05: Stop & Delete the VM
From VM Instances, select demo4-vm-from-instance-template and click Delete.
Or use the gcloud CLI:
gcloud compute instances delete demo4-vm-from-instance-template --zone us-central1-a
🔹 Step 06: Clone Template with “Create Similar”
- Navigate to Instance Templates → demo4-instance-template → Create Similar
- Name the new template: demo4-instance-template-v2
- Make necessary configuration changes and create
🔹 Step 07: Create Instance Template using gcloud CLI
Let’s automate template creation:
The "webserver-install.sh" file should be available in your cloudshell home directory
# Set Project
gcloud config set project PROJECT_ID
gcloud config set project gcplearn9
# Create Instance Template
gcloud compute instance-templates create demo4-it-v1 \
--machine-type=e2-micro \
--network-interface=network=default,network-tier=PREMIUM \
--instance-template-region=us-central1 \
--tags=http-server \
--metadata-from-file=startup-script=webserver-install.sh
âś… Wrapping Up
In this tutorial, we learned how to:
- Create an Instance Template in the Google Cloud Console
- Launch VMs using the template
- Clone templates with Create Similar
- Automate template creation with gcloud CLI
Top comments (0)