gcloudアカウントとプロジェクトを一発で切り替える方法

複数のGCPプロジェクトや異なるアカウントを使って開発していると、gcloud config setを毎回実行するのは面倒です。gcloud config configurationsを使えば、設定をまとめて切り替えることができます。 設定の作成 新しい設定を作成します: 1 2 3 4 5 6 7 8 9 # 新しい設定を作成 gcloud config configurations create my-dev-config # 設定を切り替え gcloud config configurations activate my-dev-config # アカウントとプロジェクトを設定 gcloud config set account user@example.com gcloud config set project my-dev-project 設定の一覧表示 1 2 # 設定一覧を表示 gcloud config configurations list 出力例: 1 2 3 NAME IS_ACTIVE ACCOUNT PROJECT default False old-user@gmail.com old-project my-dev-config True user@example.com my-dev-project 設定の切り替え 1 2 3 # 設定を切り替える gcloud config configurations activate default gcloud config configurations activate my-dev-config よく使うコマンド 1 2 3 4 5 6 7 8 # 現在の設定を確認 gcloud config list # 設定を削除 gcloud config configurations delete my-dev-config # 設定を複製 gcloud config configurations create new-config --activate 実際の使用例 開発環境とプロダクション環境を切り替える場合:...

July 18, 2025

GCPでカスタムロールをサービスアカウントにbindingしようとしてエラーになる場合

1 2 3 4 $ gcloud projects add-iam-policy-binding myproject --member=serviceAccount:myserviceaccount@myproject.iam.gserviceaccount.com --role='roles/mycustomrole' ERROR: Policy modification failed. For a binding with condition, run "gcloud alpha iam policies lint-condition" to identify issues in condition. ERROR: (gcloud.projects.add-iam-policy-binding) INVALID_ARGUMENT: Role roles/mycustomrole is not supported for this resource. --role の指定を roles/mycustomrole ではなく projects/myproject/roles/mycustomroleにすればOK 1 2 3 $ gcloud projects add-iam-policy-binding myproject --member=serviceAccount:myserviceaccuont@myproject.iam.gserviceaccount.com --role='projects/myproject/roles/mycustomrole' Updated IAM policy for project [myproject].

March 3, 2020