DEV Community

Cover image for Part-11: 🚀Google Cloud Compute Engine: Working with Instance Templates (GCP)
Latchu@DevOps
Latchu@DevOps

Posted on

Part-11: 🚀Google Cloud Compute Engine: Working with Instance Templates (GCP)

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:

  1. Creating an Instance Template
  2. Launching a VM from the template
  3. Cloning templates using Create Similar
  4. 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

lt-1

  • Machine Configuration:
Series: E2
Machine Type: e2-micro
Enter fullscreen mode Exit fullscreen mode

lt-2

  • Availability Policies:
VM Provisioning Model: Standard
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode

lt-3

  • Firewall: check Allow HTTP traffic
  • Advanced Options → Management:
Description: demo4-vm-startupscript
Reservations: leave default
Enter fullscreen mode Exit fullscreen mode

lt-4

  • 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
Enter fullscreen mode Exit fullscreen mode

👉 Finally, click Create.

lt-5

lt-6


🔹 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

cvm-1


🔹 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>
Enter fullscreen mode Exit fullscreen mode

cvm-2


🔹 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
Enter fullscreen mode Exit fullscreen mode

🔹 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

cvm-3

cvm-4


🔹 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 

Enter fullscreen mode Exit fullscreen mode

cvm-5

cvm-6


âś… 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)