gcloud – Google Cloud CLI
HTTP/APIクライアント 2025.08.31
gcloud コマンドは、Google Cloud Platform (GCP) を操作する公式 CLI ツールです。
プロジェクト管理、Compute Engine の VM 作成、Cloud Storage の操作、IAM 権限設定、デプロイなどをコマンドラインから実行できます。
構文(Syntax)
gcloud <サービス> <リソース> <サブコマンド> [オプション]
例:
gcloud compute instances list
gcloud storage buckets create my-bucket
gcloud iam service-accounts create my-service
主なサービスごとの利用例
プロジェクト管理
| コマンド | 説明 | 使用例 |
|---|
gcloud projects list | プロジェクト一覧を表示 | gcloud projects list |
gcloud config set project <ID> | 使用するプロジェクトを設定 | gcloud config set project my-project-id |
Compute Engine(仮想マシン)
| コマンド | 説明 | 使用例 |
|---|
gcloud compute instances list | VM インスタンス一覧を表示 | gcloud compute instances list |
gcloud compute instances create | VM インスタンスを作成 | gcloud compute instances create my-vm --zone=asia-northeast1-b |
gcloud compute ssh | VM に SSH 接続 | gcloud compute ssh my-vm --zone=asia-northeast1-b |
gcloud compute instances delete | VM を削除 | gcloud compute instances delete my-vm --zone=asia-northeast1-b |
Cloud Storage(オブジェクトストレージ)
| コマンド | 説明 | 使用例 |
|---|
gcloud storage buckets list | バケット一覧を表示 | gcloud storage buckets list |
gcloud storage buckets create | 新しいバケットを作成 | gcloud storage buckets create my-bucket --location=ASIA |
gcloud storage cp | ファイルをコピー | gcloud storage cp file.txt gs://my-bucket/ |
gcloud storage rm | オブジェクト削除 | gcloud storage rm gs://my-bucket/file.txt |
IAM(アクセス管理)
| コマンド | 説明 | 使用例 |
|---|
gcloud iam service-accounts list | サービスアカウント一覧 | gcloud iam service-accounts list |
gcloud iam service-accounts create | サービスアカウントを作成 | gcloud iam service-accounts create my-service |
gcloud projects add-iam-policy-binding | IAM 権限を付与 | gcloud projects add-iam-policy-binding my-project --member="user:me@example.com" --role="roles/viewer" |
Cloud Run / App Engine
| コマンド | 説明 | 使用例 |
|---|
gcloud run deploy | Cloud Run にデプロイ | gcloud run deploy my-service --image gcr.io/my-project/my-image |
gcloud app deploy | App Engine にデプロイ | gcloud app deploy app.yaml |
実行例
CLI 初期設定
gcloud init
対話例:
Pick cloud project to use:
[1] my-project-1
[2] my-project-2
プロジェクトを設定
gcloud config set project my-project-id
VM インスタンスを作成
gcloud compute instances create test-vm --zone=asia-northeast1-b --machine-type=e2-micro
Cloud Storage バケットを作成
gcloud storage buckets create my-bucket --location=ASIA
IAM 権限を付与
gcloud projects add-iam-policy-binding my-project \
--member="user:me@example.com" \
--role="roles/editor"
エラー例(認証情報が未設定)
gcloud compute instances list
出力例:
ERROR: (gcloud.compute.instances.list) Some requests did not succeed:
- Required 'compute.instances.list' permission for 'projects/my-project'
関連コマンド
gsutil : 旧 Cloud Storage 専用 CLI(gcloud storage に統合推奨)。bq : BigQuery 専用 CLI。kubectl : GKE(Google Kubernetes Engine)操作に利用。
備考
- 認証情報は
gcloud auth login またはサービスアカウントキーで設定可能。 - デフォルト設定は
~/.config/gcloud/ に保存される。 gcloud alpha / gcloud beta で新機能を試すこともできる。
参考
コメント