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.
πΉ 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
Make it executable:
ls -lh
chmod +x webserver-install.sh
This script installs Nginx and deploys a simple web page that displays hostname, IP address, and app version.
πΉ Step 3: Configure gcloud Project
Set your active project:
gcloud config set project PROJECT_ID
gcloud config set project gcp-zero-to-hero-468909
πΉ 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
πΉ Step 5: Manage the VM Lifecycle
π List instances
gcloud compute instances list
π Stop the VM
gcloud compute instances stop demo3-vm-gcloud --zone=us-central1-a
π Start the VM again
gcloud compute instances start demo3-vm-gcloud --zone=us-central1-a
πΉ Step 6: Update Deletion Protection
π Enable deletion protection
gcloud compute instances update demo3-vm-gcloud \
--zone=us-central1-a \
--deletion-protection
π Attempt to delete VM (this will fail π«)
gcloud compute instances delete demo3-vm-gcloud --zone=us-central1-a
β Error:
ERROR: (gcloud.compute.instances.delete) Could not fetch resource:
- Invalid resource usage: 'Resource cannot be deleted if it's protected against deletion.'
π Disable deletion protection
gcloud compute instances update demo3-vm-gcloud \
--zone=us-central1-a \
--no-deletion-protection
π Now delete the VM
gcloud compute instances delete demo3-vm-gcloud --zone=us-central1-a
π― 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)