字
字节笔记本
2026年5月1日
使用 gcloud CLI 查看 GCP 账单完整指南
API中转
¥120
本文详细介绍如何使用 gcloud CLI 查看 GCP 账单相关配置,包括项目绑定账单查看、Budget 预算管理、以及通过 BigQuery Billing Export 查询实际消费金额的方法。gcloud CLI 本身不能直接显示"本月花了多少钱"这种明细账单金额,但可以通过多种方式获取所需信息。
先确认本地 gcloud 配置
bash
gcloud auth list查看当前登录账号。
bash
gcloud config list查看当前项目、账号、区域等配置。
重点看:
bash
gcloud config get-value project如果没有项目,可以设置:
bash
gcloud config set project YOUR_PROJECT_ID查看当前项目绑定的账单账号
bash
gcloud beta billing projects describe YOUR_PROJECT_ID或者当前项目:
bash
gcloud beta billing projects describe $(gcloud config get-value project)输出类似:
text
billingAccountName: billingAccounts/XXXXXX-XXXXXX-XXXXXX
billingEnabled: true
name: projects/YOUR_PROJECT_ID/billingInfo
projectId: YOUR_PROJECT_ID重点看 billingEnabled: true,说明项目已绑定账单。
列出你有权限查看的 Billing Accounts
bash
gcloud beta billing accounts list输出类似:
text
ACCOUNT_ID NAME OPEN
XXXXXX-XXXXXX-XXXXXX My Billing Account True查看 Billing Account 详情
bash
gcloud beta billing accounts describe XXXXXX-XXXXXX-XXXXXX注意不要带 billingAccounts/ 前缀,只填 ID。
查看所有项目的账单绑定情况
bash
gcloud projects list然后逐个查:
bash
for p in $(gcloud projects list --format="value(projectId)"); do
echo -n "$p: "
gcloud beta billing projects describe "$p" \
--format="value(billingAccountName,billingEnabled)" 2>/dev/null
done查看预算 Budgets
先列出 Billing Account:
bash
gcloud beta billing accounts list然后查看预算:
bash
gcloud billing budgets list \
--billing-account=XXXXXX-XXXXXX-XXXXXX查看某个预算详情:
bash
gcloud billing budgets describe BUDGET_ID \
--billing-account=XXXXXX-XXXXXX-XXXXXX实际消费金额查询:BigQuery Billing Export
如果已开启 Billing Export,可以用 BigQuery 查费用。
先看有哪些 BigQuery 数据集:
bash
bq ls查看某个数据集的表:
bash
bq ls YOUR_PROJECT_ID:YOUR_DATASETGCP Billing Export 表名类似:gcp_billing_export_v1_XXXXXX_XXXXXX_XXXXXX
查询本月费用(按服务分组):
bash
bq query --use_legacy_sql=false '
SELECT
service.description AS service,
ROUND(SUM(cost), 2) AS cost
FROM `YOUR_PROJECT_ID.YOUR_DATASET.gcp_billing_export_v1_XXXXXX_XXXXXX_XXXXXX`
WHERE
invoice.month = FORMAT_DATE("%Y%m", CURRENT_DATE())
GROUP BY service
ORDER BY cost DESC
'查询各月总费用:
bash
bq query --use_legacy_sql=false '
SELECT
invoice.month,
ROUND(SUM(cost), 2) AS total_cost
FROM `YOUR_PROJECT_ID.YOUR_DATASET.gcp_billing_export_v1_XXXXXX_XXXXXX_XXXXXX`
GROUP BY invoice.month
ORDER BY invoice.month DESC
'常用命令速查
bash
gcloud auth list
gcloud config list
gcloud config get-value project
gcloud beta billing accounts list
gcloud beta billing projects describe $(gcloud config get-value project)
gcloud beta billing accounts describe YOUR_BILLING_ACCOUNT_ID
gcloud billing budgets list --billing-account=YOUR_BILLING_ACCOUNT_ID总结
gcloud CLI 可以查项目配置、账单绑定关系和预算设置,但要查实际消费金额,最靠谱的方式是结合 BigQuery Billing Export 使用 bq query。如果只是想确认有没有欠费、账单是否开启,先跑这个命令:
bash
gcloud beta billing projects describe $(gcloud config get-value project)分享: