I am using a REST API to get a JSON dump of all GCP resources present in a project. With only a few resources present in the GCP project , API response time is very high, it is taking close to 30 seconds. My guess is that with the SDK it should be faster, no? or an architectural change maybe. The size of the JSON file is around 3MB
Hi @dheerajpanyam ,
Welcome to Google Cloud Community!
Yes, there’s Google Cloud SDK (gcloud CLI and client libraries) support to get an export of GCP inventory from a GCP project.
Here are workaround, documentation and examples that may help:
- Using the CLI gcloud asset list is the easiest way to interact with the Cloud Asset Inventory API from the command line. It provides a convenient interface without requiring you to write code.
Example 1
gcloud asset list --project=<your-project-id> --format=json > inventory.json
Example 2
gcloud asset list --project=<your-project-id> --asset-types="compute.googleapis.com/Instance","storage.googleapis.com/Bucket" --format=json > inventory.json
--asset-types="compute.googleapis.com/Instance","storage.googleapis.com/Bucket": Specifies a comma-separated list of asset types to retrieve. See the Supported Asset Types for a complete list.
-
Use Google Cloud Asset Inventory API Client Libraries instead of directly calling the API. They give you more control, handle errors better, and process data faster.
-
Instead of repeatedly querying the API, configure Cloud Asset Inventory to continuously export your asset metadata to Google Cloud Storage or BigQuery.
Cloud Asset Inventory Export to BigQuery Example and Steps:
Steps
-
Enable the Cloud Asset Inventory API: (If you haven’t already)
-
Create a Cloud Asset Feed: Use the gcloud asset feeds create command to define a feed that exports data to a BigQuery dataset.
gcloud asset feeds create \
--feed-id=my-asset-feed \
--asset-types="compute.googleapis.com/Instance","storage.googleapis.com/Bucket" \
--content-type=RESOURCE \
--project=<your-project-id> \
--location=global \
--output-config-bigquery-dataset=projects/<your-project-id>/datasets/<your-bigquery-dataset>
SQL to query the BigQuery table example
SELECT
resource.data.name
FROM
`<your-project-id>.<your-bigquery-dataset>.assets` #Replace "assets" with your actual table name
WHERE
asset_type = 'compute.googleapis.com/Instance'
If you need urgent assistance and detailed guidance, please reach out to Google Cloud Support directly.
Was this helpful? If so, please accept this answer as “Solution”. If you need additional assistance, reply here within 2 business days and I’ll be happy to help.
Thanks @reinc
Welcome @dheerajpanyam , please accept my answer as “Solution”. Thank you.