关于通过GCP Catalog API识别Compute Engine实例机型的技术咨询
Great question—this is a common pain point when working with the GCP Billing Catalog API, since instance size details (like vCPU count or exact machine type names) aren’t exposed as standalone filterable fields. While parsing the description field is one way, there are more reliable workarounds that reduce the need for full-text parsing:
1. Fetching SKUs for a specified core count
The best approach combines two GCP APIs to narrow down results before any description parsing:
- Step 1: Use the Compute Engine Machine Types API to get all machine types in your target region that match your desired core count. For example, to find all N1 instances with 8 vCPUs, query the API for machine types where
guestCpus = 8andnamestarts withn1-. This gives you exact machine type names (liken1-highcpu-8) and their corresponding families. - Step 2: Filter the Catalog API with targeted parameters
Use thecategory.resourceGroup(which maps to the instance family, e.g.,N1Highcpuforn1-highcpu-machines) plus region, usage type (OnDemand/Spot), and a partial description match for the core count. This drastically reduces the number of SKUs you need to process.
Example filter for the Catalog API:
serviceDisplayName:"Compute Engine" AND category.resourceGroup:"N1Highcpu" AND serviceRegions:"europe-west4" AND category.usageType:"OnDemand" AND description:contains("8 VCPU")
2. Fetching SKUs for a specific instance name (e.g., N1Standard 16)
Again, there’s no direct field for the exact instance name, but you can map the instance name to its family and core count to build a precise filter:
- Map the instance name to its family:
N1Standard 16corresponds to theN1StandardresourceGroup, with 16 vCPUs. - Use the same Catalog API filter pattern as above, replacing the resourceGroup and core count in the description match.
For example, to get SKUs for n1-standard-16 in europe-west4 (OnDemand):
serviceDisplayName:"Compute Engine" AND category.resourceGroup:"N1Standard" AND serviceRegions:"europe-west4" AND category.usageType:"OnDemand" AND description:contains("16 VCPU")
Bonus: Sample Python code
Here’s how you’d implement this with the GCP Python client library:
from google.cloud import billing_v1 def get_instance_skus(instance_family, core_count, region, usage_type="OnDemand"): client = billing_v1.CloudCatalogClient() # Compute Engine service ID is fixed as 6F81-5844-456A parent = "services/6F81-5844-456A" filter_str = ( f'serviceDisplayName:"Compute Engine" ' f'AND category.resourceGroup:"{instance_family}" ' f'AND serviceRegions:"{region}" ' f'AND category.usageType:"{usage_type}" ' f'AND description:contains("{core_count} VCPU")' ) request = billing_v1.ListSkusRequest(parent=parent, filter=filter_str) skus = list(client.list_skus(request=request)) return [(sku.sku_id, sku.description) for sku in skus] # Usage example: Get OnDemand SKUs for N1Highcpu 8 in europe-west4 skus = get_instance_skus("N1Highcpu", 8, "europe-west4") for sku_id, desc in skus: print(f"SKU ID: {sku_id}, Description: {desc}")
Key Notes
- The
category.resourceGroupvalue is consistent for each instance family (e.g.,N1Highmemfor memory-optimized N1 instances,N2Standardfor N2 standard instances). - Using partial description matches with the Catalog API’s filter is more efficient than parsing all SKUs, as filtering happens server-side.
- If you need to map many instance types, consider caching results from the Machine Types API and Catalog API queries to avoid repeated calls.
内容的提问来源于stack exchange,提问作者Harsha Biyani

