Google compute Engine (GCE) - Startup Script
In this lab we are going to use startup script and launch VM instances.
Startup script :- It is a set of code which is used to perform certain task during when we launch our VM instances.
For example:- When we run a script using the command line to host our application. It would be great if you could paste your set of codes at the beginning itself so you will get a direct result each time a new VM instance is launched.
Step 1:- On top left corner, Expand services, select compute Engine and click on VM instances.
Step 2:- Will create a new instance for startup script.
Step 3:- Name your instance, choose your region and zone according to your wish plus choose machine type, For now I chose smallest machine type for this lab.
Step 4:- Scroll down Under Firewall Allow HTTP traffic and Allow HTTPS traffic. So that the webpage can accept traffic requests.
Step 5:- Expand Advanced options and Under Management, Paste your code under startup script and click on create to launch your VM instance.
#!/bin/bash
apt update
apt -y install apache2
echo "Hello world from $(hostname) $(hostname -I)" > /var/www/html/index.html
#!/bin/bash - sharp(#) and bang(!) in short it's called she-bang it is used to set bash basically this line will set for user login in linux system.
apt - apt is a command which is used to install, update any software packages for your linux system.
apt -y install apache2 - so here we are installing apache2 web server to serve our web page.
echo "Hello world from $(hostname) $(hostname -I)" > /var/www/html/index.html
echo - this command will display text which is under a file.
$(hostname) - It should show my VM instance name.
$(hostname -I) - This part will show my VM instance internal IP address.
/var/www/html/index.html - So this a default path directory most web server uses to host webpage inside which our html file lies.
Step 6:- Once your instance up and ready, Make sure to refresh the instance and copy the IP address and paste it in your web browser it should work.
Note:- During the startup process, your Startup script will take some seconds to work, so wait for a bit before copying and pasting your IP address.
Sometimes it might not work if you click on your IP address. Therefore, it's best to copy and paste your IP address.
finally we hosted our page using startup script.
That's it, for now, we will continue further more in next blog.
Hope you liked it. Will see you on the next topic ๐
Next Topic
https://bvakash.hashnode.dev/google-compute-engine-gce-instance-template
Subscribe to my newsletter
Read articles from B V AKASH directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
B V AKASH
B V AKASH
I design, architect, and maintain hybrid cloud environments.