How to Create a Custom Image in GCP

Google Cloud IconGoogle Compute Engine (GCE), a core component of Google Cloud Platform (GCP), allows users to create and run virtual machines (VMs) on Google’s infrastructure. One of the key features of GCE is the ability to create **custom images** — snapshots of a VM’s disk that can be reused to deploy consistent environments across multiple instances. Custom images simplify VM deployment, enable disaster recovery, and facilitate quick scaling.
In this guide, we’ll walk you through the process of creating, managing, and deploying custom images in Google Compute Engine. This process not only streamlines VM replication but also improves operational efficiency.

Why Use Custom Images?

Creating a custom image is essential for maintaining consistency across virtual machines. Here are the primary reasons to use custom images:

  • Consistency Across Deployments: Custom images allow you to deploy identical VMs with the same software configuration, operating system, and dependencies. This ensures consistency across all environments, reducing deployment errors.
  • Faster Scaling and Recovery: By using pre-configured images, you can quickly spin up multiple VMs, making scaling easier during high-demand periods. Additionally, in case of failure, you can restore an identical VM instantly.
  • Custom Software Stacks: You can package and replicate your software environment by capturing it in a custom image. This is useful for distributed workloads, batch processing, or software testing.
  • Simplified Backup Strategy: Custom images serve as backups that capture your VM’s current state, protecting data and configurations against potential data loss or misconfigurations.

Benefits of Creating Images Directly from Disks

  • Speed: Eliminates the additional step of snapshot creation, reducing image creation time.
  • Cost Efficiency: Avoids snapshot storage costs by directly imaging the disk.
  • Simplicity: Fewer steps make the process quicker and easier to manage.
  • Live Capture: The image can be created even while the VM is running, ensuring minimal downtime.

Step-by-Step Guide: How to Create a Custom Image in GCP

1. Pre-Requisites

Before creating a custom image, ensure the following:

  • A running VM instance on Google Compute Engine
  • Admin permissions on the GCP project
  • Installed Google Cloud SDK (for CLI users)

2. Create a Custom Image in GCP

  • Log in to Google Cloud Console
    Visit the Google Cloud Console and select your project.
  • Go to Compute Engine
    • Navigate to Compute EngineDisks in the side menu.
  • Select the Disk to Image
    • Locate and click the persistent disk attached to the VM you want to image.
  • Create Image
    • Click the Create Image button at the top of the disk details page.
  • Configure the Image
    • Name: Provide a unique name for the image (e.g., custom-vm-image).
    • Source Disk: The disk you selected should automatically appear as the source.
    • Encryption: Choose between:
      • Google-managed encryption (default).
      • Customer-supplied encryption key (CSEK).
    • Location: Choose the image storage location (multi-region, dual-region, or single-region).
  • Create the Image
    • Click Create to initiate the image creation process

3. Create a Custom Image Using Command Line (gcloud)

  • List Available Disks
    gcloud compute disks list
  • Create an Image Directly from Disk
    gcloud compute images create my-custom-image \
    --source-disk my-instance-disk \
    --source-disk-zone us-central1-a
    • my-custom-image – Name of the image to be created.
    • my-instance-disk – Persistent disk attached to the VM.
    • us-central1-a – The zone where the disk is located.
  • Verify Image Creation
    gcloud compute images list

    The new image should now appear in the list.

4. Deploy VMs from Custom Image

  • Create a New VM from the Image (Console)
    • Navigate to Compute EngineVM InstancesCreate Instance.
    • In the boot disk section, select Custom Images.
    • Choose the newly created image and proceed with the VM configuration.
  • Deploy VM via Command Line
    gcloud compute instances create new-vm-instance \
    --image my-custom-image \
    --zone us-central1-a

Advanced Options for Custom Images

  • Image Families:Group images into families for easy versioning and updates. The latest image in the family is automatically selected during VM deployment.
    gcloud compute images create image-v2 \
    --source-disk my-disk \
    --family my-image-family
  • Automate Image Creation:Use Compute Engine’s VM Manager or Cloud Scheduler to automate daily snapshots.
    gcloud compute resource-policies create snapshot-schedule daily-backup \
    --max-retention-days 30 --daily-schedule 06:00
  • Encryption:Secure your images with custom encryption keys (CMEK).
    gcloud compute images create secure-image \
    --source-disk my-disk \
    --csek-key-file key.json

Best Practices for Custom Images

  • Minimize Image Size
    Remove unnecessary files and clear logs before creating a snapshot.

    sudo apt-get autoremove
    sudo apt-get clean
  • Use Image Families
    Group images logically (e.g., production, staging) to simplify management.
  • Regular Backups
    Automate snapshot creation and set retention policies to avoid data loss.
  • Monitor and Audit
    Enable Cloud Monitoring to track VM performance and audit access logs for custom images.

Here’s a guide on how to create a custom image directly from a VM disk without creating a snapshot in Google Compute Engine (GCE). This method simplifies the process by bypassing the snapshot step, creating the image directly from the disk.

3. Create a Custom Image from Disk Using gcloud Command-Line

  1. List Available Disks
    gcloud compute disks list
  2. Create an Image Directly from Disk
    gcloud compute images create my-custom-image \
    --source-disk my-instance-disk \
    --source-disk-zone us-central1-a
    • my-custom-image – Name of the image to be created.
    • my-instance-disk – Persistent disk attached to the VM.
    • us-central1-a – The zone where the disk is located.
  3. Verify Image Creation
    gcloud compute images list

    The new image should now appear in the list.

Best Practices

  • Stop the VM Before Imaging – Although live disk imaging is supported, stopping the VM reduces the risk of data corruption.
  • Label Images – Use labels to categorize images for better organization:
    gcloud compute images create my-custom-image \
    --source-disk my-instance-disk \
    --labels env=production,project=app1
  • Leverage Image Families – Group images under a family to ensure VM instances always use the latest available version.
  • Monitor and Automate – Use Cloud Monitoring and automation tools to create periodic images for disaster recovery.

Troubleshooting Tips

  1. Image Creation Fails:
    • Ensure the disk exists and is in the correct zone.
    • Check that the disk is not currently being deleted.
  2. Permission Denied:
    • Assign compute.admin IAM role to your account:
      gcloud projects add-iam-policy-binding my-project \
      --member=user:[email protected] \
      --role=roles/compute.admin
  3. Disk in Use:
    • Detach the disk from the running instance before imaging if necessary:
      gcloud compute instances detach-disk my-instance \
      --disk my-instance-disk --zone us-central1-a

Conclusion

Creating custom images in Google Compute Engine streamlines VM deployment, improves scalability, and enhances disaster recovery strategies. By following the steps outlined in this guide, you can efficiently create, manage, and deploy custom images, ensuring a consistent environment across your infrastructure.

Embrace automation, enforce security policies, and regularly update your images to maximize the value of your GCP infrastructure.

 

Related articles

Natural Language Processing AI​

Natural Language Processing AI​ Introduction to Natural Language Processing (NLP) Natural Language Processing (NLP) is a field of Artificial Intelligence...

What Are Kubernetes Images?

What Are Kubernetes Images? Kubernetes images are the backbone of containerized applications, encapsulating everything required to run software, including...

How to Archive and Compress Files with the tar and gizp Commands in Linux

📦 How to Archive, Compress, and Extract Files on Linux Using tar 📦 💻 Archiving, compressing, and extracting files...

Automating Containerized Workflows

Automating Containerized Workflows Containerization and orchestration are the cornerstones of modern DevOps workflows. Docker revolutionized how applications are packaged,...