☁️

gcloudの複数アカウント管理

2025/01/19に公開

gcloud configurations の活用法

gcloud コマンドでは、複数のアカウントやプロジェクトを簡単に切り替えるために configurations を利用できます。本記事では、複数アカウントを管理する際に役立つ方法を紹介します。

gcloud config configurations とは?

configurationsは、gcloudで使用するaccountやprojectなどの設定をまとめるものです。複数のprojectやaccountを簡単に切り替えることができます。

例えば:

  • 個人accountと仕事用accountを分けて管理
  • 開発環境用と本番環境用projectの切り替え

新しい configurations を作成する

  1. 以下のcommandで新しいconfigurationを作成します:
gcloud config configurations create CONFIG_NAME
  1. 次に、accountやprojectを設定します:
gcloud config set account ACCOUNT_EMAIL
gcloud config set project PROJECT_ID
  1. 作成したconfigurationsを確認する
    以下のcommandで、作成したconfigurationsをlist表示できます:
gcloud config configurations list

現在 active な設定はis_activeのcolumnで確認できます。

  1. 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
}

参考文献

https://cloud.google.com/sdk/gcloud/reference/config/configurations
https://note.com/shimakaze_soft/n/nb8f5f938f7e8
https://zenn.dev/tsujino/articles/1897d5c48a80be

Discussion