DEV Community

Shahriyar Al Mustakim Mitul
Shahriyar Al Mustakim Mitul

Posted on

Reduce your steps to set up a VM

You can either choose
Start Script or Instance Template or Custom Images to reduce your manual VM setup process.

Let's start step by step:

Startup Script

Image description

Let's do it in GCP. Head over to VM Instances as we have no VM created.

Image description

Give it a name
Image description

Allow the HTTP Traffic and choose this one.

Image description

Here under the management, you can find the startup script

Image description

    #!/bin/bash
    apt update 
    apt -y install apache2
    echo "Hello world from $(hostname) $(hostname -I)" > /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

This is what we are going to use.

We will first update the os and then install apache. The write this simple Hello world and hostname with its ip address.

Here hostname's ip address is actually the internal IP.

Image description
and press create.
Our instance is launched.

Image description

Press this external Ip (Publicly available server)

Image description

Here you can find the web page with Internal IP mentioned as previously mentioned.

Image description

Image description

Done!
Instance Template
When you want to launch multiple instances, its tough to launch instances all time manually. Thus you can create a template and use it to launch others.

Note:you can not change a instance template after creating that one.

Go to instance templates

Image description

Give it a name:

Image description
Note: You don't need to provide regions and zones here as it can be used to create Vm instances in any region and zones.

Remain others in default and allow the HTTP traffic.

Image description

Follow the management option and then startup script

Image description

    #!/bin/bash
    apt update 
    apt -y install apache2
    echo "Hello world from $(hostname) $(hostname -I)" > /var/www/html/index.html
Enter fullscreen mode Exit fullscreen mode

Add this one .

Image description

Then press create.
Tips: There is no cost for creating an instance template. But you will pay once you use instance templates to create a VM.

Let's create one using Instance template .
Image description

You can see most of the details provided from earlier.

Image description

Although you can customize it once creating the VM.

This is the VM we created using the VM instances.
Press the external IP to see our script doing magic for us.

Image description

Custom Images
The problem for Instance templates is that, every time one instance is created, the script starts working and it takes time.

But to solve this issue, we can create an instance and make it an image.

Then we can use this image for all of our instances.

Image description

Now go to disks.

Image description

Disks are the OS attached with your VM instances. We have this disk as we have 1 vm instances already created. That time , this disk got created as well.

As we created an instance previously and got this disk with it. We will create a copy of this disk to make our custom image.

Image description

After pressing the create image, you can see this one

Image description

Note: You should not create an image from a disk which is connected with a running instance.

Once you create the image, you can us it to create instances.

Image description

Also you can use instance template using this image. And again create instances.

Copy the instance template we have and then modify it with image we just created.

Image description

Change this boot disk and choose your custom image.

Image description

Image description

You can also modify the script and remove the line causing install in our instances.

Image description

Press create now!

Now you can create instances using this instance templates.

Image description

Done!

Top comments (0)