How to Create GCP Image using GCLOUD Command

Manish Sharma
2 min readFeb 16, 2022

In this tutorial I will walk you through the steps involved in creating custom GCP image using GCLOUD command. Using GCLOUD command you can think of automation.

Create Virtual Machine

If you have Virtual Machine already created then you can skip this step.

To create Virtual Machine in GCP your first have to enable Compute Engine API which you can do from GCP console by clicking on ENABLE button or also using GCLOUD command.

GCP Console

GCLOUD Command

  1. Enter the following to display the project IDs for your Google Cloud projects:
$ gcloud projects list
output:
PROJECT_ID: blogging-project-341505
NAME: Blogging Project
PROJECT_NUMBER: 404228972898

2. Using the applicable project ID from the previous step, set the default project to the one in which you want to enable the API:

$ gcloud config set project <PROJECT_ID>
ex: gcloud config set project blogging-project-341505

3. Get a list of services that you can enable in your project:

$ gcloud service list --available | grep 'compute'
output:
NAME: compute.googleapis.com
NAME: prisma-cloud-compute-paloaltonetworks-public.cloudpartnerservices.goog

4. If you don’t see the API listed, that means you haven’t been granted access to enable the API.

5. Enable the service:

$ gcloud services enable <service_name>
ex: gcloud services enable compute.googleapis.com
Output:
Operation "operations/acf.p2-404228972898-59736071-f89c-4eee-b11c-dd028315efcf" finished successfully.

6. Validate service is enabled

$ gcloud services list --enabled | grep 'compute.googleapis.com'
ex: gcloud services list --enabled | grep 'compute.googleapis.com'
Output:
NAME: compute.googleapis.com

7. Validate from GCP Console. You can see ENABLE button is changed with MANAGE which means Compute Engine API is enabled.

Create Custom GCP Image using GCLOUD Command

  • Create a VM and install require softwares.
  • Ensure that after installation you Stop the instance (if it is not already stopped) else your custom image will not be consistent with your source disk.
  • Open Cloud Shell (the icon is in the blue bar at the top of the page)
  • Enter command to create image from the vm-wordpress disk, name the image “image_wordpress_<DDMMYYYY>”, and add to the wordpress image family
  • gcloud compute images create image_wordpress_16022022 --source-disk=vm-wordpress --source-disk-zone=us-central1-a --family=wordpress
  • Verify that image is created and in the appropriate family using the web console
  • You can view created image from web console or family information from command line using gcloud command:
  • In web console, go to Compute Engine > Images
  • GCLOUD Command: To describe image family and view latest image, enter command:
  • gcloud compute images describe-from-family wordpress

--

--

Manish Sharma

I am technology geek & keep pushing myself to learn new skills. I am AWS Solution Architect — Associate, Professional & Terraform Associate Developer certified.