Google Cloud Recommender

2.21.0 · active · verified Fri Apr 17

The `google-cloud-recommender` client library provides a Python interface for the Google Cloud Recommender API. This service delivers proactive recommendations and insights to help users optimize costs, improve performance, enhance security, and achieve operational excellence across their Google Cloud resources. The library is currently at version 2.21.0 and follows Google's frequent release cadence, often receiving updates multiple times a month as part of the broader `google-cloud-python` monorepo.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the `RecommenderClient` and list recommendations for a specific recommender (e.g., IAM policy recommendations) within a Google Cloud project. It uses environment variables for project ID, which is a best practice for authentication. Remember to enable the Recommender API and set up appropriate IAM permissions (e.g., `recommender.recommendations.list`).

import os
from google.cloud import recommender_v1

# Set your Google Cloud Project ID and a specific location
project_id = os.environ.get('GCP_PROJECT_ID', 'your-project-id')
location = 'global' # Recommender often uses 'global' or a specific region

# Initialize the client
client = recommender_v1.RecommenderClient()

# Define the recommender name (e.g., for IAM policy recommendations)
# Common recommenders: google.iam.policy.Recommender, google.compute.instance.MachineTypeRecommender
recommender_name = f"projects/{project_id}/locations/{location}/recommenders/google.iam.policy.Recommender"

# List recommendations for a given recommender
try:
    # Iterate through the paginated results
    for recommendation in client.list_recommendations(parent=recommender_name):
        print(f"Recommendation ID: {recommendation.name.split('/')[-1]}")
        print(f"Description: {recommendation.description}")
        print(f"State: {recommendation.state_info.state.name}")
        if recommendation.primary_impact:
            print(f"Primary Impact: {recommendation.primary_impact.category.name}")
        print('---\n')
except Exception as e:
    print(f"An error occurred: {e}")
    print("Ensure you have `recommender.recommendations.list` permission and `GCP_PROJECT_ID` is set correctly.")

view raw JSON →