Google Cloud CLI は Google Cloud のリソースを作成・管理するためのコマンドラインツールです。Google Cloud SDK に含まれており、プロジェクト管理からデプロイまで幅広い操作を行えます。
インストール
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| brew install --cask google-cloud-sdk
sudo apt-get install google-cloud-cli
curl https://sdk.cloud.google.com | bash exec -l $SHELL
gcloud version
gcloud components update
|
インストールガイド(公式)
初期設定
gcloud init
対話式でプロジェクト・リージョン・ゾーンを設定します。
1 2 3 4 5
| gcloud init
gcloud init --console-only
|
gcloud init リファレンス
認証
1 2 3 4 5 6 7 8 9 10 11 12
| gcloud auth login
gcloud auth activate-service-account --key-file=credentials.json
gcloud auth application-default login
gcloud auth list
|
gcloud auth リファレンス
プロジェクト設定
1 2 3 4 5 6 7 8 9
| gcloud config set project PROJECT_ID
gcloud config get project
gcloud config set compute/region asia-northeast1 gcloud config set compute/zone asia-northeast1-a
|
名前付き構成(configurations)
プロジェクトや認証情報のセットを切り替えて使えます。本番環境と開発環境の切り替えに便利です。
1 2 3 4 5 6 7 8 9 10 11 12 13
| gcloud config configurations list
gcloud config configurations create dev gcloud config set project my-dev-project gcloud config set compute/region asia-northeast1
gcloud config configurations activate prod
gcloud compute instances list --configuration=prod
|
gcloud config configurations リファレンス
よく使う共通オプション
| オプション |
説明 |
--project=PROJECT_ID |
プロジェクトを指定(デフォルト設定を上書き) |
--format=json |
出力形式を指定(json、yaml、csv、table) |
--filter="EXPRESSION" |
結果をフィルタリング |
--quiet / -q |
確認プロンプトをスキップ |
--verbosity=debug |
デバッグ出力を有効化 |
--impersonate-service-account=SA |
サービスアカウントになりすまして実行 |
1 2 3 4 5 6 7 8
| gcloud compute instances list --format=json
gcloud compute instances list --filter="zone:asia-northeast1-a AND status=RUNNING"
gcloud projects list --format="table(projectId, name, projectNumber)"
|
gcloud topic filters(公式)
gcloud topic formats(公式)
API の有効化
Google Cloud の各サービスは API を有効化してから使います。
1 2 3 4 5 6 7 8 9
| gcloud services enable run.googleapis.com gcloud services enable container.googleapis.com
gcloud services list --enabled
gcloud services list --available --filter="name:run"
|
gcloud services リファレンス
サブコマンド別の解説
参考リンク