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
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
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
Test the app in the browser:
http://<external-ip-of-vm>
http://<external-ip-of-vm>/midemo.html
๐น 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.
๐น 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.
๐น 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
๐น Step 6: Verify Webserver
Access web pages of the new VM:
http://<external-ip-of-vm>
http://<external-ip-of-vm>/midemo.html
โ 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
๐น 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
โ 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)