☁️
gcloudの複数アカウント管理
gcloud configurations の活用法
gcloud コマンドでは、複数のアカウントやプロジェクトを簡単に切り替えるために configurations を利用できます。本記事では、複数アカウントを管理する際に役立つ方法を紹介します。
gcloud config configurations とは?
configurations
は、gcloudで使用するaccountやprojectなどの設定をまとめるものです。複数のprojectやaccountを簡単に切り替えることができます。
例えば:
- 個人accountと仕事用accountを分けて管理
- 開発環境用と本番環境用projectの切り替え
新しい configurations を作成する
- 以下のcommandで新しいconfigurationを作成します:
gcloud config configurations create CONFIG_NAME
- 次に、accountやprojectを設定します:
gcloud config set account ACCOUNT_EMAIL
gcloud config set project PROJECT_ID
- 作成したconfigurationsを確認する
以下のcommandで、作成したconfigurationsをlist表示できます:
gcloud config configurations list
現在 active な設定はis_active
のcolumnで確認できます。
- configurations を切り替える
以下のcommandで、任意の設定に切り替え可能です:
gcloud config configurations activate CONFIG_NAME
より便利なカスタマイズ
複数のconfigurations
を使う場合、以下のような関数を.zshrc
等に追加すると切り替えがさらに便利になります:
# sgcはswitch gcloud configの略
function sgc() {
if [ -z "$1" ]; then
echo "Usage: sgc CONFIG_NAME"
return 1
fi
gcloud config configurations activate "$1" --quiet
echo "################ gcloud config configurations list is shown below: ###################"
gcloud config configurations list
}
参考文献
Discussion