You can query assets in your project, folder, or organization using a BigQuery SQL-compatible dialect.
Before you begin
Enable the Cloud Asset Inventory API in the project you're running Cloud Asset Inventory commands from.
Make sure your account has the correct role to call the Cloud Asset Inventory API. For individual permissions for each call type, see Permissions.
Limitations
Asset type names have
.and/replaced with_. For example,compute.googleapis.com/Instancebecomescompute_googleapis_com_Instance.The request must be a
SELECTquery.Legacy BigQuery SQL isn't supported.
Query results larger than 10GB aren't supported.
Query results larger than 10MB or 1,000 rows are always paginated.
Query results can only be exported to a BigQuery dataset in the
USmulti-region.
Tables you can query
You can query the following tables:
For
RESOURCEcontent types, table names in the dataset correspond to the asset type name, assuming that asset type exists. For example, thecompute_googleapis_com_Instancetable contains Compute Engine instance metadata.For non-
RESOURCEcontent types, table names in the dataset correspond to the RPC/REST content type names. For example,ACCESS_POLICY.To query resource standard metadata across resource types, use the table name
STANDARD_METADATA. This includes all fields exceptresource.DATA, which is specific to each resource type.
Query asset metadata
Console
To query the asset metadata for your project, folder, or organization, complete the following steps:
-
In the Google Cloud console, go to the Asset query tab on the Asset Inventory page.
If the Asset query tab doesn't appear, you need access to the Security Command Center Premium or Enterprise tier, or Gemini Cloud Assist.
- Change to the project, folder, or organization you want to query.
- Click the Asset query tab.
-
To query asset metadata, either use a sample query or build your own:
- To use a sample, click an entry in the Query library tab to preview the query. Click Apply to copy that sample into the Edit query box, then either edit the query, or click Run to execute it.
- To build your own query, enter the query text directly into the Edit query box, then click Run to execute it. To assist in writing your own query, you can click a table in the Select table pane to preview its schema and content. See Query syntax to learn how to construct a query.
The asset metadata matching the query is shown in the Query result tab.
-
Optional: To download the query result sets in CSV format, click Export.
The maximum size of the CSV file is 2 MB. If the download request fails because the file size exceeds this limit, a message appears with instructions for exporting the full results.
gcloud
gcloud asset query \ --SCOPE \ --statement="SQL_SELECT_QUERY" \ --timeout="TIMEOUTs"
Provide the following values:
-
SCOPE: Use one of the following values:-
project=PROJECT_ID, wherePROJECT_IDis the ID of the project that has the assets you want to query. -
folder=FOLDER_ID, whereFOLDER_IDis the ID of the folder that has the assets you want to query.How to find the ID of a Google Cloud folder
Google Cloud console
To find the ID of a Google Cloud folder, complete the following steps:
-
Go to the Google Cloud console.
- Click the switcher list box in the menu bar.
- Select your organization from the list box.
- Search for your folder name. The folder ID is shown next to the folder name.
gcloud CLI
You can retrieve the ID of a Google Cloud folder that's located at the organization level with the following command:
gcloud resource-manager folders list \ --organization=$(gcloud organizations describe ORGANIZATION_NAME \ --format="value(name.segment(1))") \ --filter='"DISPLAY_NAME":"TOP_LEVEL_FOLDER_NAME"' \ --format="value(ID)"
Where TOP_LEVEL_FOLDER_NAME is a partial or full string match for the folder's name. Remove the
--formatflag to see more information about the found folders.The previous command doesn't return the IDs of subfolders within folders. To do so, run the following command using a top level folder's ID:
gcloud resource-manager folders list --folder=FOLDER_ID
-
-
organization=ORGANIZATION_ID, whereORGANIZATION_IDis the ID of the organization that has the assets you want to query.How to find the ID of a Google Cloud organization
Google Cloud console
To find the ID of a Google Cloud organization, complete the following steps:
-
Go to the Google Cloud console.
- Click the switcher list box in the menu bar.
- Select your organization from the list box.
- Click the All tab. The organization ID is shown next to the organization name.
gcloud CLI
You can retrieve the ID of a Google Cloud organization with the following command:
-
-