DEV Community

Cover image for Part-9: πŸš€ Deploying a Web Server on Google Cloud VM with gcloud CLI (GCP)
Latchu@DevOps
Latchu@DevOps

Posted on

Part-9: πŸš€ Deploying a Web Server on Google Cloud VM with gcloud CLI (GCP)

Google Cloud provides multiple ways to manage infrastructure β€” you can use the Console UI, Cloud Shell, or the gcloud CLI. In this tutorial, we’ll use gcloud commands inside Cloud Shell to create a VM, install Nginx with a startup script, and manage lifecycle actions (start, stop, update, delete).


πŸ”Ή Step 1: Launch Cloud Shell

  • Open the Google Cloud Console β†’ Click on the Cloud Shell icon (top-right corner).
  • Authorize the shell when prompted.
  • You’ll now have a terminal directly connected to your GCP environment with gcloud pre-installed.

gcloud-shell


πŸ”Ή Step 2: Upload Startup Script

Create a file called webserver-install.sh and 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

Make it executable:

ls -lh
chmod +x webserver-install.sh

Enter fullscreen mode Exit fullscreen mode

This script installs Nginx and deploys a simple web page that displays hostname, IP address, and app version.

upload-script


πŸ”Ή Step 3: Configure gcloud Project

Set your active project:

gcloud config set project PROJECT_ID
gcloud config set project gcp-zero-to-hero-468909

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 4: Create a VM with Startup Script

Run the command to create a new VM and attach the startup script:

gcloud compute instances create demo3-vm-gcloud \
  --zone=us-central1-a \
  --machine-type=e2-micro \
  --network-interface=subnet=default \
  --tags=http-server \
  --metadata-from-file=startup-script=webserver-install.sh

Enter fullscreen mode Exit fullscreen mode

create-vm


πŸ”Ή Step 5: Manage the VM Lifecycle

πŸ‘‰ List instances

gcloud compute instances list

Enter fullscreen mode Exit fullscreen mode

list-vm

VM-console

πŸ‘‰ Stop the VM

gcloud compute instances stop demo3-vm-gcloud --zone=us-central1-a

Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Start the VM again

gcloud compute instances start demo3-vm-gcloud --zone=us-central1-a

Enter fullscreen mode Exit fullscreen mode

πŸ”Ή Step 6: Update Deletion Protection

πŸ‘‰ Enable deletion protection

gcloud compute instances update demo3-vm-gcloud \
    --zone=us-central1-a \
    --deletion-protection

Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Attempt to delete VM (this will fail 🚫)

gcloud compute instances delete demo3-vm-gcloud --zone=us-central1-a

Enter fullscreen mode Exit fullscreen mode

❌ Error:

ERROR: (gcloud.compute.instances.delete) Could not fetch resource:
 - Invalid resource usage: 'Resource cannot be deleted if it's protected against deletion.'

Enter fullscreen mode Exit fullscreen mode

failed-to-delete

πŸ‘‰ Disable deletion protection

gcloud compute instances update demo3-vm-gcloud \
    --zone=us-central1-a \
    --no-deletion-protection

Enter fullscreen mode Exit fullscreen mode

πŸ‘‰ Now delete the VM

gcloud compute instances delete demo3-vm-gcloud --zone=us-central1-a

Enter fullscreen mode Exit fullscreen mode

delete-vm


🎯 Final Thoughts

In this tutorial, we learned how to:

  • Use Cloud Shell with gcloud CLI
  • Deploy a VM with a startup script
  • Manage VM lifecycle: start, stop, list
  • Protect and safely delete VMs with deletion protection

This is a simple but powerful way to automate server setup on GCP. Instead of clicking through the console, you can script and repeat deployments with just a few commands.

Top comments (0)