DEV Community

Cover image for Part-13: ๐Ÿš€ Google Cloud Platform โ€“ How to Create and Use Machine Images for VM Deployment (GCP)
Latchu@DevOps
Latchu@DevOps

Posted on

Part-13: ๐Ÿš€ Google Cloud Platform โ€“ How to Create and Use Machine Images for VM Deployment (GCP)

When working with Google Compute Engine (GCE), you may need to back up, clone, or quickly recreate a VM with the exact same configuration and data. Thatโ€™s where Machine Images come in handy.

In this tutorial, weโ€™ll explore how to create, use, and manage Machine Images in GCP using both the Cloud Console and the gcloud CLI.


๐Ÿ”น Step 1: Introduction

A Machine Image contains:

  • VM properties, metadata, permissions
  • Data from attached disks
  • Full VM configuration

๐Ÿ‘‰ You can use it to:

  • Create a new VM instance
  • Backup an existing VM
  • Restore a VM in case of failure

๐Ÿ”น Step 2: Create a VM Instance

First, letโ€™s create a VM that weโ€™ll later capture into a Machine Image.

# Set Project
gcloud config set project PROJECT_ID
gcloud config set project gcpdemos

# Create VM Instance
gcloud compute instances create demo5-vm \
  --zone=us-central1-a \
  --machine-type=e2-micro \
  --network-interface=subnet=default \
  --tags=http-server

Enter fullscreen mode Exit fullscreen mode

ci-1

ci-2


Now, connect to the VM via SSH in browser, upload the webserver-install.sh script, and run it:

#!/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

To change the file permission and run it

chmod 755 webserver-install.sh
./webserver-install.sh
curl localhost
sudo echo "Machine Images Demo 101" | sudo tee /var/www/html/midemo.html
curl localhost/midemo.html
Enter fullscreen mode Exit fullscreen mode

ci-3

Test the app in the browser:

http://<external-ip-of-vm>
http://<external-ip-of-vm>/midemo.html
Enter fullscreen mode Exit fullscreen mode

ci-4

ci-5


๐Ÿ”น Step 3: Create Machine Image

From the Console:

  • Go to Compute Engine โ†’ VM Instances โ†’ demo5-vm โ†’ Create new Machine Image
  • Name: demo5-vm-machine-image
  • Location: Multi-regional (us)
  • Encryption: Google-managed key (default)
  • Review advanced settings โ†’ Click Create

โณ Takes ~5โ€“10 minutes.

ci-6


๐Ÿ”น Step 4: Verify Properties

Go to:
Compute Engine โ†’ Machine Images โ†’ demo5-vm-machine-image

Check details like: machine type, metadata, tags, boot disk, service account, etc.

ci-7


๐Ÿ”น Step 5: Create VM from Machine Image

You can create a VM directly from the machine image:

  • Option 1: VM Instances โ†’ Create Instance โ†’ From Machine Image
  • Option 2: Machine Images โ†’ demo5-vm-machine-image โ†’ Actions โ†’ Create Instance

Name it: demo5-vm-from-machine-image

ci-8


๐Ÿ”น Step 6: Verify Webserver

Access web pages of the new VM:

http://<external-ip-of-vm>
http://<external-ip-of-vm>/midemo.html

Enter fullscreen mode Exit fullscreen mode

ci-9

ci-10

โœ… Observations:

index.html and midemo.html content remain the same as the source VM.

VM hostname and private IP in the static page will still show values from the original VM (demo5-vm) because the Machine Image is a clone.

No need to reinstall appsโ€”Machine Images save time by reusing pre-installed software.


๐Ÿ”น Step 7: Create Machine Image using gcloud CLI

# Create Machine Image
gcloud compute machine-images create demo5-vm-machine-image-gcloud \
  --source-instance=demo5-vm \
  --source-instance-zone=us-central1-a \
  --storage-location=us  

# List Machine Images
gcloud compute machine-images list  

# Delete Machine Images
gcloud compute machine-images delete demo5-vm-machine-image
gcloud compute machine-images delete demo5-vm-machine-image-gcloud

Enter fullscreen mode Exit fullscreen mode

๐Ÿ”น Step 8: Cleanup

Stop and delete the VM instances to avoid extra charges:

# Delete VM instances
gcloud compute instances delete demo5-vm --zone us-central1-a
gcloud compute instances delete demo5-vm-from-machine-image --zone us-central1-a

Enter fullscreen mode Exit fullscreen mode

โœ… Key Takeaways

Machine Images capture everything about a VM (config + disks).

Perfect for backups, cloning, replication, and DR (disaster recovery).

Can be created and restored via Console, gcloud CLI, or REST API.

Saves time and ensures consistency across environments.

Top comments (0)