Simplifying VM creation with Instance Templates | Scribe

    Simplifying VM creation with Instance Templates

    • Hafeez Baig |
    • 21 steps |
    • 2 minutes
    1
    Sign in into the **Google Cloud Platform**
    2
    Type "**Compute Engine**" in the search bar and click on the **Compute Engine** option
    information ordinal icon
    **What is Compute Engine?** Google Compute Engine (GCE) is an Infrastructure-as-a-Service (IaaS) offering from Google Cloud Platform (GCP) that allows users to run virtual machines (VMs) on Google's infrastructure. It provides scalable and flexible computing resources, high-performance persistent storage, advanced networking capabilities, and integration with other GCP services, making it suitable for a variety of workloads including web hosting, data processing, and high-performance computing.
    3
    Click on the **Instance templates** option from the left bar
    information ordinal icon
    **What are Instance Templates?**\ \ Instance templates in Google Cloud Platform (GCP) are resource configurations that define the settings for virtual machine (VM) instances. These templates include details such as the machine type, boot disk image, network settings, and metadata. By using instance templates, you can easily create multiple VM instances with the same configuration, ensuring consistency and simplifying the management of large-scale deployments. They are particularly useful for managed instance groups, which use these templates to create and manage identical instances.
    4
    On the **Instance templates** wizard, click on the **CREATE INSTANCE TEMPLATE** button
    5
    **Create an instance template** wizard will open, give the Name as - "**my-instance-template-with-startup-script**"
    6
    Scroll to the **Machine configuration** section, select the **General Purpose** option and select **E2** for **Series**
    7
    Scroll to the **Firewall** section, checkbox the **Allow HTTP traffic** option
    information ordinal icon
    **What is the Allow HTTP traffic option?**\ \ The "Allow HTTP traffic" option in Google Cloud Platform (GCP) is a firewall setting for virtual machine instances. When enabled, it creates a firewall rule that permits incoming HTTP traffic (port 80) to the VM, allowing web servers hosted on the VM to be accessible via the HTTP protocol.
    8
    Scroll to the **Advance options** section and click on the dropdown option
    9
    Scroll to the **Management** section and click on the dropdown option
    10
    Scroll to the **Automation** section and enter the script in the **Startup script** text input field\ \ [[#!/bin/bash ]]\ [[apt update apt -y install apache2 ]]\ [[echo "Hello World from $(hostname) $(hostname -i)" > /var/www/html/index.html]]
    information ordinal icon
    The Automation script performs the following tasks: 1. `#!/bin/bash`: Specifies that the script should be run in the Bash shell. 2. `apt update`: Updates the package list on the system to ensure you have the latest information on available packages. 3. `apt -y install apache2`: Installs the Apache2 web server without prompting for confirmation, making the process non-interactive. 4. `echo "Hello World from $(hostname) $(hostname -i)" > /var/www/html/index.html`: Creates a "Hello World" web page that includes the hostname and IP address of the machine, and writes it to the default web directory. **In summary**, this script: - Updates the package list. - Installs the Apache2 web server. - Sets a custom "Hello World" page that displays the machine's hostname and IP address. This process ensures your web server is set up and verifies its functionality with a personalized message.
    11
    Scroll the page and click on the **CREATE** button
    12
    On the top right side click on the **Notifications** icon, here you can view that the Instance Template has been successfully created
    information ordinal icon
    Congratulations you have successfully created a VM instance with the Web Server installed in it with advance automation script
    13
    On the Instance templates wizard, click on the three dots on the right side and click on the **Create VM** option **Note:** We are creating a VM using the Instance template that we just created.
    14
    **Create an instance** wizard will open, the Name text input field is prepopulated **Note:** You can modify and change the VM Name as you need.
    15
    Scroll to the **Automation** section, here you can see that the **Startup script** text input field is prepopulated **Note:** It's prepopulated because we have already added this Automation script.