DEV Community

Cover image for Part-5: πŸš€ Implement Instance level Startup Script in Google Cloud Platform (GCP)
Latchu@DevOps
Latchu@DevOps

Posted on

Part-5: πŸš€ Implement Instance level Startup Script in Google Cloud Platform (GCP)

Google Cloud Startup Scripts allow you to run commands automatically when a Compute Engine VM instance boots. This is super useful for tasks like installing packages, configuring web servers, and setting up environments without manual effort.

In this tutorial, we’ll walk through creating a VM in Google Cloud Platform (GCP) and using a startup script to install Nginx and serve a custom HTML page.


πŸ›  Step 1: Enable the Compute Engine API

Before creating a VM, ensure that the Google Compute Engine API is enabled in your project.


πŸ›  Step 2: Create a VM Instance

Navigate to:

Google Cloud Console β†’ Compute Engine β†’ VM Instances β†’ Create Instance

Fill in the details as below:

  • Name: demo2-vm-startupscript
  • Labels: environment: dev
  • Region: us-central1
  • Zone: us-central1-a

machine-config

  • Machine Configuration:
  1. Series: E2
  2. Machine Type: e2-micro (free-tier eligible)

machine-config-1

  • Operating system and storage

os

  • Availability Policies: Standard provisioning
  • Display Device: unchecked (default)
  • Confidential VM Service: unchecked (default)
  • Container: unchecked (default)
  • Boot Disk: leave to defaults (Debian/Ubuntu recommended)
  • Identity & API Access:
  1. Service Account: Compute Engine default
  2. Access Scopes: Allow default access
  • Firewall: βœ… Allow HTTP traffic

πŸ›  Step 3: Add Startup Script

Expand Advanced Options β†’ Management β†’ Automation β†’ Startup Script

Paste the following script:

#!/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, 210);'> <h1>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

vm-script


πŸ›  Step 4: Configure Additional Settings

  • Description: demo2-vm-startupscript
  • Deletion Protection: βœ… Enable it
  • Reservations: leave default
  • Metadata: leave empty
  • Data Encryption: leave defaults

Finally, click Create.

Active-instance

πŸ›  Step 5: Access the webpage

The instances are running and the webpage is accessible through the browser.

access-webpage

πŸ›  Step 6: Delete the VM

If you select the VM, you’ll notice the Delete option is disabled. To remove it, go to VM > Edit > Deletion protection, uncheck Enable deletion protection, and save the changes.

disable-deletion-protection

Now you can safely delete this VM since it’s no longer needed.

delete-vm

Top comments (0)