Exporting insights and recommendations to BigQuery

Overview

With the BigQuery export, you can view daily snapshots of recommendations for your organization. This is done using the BigQuery Data Transfer Service. The following insights and recommendations can be exported:

Name Recommender ID
IAM recommender google.iam.policy.Recommender
VM machine type recommender google.compute.instance.MachineTypeRecommender
Managed instance group machine type recommender google.compute.instanceGroupManager.MachineTypeRecommender
Idle PD recommender google.compute.disk.IdleResourceRecommender
Idle VM recommender google.compute.instance.IdleResourceRecommender

Before you begin

It is recommended that you complete these steps before you create a data transfer for recommendations:

  • Allow the BigQuery Data Transfer Service permission to manage your data transfer.If you use the BigQuery web UI to create the transfer, you must allow pop-ups from console.cloud.google.com on your browser to be able to view the permissions. For more details, see enable a BigQuery Data Transfer Service.
  • Create a BigQuery dataset to store data.
    • All recommendations are moved to a dataset in the US. This is because datasets only in the US are currently supported.
    • The data transfer uses the same region where the dataset is created. The location is immutable once the dataset and transfer are created.

Pricing

Exporting recommendations to BigQuery is currently available to all Recommender customers at no cost during Preview. Once it is generally available, customers can utilize it based on their Recommender pricing tier.

Required permissions

The following permissions are required at the project level under which you create a data transfer:

  • bigquery.transfers.update - Allows you to create the transfer
  • bigquery.datasets.update - Allows you to update actions on the target dataset

You must grant either of the following roles on the project under which you create the data transfer:

  • BigQuery admin role - roles/bigquery.admin
  • Custom roles containing the above permissions

The following permissions are required at the organization level to which your recommender is exported to.

  • recommender.exporter - Allows you to export recommendations to BigQuery

To grant this permission, you must run the following command:

gcloud organizations add-iam-policy-binding *<organization_id>* --member='user:*<user_name>*' --role='roles/recommender.exporter'

Limitations

The following limitations apply until the BigQuery export feature is generally available (GA):

  • To allow you to export recommendations to BigQuery, you can add the recommender.exporter permission only using gcloud.
  • The content overview data is not migrated yet.
  • Only project-level recommendations are currently exported. The export of billing account CUD recommendations will be supported at GA.

Creating a data transfer for recommendations

  1. Sign in to Google Cloud Console.

    Sign in to Cloud Console

  2. From the Home screen, click the Recommendations tab.

  3. Click Export to view the BigQuery export form.

  4. Select a Destination Project to store the recommendation data and click Next.

  5. Click Enable APIs to enable the BigQuery APIs for the export. This can take a several seconds to complete. Once done, click Continue.

  6. On the Configure Transfer form, provide the following details:

    • In the Transfer config name section, for Display name, enter a name for the transfer. The transfer name can be any value that allows you to easily identify the transfer if you need to modify it later.

    • In the Schedule options section, for Schedule, leave the default value (Start now) or click Start at a set time.

      • For Repeats, choose an option for how often to run the transfer.

        • Daily (default)
        • Weekly
        • Monthly
        • Custom
        • On-demand
      • For Start date and run time, enter the date and time to start the transfer. If you choose Start now, this option is disabled.

    • In the Destination settings section, for Destination dataset, choose the dataset ID you created to store your data.

    • In the Data source details section:

      • The default value for organization_id is the organization that you are currently viewing recommendations for. If you want to export recommendations to another organization, you can change this on top of the console in the organization viewer.

    • (Optional) In the Notification options section:

      • Click the toggle to enable email notifications. When you enable this option, the transfer administrator receives an email notification when a transfer run fails.
      • For Select a Pub/Sub topic, choose your topic name or click Create a topic. This option configures Pub/Sub run notifications for your transfer.

  7. Click Create to create the transfer.

  8. Click Allow on the consent pop-up.

  9. Once the transfer is created, you are directed back to the Recommendation Hub. You can click the link to access the transfer configuration details. Alternatively, you can access the transfers by doing the following:

    • Go to the BigQuery page in the Cloud Console.

      Go to the BigQuery page

    • Click Data Transfers. You can view all the available data transfers.

Viewing the run history for a transfer

To view the run history for a transfer, do the following:

  1. Go to the BigQuery page in the Cloud Console.

    Go to the BigQuery page

  2. Click Data Transfers. You can view all the available data transfers.

  3. Click the appropriate transfer in the list.

  4. In the list of run transfers displayed under the RUN HISTORY tab, select the transfer you want to view the details for.

  5. The Run details panel is displayed for the individual run transfer you selected. Some of the possible run details displayed are:

    • Deferred transfer due to unavailable source data.
    • Job indicating the number of rows exported to a table
    • Missing permissions for a datasource that you must grant and later schedule a backfill.

Viewing tables for a transfer

When you export recommendations to BigQuery, the dataset contains two tables that are partitioned by date:

  • recommendations_export
  • insight_export

For more details on tables and schema, see Creating and using tables and Specifying a schema.

To view the tables for a data transfer, do the following:

  1. Go to the BigQuery page in the Cloud Console.

    Go to the BigQuery page

  2. Click Data Transfers. You can view all the available data transfers.

  3. Click the appropriate transfer in the list.

  4. Click the CONFIGURATION tab and click the dataset.

  5. In the Explorer panel, expand your project and select a dataset. The description and details appear in the details panel. The tables for a dataset are listed with the dataset name in the Explorer panel.

Scheduling a backfill

Recommendations for a date in the past (this date being later than the date when the organization was opted in for the export) can be exported by scheduling a backfill. To schedule a backfill, do the following:

  1. Go to the BigQuery page in the Cloud Console.

    Go to the BigQuery page

  2. Click Data Transfers.

  3. On the Transfers page, click on an appropriate transfer in the list.

    1. Click Schedule backfill.

    2. In the Schedule backfill dialog, choose your Start date and End date.

For more information on working with transfers, see Working with transfers.

Example Queries

You can use the following sample queries to analyze your exported data.

Viewing cost savings for recommendations where the recommendation duration is displayed in days

SELECT name, recommender, target_resources,
  case primary_impact.cost_projection.cost.units is null
       when true then round(primary_impact.cost_projection.cost.nanos * power(10,-9),2)
       else
       round( primary_impact.cost_projection.cost.units +
       (primary_impact.cost_projection.cost.nanos * power(10,-9)), 2)
   end
   as dollar_amt,
   primary_impact.cost_projection.duration.seconds/(60*60*24) as duration_in_days
FROM `<project>.<dataset>.recommendations_export`
WHERE DATE(_PARTITIONTIME) = "<date>"
and primary_impact.category = "COST"

Viewing the list of unused IAM roles

SELECT *
FROM `<project>.<dataset>.recommendations_export`
WHERE DATE(_PARTITIONTIME) = "<date>"
and recommender = "google.iam.policy.Recommender"
and recommender_subtype = "REMOVE_ROLE"

Viewing a list of granted roles that must be replaced by smaller roles

SELECT *
FROM `<project>.<dataset>.recommendations_export`
WHERE DATE(_PARTITIONTIME) = "<date>"
and recommender = "google.iam.policy.Recommender"
and recommender_subtype = "REPLACE_ROLE"

Viewing insights for a recommendation

SELECT recommendations.name as recommendation_name,
insights.name as insight_name,
recommendations.cloud_entity_id,
recommendations.cloud_entity_type,
recommendations.recommender,
recommendations.recommender_subtype,
recommendations.description,
recommendations.target_resources,
recommendations.recommendation_details,
recommendations.state,
recommendations.last_refresh_time as recommendation_last_refresh_time,
insights.insight_type,
insights.insight_subtype,
insights.category,
insights.description,
insights.insight_details,
insights.state,
insights.last_refresh_time as insight_last_refresh_time
FROM `<project>.<dataset>.recommendations_export` as recommendations,
   `<project>.<dataset>.insights_export` as insights
WHERE DATE(recommendations._PARTITIONTIME) = "<date>"
and DATE(insights._PARTITIONTIME) = "<date>"
and insights.name in unnest(recommendations.associated_insights)

Viewing recommendations for projects belonging to a specific folder

This query returns parent folders up to five levels from the project.

SELECT *
FROM `<project>.<dataset>.recommendations_export`
WHERE DATE(_PARTITIONTIME) = "<date>"
and "<folder_id>" in unnest(ancestors.folder_ids)