Reduce your steps to set up a VM
Tags: googlecloud
Published: 2022-12-22 10:22:44

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5oo1wtjmj811omlznkgq.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/09w1hr78beykth41y3ay.png)

Give it a name
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m6qnn78e7129vf1aollr.png)

Allow the HTTP Traffic and choose this one.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qyyk26mv3y9as2g9crch.png)

Here under the management, you can find the startup script

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ejcv0d0j2hdhf67qvf25.png)

```
    #!/bin/bash
    apt update 
    apt -y install apache2
    echo "Hello world from $(hostname) $(hostname -I)" > /var/www/html/index.html
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dbws26p6nrfm3703o9da.png)
 and press create.
Our instance is launched.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/leb1s4w3zp5n6tkqsqn5.png)

Press this external Ip (Publicly available server)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/teit0fcj6d1j42rb0hks.png)

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jdgrda1xqckduukktpdc.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/apjui6mscmu04aptw1m8.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utulqi2hw2oiv5ttb1aw.png)

Give it a name:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y135ol0xcambsf9md0tm.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1e6rigatncf718olxw73.png)

Follow the management option and then startup script

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qypgdp4jfjuhktlslryz.png)

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

Add this one .

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bs2x7giwpy9uw74n4hs0.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3g3aq1oamtco4fxmzk95.png)

You can see most of the details provided from earlier.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jrfci1c4ktdjpz7fuedg.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ab3uah8fraktjrkvtip1.png)


**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2qumhoe9tmz5pcyj9x6x.png)

Now go to disks.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m2ony8m1aqh9bummn240.png)


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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7kb7wflyz2h0yuhr3kll.png)


After pressing the create image, you can see this one

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/umzyfsat6nrs7ca468m1.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ju3iyw2d8rtepg724si.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ph8yzpjfan3zaziwqgt2.png)

Change this boot disk and choose your custom image.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j7l0tk1zlxzihqpnusl0.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ox9igpokwv9xsq1omwbl.png)

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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/20g8u46m71aqhzbawkcm.png)

Press create now!

Now you can create instances using this instance templates.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/utbr0mzwoak3ybu967qv.png)


Done!