0

In google cloud ,we mark custom images deprecated after a specific time. how can we get report of the VM instances which are using the deprecated image?

thanks

1 Answer 1

0

In order to view all the deprecated images available in GCE, as seen in Debian's documentation use the below command:

gcloud compute images list --project=name-of-your-project --no-standard-images --show-deprecated

Then view source image with

gcloud compute disk list

Use the disk describe command with a --format flag to filter the output.

gcloud compute disks describe disk_name --zone zone_1 --format='get(sourceImage)'

Creating a script that checks all the deprecated images with all disks used by VMs could do the trick

for project in $(gcloud projects list --format "value(project_id)"); 
  do if [ $(gcloud services list --filter 'config.name="compute.googleapis.com"' --project $project --format 'value(name)' ) ]; 
    then for instance in $(gcloud compute instances list --format "value(selfLink)" --project $project); 
      do for sources in $(gcloud compute instances describe $instance --project $project --format "value(disks.source)" ); 
        do for source in $(echo $sources | tr \; \\n);
          do sourceimage=$(gcloud compute disks describe $source --project $project --format='get(sourceImage)' ); 
           if [ ! -z $sourceimage ];
             then state=$(gcloud compute images describe $sourceimage --project $project --format "value(deprecated.state)" 2>/dev/null);
               if [ ! -z $state ];
                 then if [ $state == DEPRECATED ];
                   then echo $instance;
               fi;
             fi;
           fi;
        done;
      done;
    done;
  fi;
done | sort | uniq

Keep in mind that it will need some time because the loop has to go project by project.

2
  • thanks @pedro, this is great, but trying to run it across projects gives error on the Project which doesnot have Compute engine api enabled. can we add a condition where the script runs on the projects with compute engine enabled?
    – Mithun
    Jun 8, 2021 at 8:08
  • I modified the script with the changes suggested.
    – Pit
    Jun 14, 2021 at 10:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .