πΉ What is a Project-Level Startup Script?
A startup script is a script that runs automatically whenever a VM boots.
- VM-Level Startup Scripts apply only to the specific VM.
- Project-Level Startup Scripts apply to all VMs within that project by default.
π Use Project-Level scripts when you want to enforce common configurations across all your VMs (e.g., install antivirus, logging agents, monitoring agents, etc.).
π οΈ Step 1: Define Project-Level Startup Script
- Go to Compute Engine β Settings β Metadata
- Click on Edit
- Add Metadata Key: startup-script
- Add Metadata Value:
#!/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, 310);'> <h1>PROJECT-LEVEL STARTUP SCRIPT 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
π οΈ Step 2: Create a New VM to Test
- Go to VM Instances β Create Instance
- Name: projectlevel-startuscript-demo
- Machine Type: e2-micro
- Firewall: Allow HTTP traffic
- Leave all other settings as default
- Click Create
π Once the VM starts, open the external IP in your browser β you should see the custom HTML page deployed by the project level startup script.
π οΈ Step 3: Override Project-Level Startup Script
What if you donβt want the project-level script applied to a VM?
Easy β just provide a VM-level startup script, which will override the project-level one.
Create another VM
- Name: override-projectlevel-startuscript
- Machine Type: e2-micro
- Firewall: Allow HTTP traffic
- Management β Automation β Startup Script β paste the contents of your custom override-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(2, 210, 210);'> <h1>OVERRIDE STARTUP-SCRIPT - 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
Click Create
π This VM will follow the VM-level script, ignoring the project-level one.
π§Ή Step 4: Clean Up
Delete the VMs you created for testing.
Remove the Project-Level Startup Script.
- Go to Compute Engine β Settings β Metadata β Edit
- Delete the startup-script metadata key
- Click Save
Top comments (0)