管理服務啟用狀態
階層式服務啟用功能可讓您管理透過 Google Cloud 資源階層繼承的服務啟用狀態。詳情請參閱總覽。
本文說明如何使用 Google Cloud CLI 管理專案、資料夾和機構的服務啟用狀態。
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
選取或建立專案所需的角色
- 選取專案:選取專案時,不需要具備特定 IAM 角色,只要您已獲授角色,即可選取任何專案。
-
建立專案:如要建立專案,您需要「專案建立者」角色 (
roles/resourcemanager.projectCreator),其中包含resourcemanager.projects.create權限。瞭解如何授予角色。
-
建立 Google Cloud 專案:
gcloud projects create PROJECT_ID
將
PROJECT_ID替換為您要建立的 Google Cloud 專案名稱。 -
選取您建立的 Google Cloud 專案:
gcloud config set project PROJECT_ID
將
PROJECT_ID替換為 Google Cloud 專案名稱。
-
如要使用現有專案進行本指南中的操作,請確認您具有完成本指南所需的權限。如果您建立新專案,則已具備必要權限。
啟用 Service Usage API:
啟用 API 時所需的角色
如要啟用 API,您需要具備服務使用情形管理員 IAM 角色 (
roles/serviceusage.serviceUsageAdmin),其中包含serviceusage.services.enable權限。瞭解如何授予角色。gcloud services enable serviceusage.googleapis.com
-
安裝 Google Cloud CLI。
-
若您採用的是外部識別資訊提供者 (IdP),請先使用聯合身分登入 gcloud CLI。
-
執行下列指令,初始化 gcloud CLI:
gcloud init -
選取或建立專案所需的角色
- 選取專案:選取專案時,不需要具備特定 IAM 角色,只要您已獲授角色,即可選取任何專案。
-
建立專案:如要建立專案,您需要「專案建立者」角色 (
roles/resourcemanager.projectCreator),其中包含resourcemanager.projects.create權限。瞭解如何授予角色。
-
建立 Google Cloud 專案:
gcloud projects create PROJECT_ID
將
PROJECT_ID替換為您要建立的 Google Cloud 專案名稱。 -
選取您建立的 Google Cloud 專案:
gcloud config set project PROJECT_ID
將
PROJECT_ID替換為 Google Cloud 專案名稱。
-
如要使用現有專案進行本指南中的操作,請確認您具有完成本指南所需的權限。如果您建立新專案,則已具備必要權限。
啟用 Service Usage API:
啟用 API 時所需的角色
如要啟用 API,您需要具備服務使用情形管理員 IAM 角色 (
roles/serviceusage.serviceUsageAdmin),其中包含serviceusage.services.enable權限。瞭解如何授予角色。gcloud services enable serviceusage.googleapis.com
- 如果您未指出要管理的 Google Cloud 資源,本文中的指令會預設為目前的專案。舉例來說,
gcloud beta services list --enabled會傳回目前專案的已啟用服務清單。您可以擷取目前的專案 ID:
gcloud config list --format='text(core.project)'
必要的角色
如要取得管理服務啟用狀態所需的權限,請要求管理員授予目標資源的服務使用情形管理員 (roles/serviceusage.serviceUsageAdmin) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
這個預先定義的角色具備管理服務啟用狀態所需的權限。如要查看確切的必要權限,請展開「Required permissions」(必要權限) 部分:
所需權限
如要管理服務啟用狀態,必須具備下列權限:
-
列出服務:
-
serviceusage.effectivepolicy.get -
serviceusage.services.list -
serviceusage.services.get
-
-
啟用服務:
-
serviceusage.consumerpolicy.get -
serviceusage.consumerpolicy.update -
serviceusage.groups.listExpandedMembers
-
-
停用服務:
-
serviceusage.consumerpolicy.get -
serviceusage.consumerpolicy.update -
serviceusage.consumerpolicy.analyze
-
列出服務
您可以使用 gcloud beta services list 指令,列出專案、資料夾或機構啟用或可用的 Google API 和服務。
服務啟用和可用性可從資源祖先繼承。資源的已啟用服務包括資源本身啟用的服務,以及所有資源上層啟用的服務。
專案
gcloud beta services list --enabled \
--project=PROJECT_ID
將 PROJECT_ID 替換為Google Cloud 專案 ID。或者,省略旗標,預設為目前的專案。
資料夾
gcloud beta services list --enabled \
--folder=FOLDER_ID
將 FOLDER_ID 替換為Google Cloud 資料夾 ID。
機構
gcloud beta services list --enabled \
--organization=ORGANIZATION_ID
將 ORGANIZATION_ID 替換為Google Cloud 機構資源 ID。
注意事項:
您必須指定下列其中一個旗標:
--available,列出可供使用的服務。--enabled,列出已啟用供使用的服務。
您可以使用其他標記來篩選及限制列出的服務。
啟用服務
您可以使用 gcloud beta services enable 指令,為專案、資料夾或機構啟用一或多項服務。
這項指令會更新目標資源的 ConsumerPolicy。詳情請參閱「管理消費者政策」。
專案
gcloud beta services enable SERVICE \
--project=PROJECT_ID
更改下列內容:
SERVICE:要啟用的服務名稱。如要啟用多項服務,請使用以空格分隔的服務名稱,例如下列指令會同時啟用 API 金鑰和 BigQuery 服務:gcloud beta services enable apikeys.googleapis.com bigquery.googleapis.com。PROJECT_ID:您的 Google Cloud 專案 ID。 或者,省略旗標,預設為目前的專案。
資料夾
gcloud beta services enable SERVICE \
--folder=FOLDER_ID
更改下列內容:
SERVICE:要啟用的服務名稱。如要啟用多項服務,請使用以空格分隔的服務名稱,例如:gcloud beta services enable apikeys.googleapis.com bigquery.googleapis.com。FOLDER_ID:您的Google Cloud 資料夾 ID。
機構
gcloud beta services enable SERVICE \
--organization=ORGANIZATION_ID
更改下列內容:
SERVICE:要啟用的服務名稱。如要啟用多項服務,請使用以空格分隔的服務名稱,例如:gcloud beta services enable apikeys.googleapis.com bigquery.googleapis.com。ORGANIZATION_ID:您的Google Cloud 機構資源 ID。
支援的旗標:
--async,立即返回,不必等待執行中的作業完成。--skip-dependency,略過啟用任何服務依附元件。否則,系統預設會啟用所有服務依附元件。您可以檢查服務依附元件。詳情請參閱「列出服務依附元件」。
--validate-only,即可驗證啟用狀態,不必實際執行作業。
注意事項:
如果您嘗試啟用已啟用的服務,系統會顯示錯誤訊息,指出該服務已啟用並存在於消費者政策中。(如果服務已啟用,但依附元件未啟用,指令會成功執行)。
如果在資料夾或機構層級啟用服務,機構政策限制
constraints/serviceuser.services可能無法正常運作。建議改用「Restrict Resource Service Usage」限制。詳情請參閱「限制資源用量」。Google Cloud 會為許多服務建立及管理服務帳戶。Google Cloud 這些服務帳戶稱為「服務代理」。如果遇到
service account not found錯誤,您可能需要使用gcloud beta services identity create指令建立服務代理程式。
停用服務
如要為專案、資料夾或機構停用一或多項服務,請使用 gcloud beta services disable 指令。
這項指令會更新目標資源的 ConsumerPolicy。詳情請參閱「管理消費者政策」。
專案
gcloud beta services disable SERVICE \
--project=PROJECT_ID
更改下列內容:
SERVICE:要停用的服務名稱。如要停用多項服務,請使用以空格分隔的服務名稱,例如:gcloud beta services disable apikeys.googleapis.com bigquery.googleapis.com。PROJECT_ID:您的 Google Cloud 專案 ID。 或者,省略旗標,預設為目前的專案。
資料夾
gcloud beta services disable SERVICE \
--folder=FOLDER_ID
更改下列內容:
SERVICE:要停用的服務名稱。如要停用多項服務,請使用以空格分隔的服務名稱,例如:gcloud beta services disable apikeys.googleapis.com bigquery.googleapis.com。FOLDER_ID:您的Google Cloud 資料夾 ID。
機構
gcloud beta services disable SERVICE \
--organization=ORGANIZATION_ID
更改下列內容:
SERVICE:要停用的服務名稱。如要停用多項服務,請使用以空格分隔的服務名稱,例如:gcloud beta services disable apikeys.googleapis.com bigquery.googleapis.com。ORGANIZATION_ID:您的Google Cloud 機構資源 ID。
支援的旗標:
--async,立即返回,不必等待執行中的作業完成。--bypass-api-usage-check略過用量檢查。否則,如果要在過去 30 天內停用服務,或在過去 3 天內啟用服務,系統會傳回錯誤。您可以指定下列其中一個旗標:
--bypass-dependency-service-check,略過依附元件檢查; 任何依附於您要停用服務的已啟用服務,都會維持啟用狀態。--disable-dependency-services繼續停用服務,以及依附於要停用服務的任何已啟用服務。
您可以檢查服務依附元件。詳情請參閱「列出服務依附元件」。
--force,即使有啟用中的服務依附於該服務、該服務在過去 30 天內曾使用過,或在過去 3 天內啟用過,依附服務也會一併停用。--bypass-api-usage-check、--bypass-dependency-service-check和--disable-dependency-services標記的優先順序都高於--force標記。同時使用
--disable-dependency-services和--bypass-api-usage-check旗標,等同於使用--force旗標。系統會停用服務及其依附元件,不會檢查使用情況。--validate-only,驗證停用作業,但不實際執行作業。
範例
啟用服務
- 啟用 BigQuery 服務,但不啟用其依附元件:
gcloud beta services enable bigquery.googleapis.com --skip-dependency
輸出內容:
Operation [operations/ucpat.p39-581601899707-73a57d57-aa46-4d0b-a5ee-57034a42f2b3] complete. Result: { "@type":"type.googleapis.com/google.api.serviceusage.with Hierarchical Service Activationbeta.ConsumerPolicy", "createTime":"2025-01-31T20:17:37.272343Z", "enableRules":[ { "services":[ "services/apikeys.googleapis.com", "services/compute.googleapis.com", "services/oslogin.googleapis.com", "services/serviceusage.googleapis.com", "services/bigquery.googleapis.com" ] } ], "etag":"W/\"W9nsVJK0V1m7ee7tM7pFDg==\"", "name":"projects/PROJECT_ID/consumerPolicies/default", "updateTime":"2025-09-11T23:05:22.758394Z" }
- 啟用 BigQuery 服務及其依附元件:
gcloud beta services enable bigquery.googleapis.com
輸出內容:
Operation [operations/ucpat.p39-581601899707-73192b0f-053c-46ee-911a-7eca6b8fe899] complete. Result: { "@type":"type.googleapis.com/google.api.serviceusage.with Hierarchical Service Activationbeta.ConsumerPolicy", "createTime":"2025-01-31T20:17:37.272343Z", "enableRules":[ { "services":[ "services/apikeys.googleapis.com", "services/compute.googleapis.com", "services/oslogin.googleapis.com", "services/serviceusage.googleapis.com", "services/cloudresourcemanager.googleapis.com", "services/dataplex.googleapis.com", "services/bigquery.googleapis.com", "services/bigquerystorage.googleapis.com", "services/bigqueryconnection.googleapis.com", "services/analyticshub.googleapis.com", "services/bigquerymigration.googleapis.com", "services/dataform.googleapis.com", "services/bigquerydatapolicy.googleapis.com", "services/bigquerydatatransfer.googleapis.com", "services/bigqueryreservation.googleapis.com" ] } ], "etag":"W/\"FDXMAIdsBW7BrXjL7oP6FA==\"", "name":"projects/PROJECT_ID/consumerPolicies/default", "updateTime":"2025-09-12T19:44:23.675769Z" }
停用服務
- Attempting to disable BigQuery service with active
dependencies:
gcloud beta services disable bigquery.googleapis.com
錯誤訊息:
ERROR: (gcloud.beta.services.disable) The services are depended on by the following active service(s) {"bigquery.googleapis.com": ["services/bigquerystorage.googleapis.com"]} . Please remove the active dependent services or provide the --disable-dependency-services flag to disable them, or --bypass-dependency-service-check to ignore this check.
- 嘗試停用近期曾使用的 BigQuery 服務:
gcloud beta services disable bigquery.googleapis.com --disable-dependency-services
錯誤訊息:
ERROR: (gcloud.beta.services.disable) The operation "operations/ucpat.p39-581601899707-d35c2e2b-d2a3-47af-a1c0-033ed65df236" resulted in a failure "The services bigquery.googleapis.com,bigquerystorage.googleapis.com have usage in the last 30 days or were enabled in the past 3 days. Please specify force if you want to proceed with the destructive policy change.
- 停用 BigQuery 服務及其依附元件,並略過用量檢查:
gcloud beta services disable bigquery.googleapis.com --disable-dependency-services --bypass-api-usage-check
輸出內容:
Operation [operations/ucpat.p39-581601899707-5c02aa04-7ad7-4eb6-a6a1-dc68653bcdb4] complete. Result: { "@type":"type.googleapis.com/google.api.serviceusage.v2beta.ConsumerPolicy", "createTime":"2025-01-31T20:17:37.272343Z", "enableRules":[ { "services":[ "services/analyticshub.googleapis.com", "services/apikeys.googleapis.com", "services/bigqueryconnection.googleapis.com", "services/bigquerydatapolicy.googleapis.com", "services/bigquerydatatransfer.googleapis.com", "services/bigquerymigration.googleapis.com", "services/bigqueryreservation.googleapis.com", "services/cloudresourcemanager.googleapis.com", "services/compute.googleapis.com", "services/dataform.googleapis.com", "services/dataplex.googleapis.com", "services/oslogin.googleapis.com", "services/serviceusage.googleapis.com" ] } ], "etag":"W/\"TqbPaELDHlZQOj7As1P06g==\"", "name":"projects/PROJECT_ID/consumerPolicies/default", "updateTime":"2025-09-30T21:39:40.746125Z" }