GCP CLI を AWS CLI と比較する(gcloud / gsutil / bq 編)
GCP シリーズの第3弾。今回は コマンドラインの使い方 を AWS CLI と対比しながら整理する。
AWS は aws という1つのコマンドにサービスがぶら下がる構成だが、GCP は 用途ごとにコマンドが分かれている のがまず最初の違い。
まず構成の違い
| AWS | GCP | |
|---|---|---|
| 基本コマンド |
aws 一本 |
gcloud がメイン |
| ストレージ操作 |
aws s3 / aws s3api
|
gsutil(または新しい gcloud storage) |
| データ分析 |
aws ...(Athena等) |
bq(BigQuery 専用) |
| 形 | aws <service> <operation> |
gcloud <group> <subgroup> <verb> |
# AWS:1コマンドにサービスがぶら下がる
aws ec2 describe-instances
aws s3 ls
# GCP:動詞ベースの階層。ストレージ/BQは別コマンド
gcloud compute instances list
gsutil ls
bq query '...'
GCP のコマンドは
名詞(リソース) + 動詞(list/create/describe…)の語順が基本。
AWS はサービス + API操作名(describe-instances 等)で、API名がほぼそのまま出てくる。
セットアップ・認証
| やりたいこと | AWS | GCP |
|---|---|---|
| 初期設定(対話) | aws configure |
gcloud init |
| ログイン(ユーザー) | (IAMキーを設定) | gcloud auth login |
| アプリ用の認証情報 | 環境変数 / プロファイル | gcloud auth application-default login |
| サービス用の鍵 | アクセスキー / IAM Role |
サービスアカウント:gcloud auth activate-service-account --key-file=KEY.json
|
| 設定の確認 | aws configure list |
gcloud config list |
| 認証情報の一覧 | aws sts get-caller-identity |
gcloud auth list |
プロファイル / コンテキストの切り替え
AWS は プロファイル、GCP は configuration(構成) で複数環境を切り替える。
# AWS:プロファイル
aws s3 ls --profile prod
export AWS_PROFILE=prod
# GCP:configuration(プロジェクト/アカウント/リージョンのセット)
gcloud config configurations create prod
gcloud config configurations activate prod
gcloud config set project my-prod-project
gcloud config set compute/region asia-northeast1
AWS の「プロファイル=認証情報の束」に対し、GCP の「configuration = アカウント+プロジェクト+既定リージョン等のセット」。
GCP ではgcloud config set projectで対象プロジェクトを切り替えるのが日常操作(AWSのアカウント切り替えに相当)。
よく使うコマンド対応表
コンピュート(VM)
| 操作 | AWS(EC2) | GCP(Compute Engine) |
|---|---|---|
| 一覧 | aws ec2 describe-instances |
gcloud compute instances list |
| 作成 | aws ec2 run-instances ... |
gcloud compute instances create NAME ... |
| 起動 | aws ec2 start-instances --instance-ids i-xxx |
gcloud compute instances start NAME |
| 停止 | aws ec2 stop-instances --instance-ids i-xxx |
gcloud compute instances stop NAME |
| 削除 | aws ec2 terminate-instances --instance-ids i-xxx |
gcloud compute instances delete NAME |
| SSH |
ssh (鍵を自前管理) |
gcloud compute ssh NAME(鍵を自動管理) |
ストレージ(オブジェクト)
| 操作 | AWS(S3) | GCP(Cloud Storage) |
|---|---|---|
| バケット一覧 | aws s3 ls |
gsutil ls / gcloud storage ls
|
| 中身を見る | aws s3 ls s3://bucket/ |
gsutil ls gs://bucket/ |
| アップロード | aws s3 cp file s3://bucket/ |
gsutil cp file gs://bucket/ |
| ダウンロード | aws s3 cp s3://bucket/file . |
gsutil cp gs://bucket/file . |
| 同期 | aws s3 sync ./dir s3://bucket/ |
gsutil rsync -r ./dir gs://bucket/ |
| 削除 | aws s3 rm s3://bucket/file |
gsutil rm gs://bucket/file |
URIスキームが
s3://↔gs://で対応。cp/rm/lsなどサブコマンドの語感はほぼ同じで移行しやすい。
なお GCP はgsutilをgcloud storageに統合中。新規ならgcloud storage cp ...でもOK(大容量転送は高速)。
IAM・権限
| 操作 | AWS | GCP |
|---|---|---|
| 誰として実行中か | aws sts get-caller-identity |
gcloud auth list |
| 権限の付与 | aws iam attach-role-policy ... |
gcloud projects add-iam-policy-binding PROJECT --member=... --role=... |
| ポリシー確認 | aws iam get-role / list-policies |
gcloud projects get-iam-policy PROJECT |
| サービスID作成 | aws iam create-role |
gcloud iam service-accounts create NAME |
データ分析(BigQuery)
BigQuery 専用の bq コマンドがある(AWS だと Athena/Redshift 相当だが、CLI 体験はかなり違う)。
| 操作 | GCP(bq) |
|---|---|
| クエリ実行 | bq query --use_legacy_sql=false 'SELECT ...' |
| データセット一覧 | bq ls |
| テーブル情報 | bq show DATASET.TABLE |
| ロード | bq load DATASET.TABLE gs://bucket/file.csv |
出力フォーマット・絞り込み
ここは思想がはっきり分かれるポイント。
| AWS | GCP | |
|---|---|---|
| 出力形式 | --output json/table/text/yaml |
--format=json/yaml/table/value/csv |
| 絞り込み |
JMESPath:--query "Reservations[].Instances[].InstanceId"
|
--filter(サーバー/クライアント側)+ --format
|
| 例 | aws ec2 describe-instances --query "...JMESPath..." |
gcloud compute instances list --filter="status=RUNNING" --format="value(name)" |
# AWS:JMESPath でクエリ
aws ec2 describe-instances \
--query "Reservations[].Instances[].{ID:InstanceId,State:State.Name}" \
--output table
# GCP:--filter で絞って --format で整形
gcloud compute instances list \
--filter="status=RUNNING" \
--format="table(name, zone, status)"
AWS は JMESPath(
--query) で抽出するのが定石。GCP は--filterで絞り、--formatで出す という分業。
スクリプトで値だけ取り出すなら GCP は--format="value(name)"が便利(AWSの--query ... --output text相当)。
知っておくと楽な小ネタ
-
ヘルプ:
gcloud <group> --help/gcloud help。AWS はaws <service> help。 -
ドライラン的な確認:多くの
gcloudコマンドは実行前に確認プロンプトが出る。スクリプトでは--quiet(-q)で抑制。 -
対象の既定値:
gcloud config setでプロジェクト・リージョン・ゾーンを既定化すると、毎回--project等を書かずに済む(AWSの既定リージョン/プロファイルに相当)。 -
JSON処理:両者とも
--format=json/--output json→jqに流すのが鉄板。 -
コンポーネント管理:
gcloud components list / update(Homebrew導入時はbrew側で更新する点に注意)。
まとめ
| 観点 | AWS | GCP |
|---|---|---|
| コマンド構成 |
aws 一本(サービス + API操作) |
gcloud / gsutil / bq に分割(名詞 + 動詞) |
| 環境切替 | プロファイル(--profile) |
configuration + gcloud config set project
|
| ストレージURI | s3:// |
gs:// |
| 抽出・整形 | JMESPath(--query)+ --output
|
--filter + --format
|
| 認証 | アクセスキー / IAM Role |
gcloud auth login / サービスアカウント鍵 |
- 一番つまずくのは 「コマンドが3つに分かれている」 ことと 「
--query(JMESPath)ではなく--filter+--format」 の2点。 - ここさえ押さえれば、AWS CLI の知識はかなりそのまま移植できる。
関連記事:
- [GCP のリソース階層を AWS と比較する(Organization / Folder / Project 編)]
- [GCP と AWS で比較する IAM とポリシー継承の仕組み]