You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何程序化通过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.

1. First: Why your original Pricing API request doesn't return VM specs

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.

2. Method 1: Use the Compute Engine API to fetch specs and match SKUs

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]/machineTypes
    

    Or get a global list (for broader coverage):

    GET https://compute.googleapis.com/compute/v1/projects/[YOUR_PROJECT_ID]/global/machineTypes
    

    Each returned machineType object includes fields like guestCpus (core count), memoryMb (total memory), and name (the official model ID, e.g., n2-standard-4).

  • Map SKUs to machine types
    The SKUs from your Pricing API response will have displayName or description fields that reference VM models (e.g., "Compute Engine N2 Standard Instance Core running in Americas"). You can:

    1. Parse the model prefix (like n2-standard) and core count from the SKU's display name/description
    2. Match this parsed data to the name field from the Compute Engine API's machine types
    3. Once matched, you can attach the CPU/memory specs directly to the SKU's pricing data
3. Method 2: Extract specs directly from SKU attributes (no extra API calls)

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.

4. Method 3: Use Google's public pricing catalog data

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.

Key Notes
  • Region-specific models: Some VM types are only available in certain regions, so make sure to align the region attribute 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.list permission if using Method 1, and cloudbilling.skus.list for Method 3.

内容的提问来源于stack exchange,提问作者user9824435

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:03:59