Retrieve recommendation details across all GCP Projects

Photo by Karolina Grabowska from Pexels

On the home page of the google cloud project, you might have noticed a tab called recommendations. This tab contains the recommendation hub dashboard which provides details on the Unused compute engine resources, over-provisioned IAM role privileges, Underutilized & Overutilized VMs, Unutilized external ips ,etc. These notifications help us in running the cloud resources with optimized cost , performance & security. They can also be exported to BigQuery for further analysis and remediation actions.

Image from https://cloud.google.com/recommender/docs/recommendation-hub/getting-started

There are various recommenders available , the list is given below.

  • google.accounts.security.SecurityKeyRecommender
  • google.cloudsql.instance.OutOfDiskRecommender
  • google.compute.commitment.UsageCommitmentRecommender
  • google.iam.policy.Recommender
  • google.compute.image.IdleResourceRecommender
  • google.compute.address.IdleResourceRecommender
  • google.compute.disk.IdleResourceRecommender
  • google.compute.instance.IdleResourceRecommender
  • google.compute.instanceGroupManager.MachineTypeRecommender
  • google.logging.productSuggestion.ContainerRecommender
  • google.monitoring.productSuggestion.ComputeRecommender
  • google.compute.instance.MachineTypeRecommender
  • google.resourcemanager.projectUtilization.Recommender

In this post we are going to discuss about how we can use the recommender REST API to retrieve only the relevant information across all the GCP projects to a csv file, so out of the above recommenders , I have used “google compute instance machine type recommender” for this script. The same script can be re-used for other recommenders with little modifications.

The details of the script are given below

Step:1 — The shell script will create a csv file with the required headers for the csv and create the csv file.

Step:2 — Get the list of projects under your org

Step:3- Get the auth token for your account

Step:4 — Iterate through project and set the GCP project.

Step:5- Enable the recommender API in the project

Step:6 — Pass the project-name and auth-token to the python recommendation retrieval script

Step:7- Define the required zones based on where you have provisioned your cloud resources.

Step:8 — Using python requests library , get the data from the API endpoint — “https://recommender.googleapis.com/v1/projects/" + projectname + “/locations/”+ zone +”/recommenders/google.compute.instance.MachineTypeRecommender/recommendations”

Step:9 — Load the JSON response to retrieve the necessary details.

Step:10- Print them in the required order , so the shell script can take care of appending the csv file with the required data

Shell script file

Shell script to retrieve GCP recommendations

Python Script file

Python script to retrieve GCP recommendations

The generated csv file can be modified using MS Excel for further analysis, the sample document is given below for your reference.

Exported csv file — modified in Excel

Thanks for reading the post, I hope it was helpful.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Rajathithan Rajasekar

Rajathithan Rajasekar

I like to write code in Python . Interested in cloud , dataAnalysis, computerVision, ML and deepLearning. https://www.linkedin.com/in/rajathithan-rajasekar/