サービスの有効化を管理する

階層型サービスのアクティベーションを使用すると、 Google Cloud リソース階層を介して継承されるサービスの有効化を管理できます。詳細については、概要をご覧ください。

このドキュメントでは、Google Cloud CLI を使用してプロジェクト、フォルダ、組織のサービス有効化を管理する方法について説明します。

始める前に

  1. Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
  2. Install the Google Cloud CLI.

  3. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  4. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  5. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  6. このガイドで既存のプロジェクトを使用する場合は、このガイドを完了するために必要な権限があることを確認します。新しいプロジェクトを作成した場合は、必要な権限がすでに付与されています。

  7. Verify that billing is enabled for your Google Cloud project.

  8. Enable the Service Usage API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable serviceusage.googleapis.com
  9. Install the Google Cloud CLI.

  10. 外部 ID プロバイダ(IdP)を使用している場合は、まず連携 ID を使用して gcloud CLI にログインする必要があります。

  11. gcloud CLI を初期化するには、次のコマンドを実行します。

    gcloud init
  12. Create or select a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.
    • Create a Google Cloud project:

      gcloud projects create PROJECT_ID

      Replace PROJECT_ID with a name for the Google Cloud project you are creating.

    • Select the Google Cloud project that you created:

      gcloud config set project PROJECT_ID

      Replace PROJECT_ID with your Google Cloud project name.

  13. このガイドで既存のプロジェクトを使用する場合は、このガイドを完了するために必要な権限があることを確認します。新しいプロジェクトを作成した場合は、必要な権限がすでに付与されています。

  14. Verify that billing is enabled for your Google Cloud project.

  15. Enable the Service Usage API:

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    gcloud services enable serviceusage.googleapis.com
  16. 管理する Google Cloud リソースを指定しない場合、このドキュメントのコマンドはデフォルトで現在のプロジェクトになります。たとえば、gcloud beta services list --enabled は現在のプロジェクトで有効になっているサービスのリストを返します。

    現在のプロジェクト ID を取得できます。

    gcloud config list --format='text(core.project)'
  17. 必要なロール

    サービス有効化の管理に必要な権限を取得するには、ターゲット リソースに対するサービス使用管理者 roles/serviceusage.serviceUsageAdmin)IAM ロールを付与するよう管理者に依頼してください。ロールの付与については、プロジェクト、フォルダ、組織に対するアクセス権の管理をご覧ください。

    この事前定義ロールには、サービスの有効化を管理するために必要な権限が含まれています。必要とされる正確な権限については、「必要な権限」セクションを開いてご確認ください。

    必要な権限

    サービスの有効化を管理するには、次の権限が必要です。

    • サービスを一覧表示する:
      • 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

    カスタムロールや他の事前定義ロールを使用して、これらの権限を取得することもできます。

    サービスの一覧表示

    プロジェクト、フォルダ、組織で有効になっている、または使用可能な Google API とサービスを一覧表示するには、gcloud beta services list コマンドを使用します。

    サービスを有効にするかどうかと可用性は、リソースの祖先から継承できます。リソースで有効になっているサービスには、リソース自体で有効になっているサービスと、すべてのリソースの祖先で有効になっているサービスが含まれます。

    プロジェクト

    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 に置き換えます。

    次の点にご注意ください。

    • 次のフラグのいずれか 1 つを指定する必要があります。

      • --available: 消費用に有効にできるサービスを一覧表示します。
      • --enabled: 消費用に有効になっているサービスを一覧表示します。
    • 他のフラグを使用して、リストに表示されるサービスをフィルタして制限できます。

    サービスを有効化する

    gcloud beta services enable コマンドを使用して、プロジェクト、フォルダ、または組織に対して 1 つ以上のサービスを有効にできます。

    このコマンドは、ターゲット リソースの 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 が期待どおりに機能しないことがあります。代わりに、リソース サービスの使用を制限する制約を使用することをおすすめします。詳細については、リソース使用量の制限をご覧ください。

    • Google Cloud は、多くのGoogle Cloud サービスのサービス アカウントを作成および管理します。これらのサービス アカウントは、サービス エージェントとも呼ばれます。service account not found エラーが発生した場合は、gcloud beta services identity create コマンドを使用してサービス エージェントを作成する必要があります。

    サービスを無効にする

    gcloud beta services disable コマンドを使用して、プロジェクト、フォルダ、または組織で 1 つ以上のサービスを無効にできます。

    このコマンドは、ターゲット リソースの 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 日間に有効にされた場合、システムはエラーを返します。

    • 次のフラグのいずれか 1 つを指定できます。

      • --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"
        }

    サービスを無効にする

    • 依存関係がアクティブな状態で BigQuery サービスを無効にしようとしています。
      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"
      }

    次のステップ