如何程序化通过SKU获取谷歌云虚拟机规格并关联定价信息?
Great question—let's walk through the best ways to programmatically map Google Cloud Compute Engine SKUs to their underlying VM specs, since you can't rely on the built-in pricing calculator.
The GET https://cloudbilling.googleapis.com/v1/services?key=API_KEY endpoint (and the entire Cloud Billing Pricing API) is designed purely to return pricing-to-SKU mappings, not hardware specifications for underlying resources like VMs. There's no way to modify this request to include VM specs directly—you'll need to use complementary tools or APIs to bridge the gap.
This is the most reliable approach, as it pulls official, up-to-date VM specs directly from Google's API, then links them to your SKU data.
Step-by-step:
Fetch all available VM machine types
Call the Compute Engine Machine Types API to get specs like CPU cores, memory, and model names. You can query per zone (most accurate, since some models are region-specific):GET https://compute.googleapis.com/compute/v1/projects/[YOUR_PROJECT_ID]/zones/[TARGET_ZONE]/machineTypesOr get a global list (for broader coverage):
GET https://compute.googleapis.com/compute/v1/projects/[YOUR_PROJECT_ID]/global/machineTypesEach returned
machineTypeobject includes fields likeguestCpus(core count),memoryMb(total memory), andname(the official model ID, e.g.,n2-standard-4).Map SKUs to machine types
The SKUs from your Pricing API response will havedisplayNameordescriptionfields that reference VM models (e.g., "Compute Engine N2 Standard Instance Core running in Americas"). You can:- Parse the model prefix (like
n2-standard) and core count from the SKU's display name/description - Match this parsed data to the
namefield from the Compute Engine API's machine types - Once matched, you can attach the CPU/memory specs directly to the SKU's pricing data
- Parse the model prefix (like
If you want to avoid making additional API requests, many Compute Engine SKUs include spec details directly in their attributes field. For example:
"attributes": { "machineFamily": "n2", "cpu": "4", "memory": "16GiB", "region": "us-central1" }
You can parse these attributes to pull CPU core counts, memory capacity, and machine family info without needing to call the Compute Engine API. Note that not all SKUs have fully populated attributes, so this method is slightly less reliable than Method 1, but it's faster and simpler for common VM SKUs.
Google publishes a full catalog of SKU details (including specs for many resources) that you can access programmatically. Call the full SKU list endpoint for Compute Engine:
GET https://cloudbilling.googleapis.com/v1/services/cloud.googleapis.com/skus?key=API_KEY
This returns every SKU associated with Compute Engine, and many include the same attributes field mentioned in Method 2, plus detailed descriptions that make parsing specs easier. You can cache this data locally to avoid repeated API calls if needed.
- Region-specific models: Some VM types are only available in certain regions, so make sure to align the
regionattribute from your SKUs with the zones/regions you query in the Compute Engine API. - Filter SKU types: Compute Engine has SKUs for on-demand instances, committed use discounts, spot instances, etc. Filter for the SKU type you care about before mapping to specs.
- Permissions: Ensure your API key or service account has the
compute.machineTypes.listpermission if using Method 1, andcloudbilling.skus.listfor Method 3.
内容的提问来源于stack exchange,提问作者user9824435

