Ends in
00
hrs
00
mins
00
secs
SHOP NOW

💝 48-Hour Valentine's Sale Extension! Get 30% OFF Any Reviewer. Use coupon code: VDAYSALE2026 & 5% OFF Store Credits/Gift Cards

Back to Course

GCP PlayCloud Labs

0% Complete
0/0 Steps
  1. GCP PlayCloud Labs
  2. Guided Lab: How to Launch a GCP Compute Engine Linux Instance
  3. Guided Lab: Creating a VM Using Instance Templates
  4. Guided Lab: Creating and Managing Instance Groups in Compute Engine
  5. Guided Lab: Creating a Spot VM Instance
  6. Guided Lab: Configuring Shielded VM Options
  7. Guided Lab: Exploring Instance Metadata in Google Cloud
  8. Guided Lab: Vertically Scaling a VM Instance
  9. Guided Lab: Setting up a Web Server on a VM Instance
  10. Guided Lab: Using Startup Scripts in GCP VM Instances
  11. Guided Lab: Creating a Custom Image from a VM Instance with Web Server in Google Cloud
  12. Guided Lab: Creating VM Snapshots and Restoring a VM from a Snapshot
  13. Guided Lab: Setting Up and Managing a Database on a VM Instance
  14. Guided Lab: Installing WordPress on an Ubuntu VM Instance with LEMP Stack
  15. Guided Lab: Deploying a LAMP Stack on a Compute Engine VM
  16. Guided Lab: Reserving or Promoting a Static IP Address for a VM Instance
  17. Guided Lab: SSH Access to GCP VM Instance from Local Machine using SSH Key Pair
  18. Guided Lab: Guarding Your VM with Deletion Protection
  19. Guided Lab: Setting Up a Linux Bastion Host on GCP
  20. Guided Lab: Creating a Cloud Storage Bucket
  21. Guided Lab: Uploading, Organizing, and Managing Objects in Cloud Storage
  22. Guided Lab: Exploring Google Cloud Storage Classes
  23. Guided Lab: Hosting a Static Website in Google Cloud Storage Bucket
  24. Guided Lab: Protecting Data on Cloud Storage Bucket Against Accidental Delete and Overwrite Using Object Versioning
  25. Guided Lab: Using Cloud Storage Lifecycle Rules to Automate Object Management
  26. Guided Lab: Managing Cloud Storage Buckets via SSH Commands
  27. Guided Lab: Creating a Cloud SQL Instance
  28. Guided Lab: Running SQL Commands in Cloud SQL Studio
  29. Guided Lab: Creating and Restoring Cloud SQL Backups
  30. Guided Lab: Integrating Cloud SQL Database instance with a VM instance
  31. Guided Lab: Connecting Cloud SQL Database with MySQL Workbench (Local)
  32. Guided Lab: Guarding Your Cloud SQL Instances with Deletion Protection
  33. Guided Lab: Creating a Cloud NAT Gateway
  34. Guided Lab: Creating a Google Kubernetes Engine (GKE) Cluster
  35. Guided Lab: Connecting to a Kubernetes Engine Cluster
  36. Guided Lab: Deploying a Simple Web Application on GKE
  37. Guided Lab: Creating a Custom Virtual Private Cloud (VPC)
  38. Guided Lab: Establishing VPC Peering for Secure Cross‑Network Communication
  39. Guided Lab: Configuring Firewall Rules to Secure and Access a VM
  40. Guided Lab: Creating an Application Load Balancer
  41. Guided Lab: Creating a Network Load Balancer
Lesson 10 of 41
In Progress

Guided Lab: Using Startup Scripts in GCP VM Instances

Description

A startup script is a file that executes tasks when a virtual machine (VM) instance boots. It can be applied either to all VMs within a project or to a specific VM. If both project‑level and VM‑level scripts are defined, the VM‑level script takes precedence. Startup scripts run only when network connectivity is available.

In this lab, you will learn how to configure a Google Cloud Platform (GCP) VM instance to automatically run a startup script upon boot. The script will install and configure Nginx, serve a custom HTML page, and apply a basic server configuration.

Prerequisites

To ensure the successful completion of this lab, you must have prior experience in creating VM instances and be familiar with their essential components. If you feel that your knowledge in this area is insufficient, we highly recommend taking this lab to gain the necessary understanding:

Objectives

In this lab, you will:

  • Launch a Compute Engine VM instance with a custom startup script.
  • Automatically install and configure Nginx.
  • Serve a custom HTML page via HTTP.
  • Verify the deployment using the VM’s external IP address.
  • Inspect the VM’s metadata to confirm that the startup script is correctly applied.

Lab Steps

Create a Compute Engine VM Instance

1. Create a VM instance using the following machine configurations:

  • Name: Enter your desired instance name.
  • Machine type: Choose e2-micro (Preset)

2. Under OS and storage, click the Change button to modify the Boot disk:

  • Operating System: Ubuntu
  • Version: 24.04 LTS Minimal (x86/64, amd64 noble minimal image built on 2025‑12‑17)
  • Click Select once done.

3. Under Network → Firewall, check:

  • Allow HTTP traffic

4. Navigate to the Advanced section.

5. In the Startup script field, paste the following script:

# Update system
sudo apt update -y

# Install Nginx
sudo apt install nginx -y

# Start Nginx
sudo systemctl start nginx

# Create a custom HTML page in the HTML directory
echo '<h1>Welcome to my web page!</h1>' | sudo tee /usr/share/nginx/html/mypage.html > /dev/null

# Add the configuration directly
echo 'server {
    listen 80 default_server;
    server_name _;
    root /usr/share/nginx/html;
    
    location / {
        index mypage.html;
    }
}' | sudo tee /etc/nginx/conf.d/server.conf > /dev/null

# Remove the default site to avoid duplicate default_server errors:
sudo unlink /etc/nginx/sites-enabled/default

# Test and Reload Nginx for the changes to take effect
sudo nginx -t && sudo service nginx reload

# Enable Nginx at boot
sudo systemctl enable nginx

6. Leave other configurations at their default values.

7. Once done, click the Create button.

Wait for the VM to finish provisioning.

Verify the Setup

1. Once the VM is running, copy its External IP address.

2. Open a browser and navigate to http://<EXTERNAL_IP>.

3. You should see the custom page: “Welcome to my web page!”

If the page does not load:

  • Refresh the browser.
  • Ensure the firewall rule Allow HTTP traffic is enabled.
  • SSH into the VM and run: systemctl status nginx to confirm Nginx is running.
Verify the Startup Script via Metadata

1. Connect to your VM using SSH from the Cloud Console.

2. Run the following command to retrieve the startup script stored in the VM’s metadata:

curl -H "Metadata-Flavor: Google" \
  http://metadata.google.internal/computeMetadata/v1/instance/attributes/startup-script

Congratulations! You have automated the setup of a VM with a startup script, seamlessly installed and configured Nginx, and deployed a custom HTML page over HTTP. This exercise highlights how startup scripts can simplify server provisioning in GCP by ensuring services are installed, configured, and ready to run the moment your instance boots.

Skip to content