DEV Community

Cover image for Part-6: Implement Project Level Start-up scripts and Override it on Google Cloud Platform VM (GCP)
Latchu@DevOps
Latchu@DevOps

Posted on

Part-6: Implement Project Level Start-up scripts and Override it on Google Cloud Platform VM (GCP)

πŸ”Ή 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

Enter fullscreen mode Exit fullscreen mode

metadata-project-level


πŸ› οΈ 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.

webpage-access


πŸ› οΈ 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
Enter fullscreen mode Exit fullscreen mode

Click Create

πŸ‘‰ This VM will follow the VM-level script, ignoring the project-level one.

webpage-access


🧹 Step 4: Clean Up

Delete the VMs you created for testing.

Delete-VM

Remove the Project-Level Startup Script.

  • Go to Compute Engine β†’ Settings β†’ Metadata β†’ Edit
  • Delete the startup-script metadata key
  • Click Save

Top comments (0)