GCP

GCPのAPIをgcloudコマンド確認・有効にする方法

概要

GCPはサービスを利用する場合、そのサービスのAPIを有効にする必要があります。
GUI上から操作できますが、今回はコマンドラインで有効になっているAPIを調べたり、
APIを有効にする方法を調べてみました。

手順

前提

gcloudコマンドのデフォルトプロジェクトが確認対象のプロジェクトか確認

  • gcloudコマンドのデフォルトプロジェクト
    gcloud projects list

未設定の場合、以下で設定

  • Google Cloud プロジェクトのプロジェクト ID を確認

    gcloud projects list
  • デフォルト プロジェクトをそのプロジェクト変更

    gcloud config set project <<YOUR_PROJECT_ID>>

プロジェクトで使用可能なサービスを確認

プロジェクトで使用可能なサービスのAPI一覧表示させる

  • コマンド

    gcloud services list --available

  • ※formatを変更しないと見ずらいのでcsv、jsonに適宜変更
    ※--limit= で出力を制限してますが、全部みたい際には、このオプションを削除


$ gcloud services list --available  --format="csv(NAME,TITLE)" --limit=3
name,title
aapl-miriinfotech-public.cloudpartnerservices.goog,Miri Infotech lapp
abantecart-bitnami-launchpad.cloudpartnerservices.goog,Bitnami AbanteCart Certified by Bitnami
abilitec-api.endpoints.liveramp-identity-public.cloud.goog,AbiliTec API
$

$ gcloud services list --available --format="json(config.name,config.title)" --limit=3
[
  {
    "config": {
      "name": "aapl-miriinfotech-public.cloudpartnerservices.goog",
      "title": "Miri Infotech lapp"
    }
  },
  {
    "config": {
      "name": "abantecart-bitnami-launchpad.cloudpartnerservices.goog",
      "title": "Bitnami AbanteCart Certified by Bitnami"
    }
  },
  {
    "config": {
      "name": "abilitec-api.endpoints.liveramp-identity-public.cloud.goog",
      "title": "AbiliTec API"
    }
  }
]
$

有効化したいサービス名でフィルターして、対象APIを特定する
※名前だけだと、pubsub用のAPIが判断できないし、なぜかfilter のキーワードと違うものがヒットする。。

$ gcloud services list --available --filter=pubsub
NAME: pubsub.googleapis.com
TITLE: Cloud Pub/Sub API

NAME: pubsublite.googleapis.com
TITLE: Pub/Sub Lite API

NAME: redis.googleapis.com
TITLE: Google Cloud Memorystore for Redis API

NAME: solace-pubsub-platform.endpoints.solace-gcp-public.cloud.goog
TITLE: Solace PubSub+ Platform

NAME: storagetransfer.googleapis.com
TITLE: Storage Transfer API
$

現在のプロジェクトで有効な API を確認

現在のプロジェクトで有効な API とサービスを一覧表示

  • コマンド
gcloud services list
$ gcloud services list  --format="csv(NAME,TITLE)" --limit=3
name,title
autoscaling.googleapis.com,Cloud Autoscaling API
bigquery.googleapis.com,BigQuery API
bigquerymigration.googleapis.com,BigQuery Migration API
$

filter で絞り込みして、APIが有効化確認


  • ※の例では、「NAME: pubsub.googleapis.com」が有効であることがわかる
gcloud services list --filter=pubsub

$ gcloud services list --filter=pubsub
NAME: pubsub.googleapis.com
TITLE: Cloud Pub/Sub API
$

該当するサービス名を使用して、APIを有効

  • コマンド
gcloud services enable SERVICE_NAME

SERVICE_NAME は、「gcloud services list --available」のnameを指定する
例えば、Cloud Pub/Sub API の場合、「pubsub.googleapis.com」となる

$ gcloud services enable pubsub.googleapis.com
Operation "operations/acf.XXXXXXX" finished successfully.
$

※有効になれば、「gcloud services list」の結果に出力されます。

結論

そもそも、サービスでどのAPIを有効にすればいいのかが最初はわからないので、
初手からAPIをコマンドラインから有効にするのは難易度が高いですね。

例えば、複数のプロジェクトで同じ設定が必要な場合、1プロジェクトはGUIでAPIを
有効にしてAPIをリスト化し、それをベースに他プロジェクトはコマンドラインで
APIを有効にする等する際には便利そうです。

参考ドキュメント

プロジェクトで使用可能なサービスの一覧表示
https://cloud.google.com/service-usage/docs/list-services?hl=ja#available

プロジェクトで有効なサービスを一覧表示する
https://cloud.google.com/service-usage/docs/list-services?hl=ja#enabled

API の有効化
https://cloud.google.com/endpoints/docs/openapi/enable-api?hl=ja#enabling_an_api

gcloud projects list
https://cloud.google.com/sdk/gcloud/reference/projects/list

人気記事

1

Kubernetes kubectl rollout コマンドでdeploymentをロールバックしてみる 概要 kubectl rollout コマンドでデプロイ履歴を確認してロールバックするやり方 ...

2

  terraformのバージョン管理どうすればいいのか? terraformの複数バージョンを簡単に動かしたい terraformのインストール方法が分からない と、疑問を抱えている人の疑 ...

3

本記事では どんな環境にterraformをインストールできるの? terraformのインストールどうしたらいいの? terraformのどのバージョンをインストールすればいいの? と、困っている人 ...

4

Kubernetes kubectl version コマンドでバージョンを調べてみる 概要 kubectl version は、Kubernetes の Client、Serverのバージョンを確認 ...

-GCP